Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial release of group-file-by-date
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/env python | ||
|
||
import sys | ||
import getopt | ||
import os | ||
import time | ||
|
||
opts, args = getopt.getopt(sys.argv[1:], "s:d:p:h") | ||
|
||
source = None | ||
destination = None | ||
show_help = False | ||
pattern = "%Y-%m-%d" | ||
|
||
for o, a in opts: | ||
if o == "-s": | ||
source = a | ||
elif o == "-d": | ||
destination = a | ||
elif o == "-p": | ||
pattern = a | ||
elif o == "h": | ||
show_help = True | ||
|
||
def help(): | ||
print """Usage: %s -s source-dir -d destination-dir [-p pattern] | ||
Options | ||
-s SOURCE directory that contains the files | ||
-d DESTINATION target directory | ||
-p PATTERN directory pattern, uses Python's date.strftime format""" % sys.argv[0] | ||
|
||
if source == None or destination == None or show_help: | ||
help() | ||
sys.exit(1) | ||
|
||
|
||
files = {} | ||
|
||
print source | ||
|
||
for file in os.listdir(source): | ||
|
||
fullname = os.path.join(source, file) | ||
|
||
if not os.path.isfile(fullname): | ||
continue | ||
|
||
try: | ||
mtime = os.path.getmtime(fullname) | ||
dir = time.strftime(pattern, time.localtime(mtime)) | ||
|
||
group = files.get(dir, []) | ||
group.append(file) | ||
files[dir] = group | ||
|
||
except: | ||
print "ngaco" | ||
|
||
for dir in files: | ||
print "mkdir %s" % os.path.join(destination, dir) | ||
|
||
for file in files[dir]: | ||
print "mv \"%s\" \"%s\"" % (os.path.join(source, file), os.path.join(destination, dir, file)) | ||
|
||
|