-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovetoarchive.py
executable file
·89 lines (78 loc) · 3.4 KB
/
movetoarchive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/opt/local/bin/python
# movetoarchive - quickly archive files in a year and month folder structure.
# The folder structure has year folders and year/year-month subfolders. Folders
# are created as needed, files are moved based on their date of last change.
# (c) 2013 by Martin Demling
# License: GPL3
# https://github.com/0x6d64/movetoarchive
import sys
import os
import time
import shutil
import argparse
#---------------------------------
def PrintSeparator():
if sys.stdout.isatty():
rows, columns = os.popen('stty size', 'r').read().split()
TerminalRows = int(rows)
TerminalColumns = int(columns)
else:
TerminalRows = 0
TerminalColumns = 30
print(TerminalColumns * "-")
#---------------------------------
# PrintSeparator()
# print "debug-info:"
# print "in dir: " + os.getcwd()
#
# for index, arg in enumerate(sys.argv):
# print str(index) + ". arg: " + str(arg)
# PrintSeparator()
#---------------------------------
parser = argparse.ArgumentParser()
group1 = parser.add_mutually_exclusive_group()
group1.add_argument("-m", "--month", help="create subfolders for years and months (default)", action="store_true")
group1.add_argument("-y", "--year", help="create subfolders for years", action="store_true")
group2 = parser.add_mutually_exclusive_group()
group2.add_argument("-v", "--verbose", help="verbose output (default)", action="store_true")
group2.add_argument("-q", "--quiet", help="quiet output", action="store_true")
args= parser.parse_args()
if not args.month and not args.year:
args.month = True
FolderMode = 0755
CurrentDir = os.getcwd() #get the directory where the script is run from
FileCount = 0 #only for stats
CreateCount = 0
for FilePath in os.listdir(CurrentDir): #handle all files in current dir
if os.path.isfile(FilePath): #deal only with files, not folders
if args.verbose:
print("handling file %s") % FilePath
TimeOfLastChange = time.localtime(os.path.getmtime(FilePath))
YearString = str(TimeOfLastChange.tm_year)
MonthString = str(TimeOfLastChange.tm_mon)
if len(MonthString) == 1:
MonthString = "0" + MonthString # pad one digit numbers with zero
YearDirAbspath = CurrentDir + "/" + YearString
MonthDirAbspath = YearDirAbspath + "/" + YearString + "-" + MonthString # dir in format "2013-9"
if not os.path.isdir(YearDirAbspath):
if args.verbose:
print("creating folder ./%s") % YearString
os.mkdir(YearDirAbspath, FolderMode)
CreateCount += 1
if not args.year:
if not os.path.isdir(MonthDirAbspath):
if args.verbose:
print("creating folder ./%s/%s") % (YearString, MonthString)
os.mkdir(MonthDirAbspath, FolderMode)
CreateCount += 1
TempAttributes = os.stat(FilePath) #save file attributes in temp var
if args.month:
NewDirAbsPath = MonthDirAbspath
else:
NewDirAbsPath = YearDirAbspath
shutil.move(FilePath, NewDirAbsPath)
NewFileAbspath = NewDirAbsPath + "/" + os.path.basename(FilePath)
os.utime(NewFileAbspath, (TempAttributes.st_atime, TempAttributes.st_mtime)) #restore file attributes
FileCount += 1
if not args.quiet:
print "%d files moved and %d directories created" % (FileCount, CreateCount)