@@ -28,11 +28,9 @@ def moveto(file, from_folder, to_folder):
28
28
os .rename (from_file , to_file )
29
29
30
30
31
- def classify (formats , output ):
31
+ def classify (formats , output , directory ):
32
32
print ("Scanning Files" )
33
33
34
- directory = getcwd ()
35
-
36
34
for file in os .listdir (directory ):
37
35
filename , file_ext = os .path .splitext (file )
38
36
file_ext = file_ext .lower ()
@@ -49,15 +47,15 @@ def classify(formats, output):
49
47
print ("Done!" )
50
48
51
49
52
- def classify_by_date (date_format , output_dir ):
50
+ def classify_by_date (date_format , output , directory ):
53
51
print ("Scanning Files" )
54
52
55
- directory = getcwd ()
56
53
files = [x for x in os .listdir (directory ) if not x .startswith ('.' )]
57
- creation_dates = map (lambda x : (x , arrow .get (os .path .getctime (x ))), files )
54
+ creation_dates = map (lambda x : (x , arrow .get (os .path .getctime (os . path . join ( directory , x ) ))), files )
58
55
59
56
for file , creation_date in creation_dates :
60
57
folder = creation_date .format (date_format )
58
+ folder = os .path .join (output , folder )
61
59
moveto (file , directory , folder )
62
60
63
61
print ("Done!" )
@@ -88,6 +86,9 @@ def main():
88
86
parser .add_argument ("-o" , "--output" , type = str ,
89
87
help = "Main directory to put organized folders" )
90
88
89
+ parser .add_argument ("-d" , "--directory" , type = str ,
90
+ help = "The directory whose files to classify" )
91
+
91
92
parser .add_argument ("-dt" , "--date" , action = 'store_true' ,
92
93
help = "Organize files by creation date" )
93
94
@@ -120,9 +121,18 @@ def main():
120
121
else :
121
122
output = _format_arg (args .output )
122
123
124
+ if args .directory is None :
125
+ directory = getcwd ()
126
+ else :
127
+ directory = _format_arg (args .directory )
128
+ if args .output is None :
129
+ ''' if -d arg given without the -o arg, keeping the files of -d
130
+ in the -d path only after classifying '''
131
+ output = directory
132
+
123
133
if args .date :
124
- classify_by_date ('DD-MM-YYYY' , output )
134
+ classify_by_date ('DD-MM-YYYY' , output , directory )
125
135
else :
126
- classify (formats , output )
136
+ classify (formats , output , directory )
127
137
128
138
sys .exit ()
0 commit comments