I got a lot of data over the years, and I'm looking for a tool that can get a folder name and then move all the files to subfolders by year, like:
2005
2006
2007
etc..
Anyone knows something or similar tool?
|
|
Save this text into a file (
It loops through the source directory, gets the mtime of all files and folders, and moves them to the destination directory. Note: It's not recursive! Use it at your own risk!
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
import os
import sys
import shutil
from datetime import date
# Check arguments
if len(sys.argv) > 2:
src = sys.argv[1]
dst = sys.argv[2]
else:
print "Arguments: ./sort.py [src] [dst]"
sys.exit(1)
# Check paths
if not os.access(src, os.R_OK):
print "Source path not found"
sys.exit(2)
if not os.access(dst, os.W_OK):
print "Destination path not found"
sys.exit(3)
# Start sorting from root
for f in os.listdir(src):
fpath = os.path.join(src, f)
mtime = os.stat(fpath).st_mtime
year = str(date.fromtimestamp(mtime).year)
ypath = os.path.join(dst, year)
if not os.access(ypath, os.W_OK):
os.mkdir(ypath)
print "Moving %s to %s" % (fpath, ypath)
shutil.move(fpath, ypath)
|
|||
|
|
|
A solution using Save the following script to
make it executable, then run
where
where If you comment the line with The script take care of name clashes with the backup feature of |
|||||
|