File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import os
2+
3+ from mtlogger import logger
4+ from natsort import natsorted
5+
6+ def process_items (parent_dir , items , name_pattern ):
7+ items = natsorted (items )
8+ num_digits = len (str (len (items )))
9+
10+ for index , old_name in enumerate (items , start = 1 ):
11+ old_path = os .path .join (parent_dir , old_name )
12+ extension = os .path .splitext (old_name )[1 ] if os .path .isfile (old_path ) else ''
13+ sequence = str (index ).zfill (num_digits )
14+ new_name = f'{ name_pattern .replace ("$" , sequence )} { extension } '
15+ new_path = os .path .join (parent_dir , new_name )
16+
17+ os .rename (old_path , new_path )
18+ logger .log (f'Renamed "{ old_name } " to "{ new_name } ".' )
19+ logger .log (f'\n Finished processing "{ parent_dir } ".' )
Original file line number Diff line number Diff line change 11import os
22
3+ from _common import process_items
34from mtlogger import logger
4- from natsort import natsorted
55
66def main ():
77 parent_dir = input ('Enter the path to the folder containing your files:\n ' ).strip (' "\' ' )
8+ print ()
89
9- items = [f for f in os .listdir (parent_dir ) if os .path .isfile (os .path .join (parent_dir , f )) and not f .startswith ('.' )]
10- items = natsorted (items )
11-
12- total_items = len (items )
13- num_digits = len (str (total_items ))
10+ name_pattern = input ('Enter the name pattern pattern (optional; use $ for number interpolation):\n ' ).strip () or '$'
11+ print ()
1412
15- for index , old_name in enumerate (items , start = 1 ):
16- file_extension = os .path .splitext (old_name )[1 ]
17- new_name = f'{ str (index ).zfill (num_digits )} { file_extension } '
18- old_path = os .path .join (parent_dir , old_name )
19- new_path = os .path .join (parent_dir , new_name )
20-
21- os .rename (old_path , new_path )
22- logger .log (f'Renamed "{ old_name } " to "{ new_name } ".' )
23- logger .log (f'\n Finished processing "{ parent_dir } ".' )
13+ items = [f for f in os .listdir (parent_dir ) if os .path .isfile (os .path .join (parent_dir , f )) and not f .startswith ('.' )]
14+ process_items (parent_dir , items , name_pattern )
2415
2516if __name__ == '__main__' :
2617 try :
Original file line number Diff line number Diff line change 11import os
22
3+ from _common import process_items
34from mtlogger import logger
4- from natsort import natsorted
55
66def main ():
77 parent_dir = input ('Enter the path to the folder containing your folders:\n ' ).strip (' "\' ' )
8+ print ()
89
9- items = [f for f in os .listdir (parent_dir ) if os .path .isdir (os .path .join (parent_dir , f )) and not f .startswith ('.' )]
10- items = natsorted (items )
11-
12- total_items = len (items )
13- num_digits = len (str (total_items ))
10+ name_pattern = input ('Enter the name pattern pattern (optional; use $ for number interpolation):\n ' ).strip () or '$'
11+ print ()
1412
15- for index , old_name in enumerate (items , start = 1 ):
16- new_name = f'{ str (index ).zfill (num_digits )} '
17- old_path = os .path .join (parent_dir , old_name )
18- new_path = os .path .join (parent_dir , new_name )
19-
20- os .rename (old_path , new_path )
21- logger .log (f'Renamed "{ old_name } " to "{ new_name } ".' )
22- logger .log (f'\n Finished processing "{ parent_dir } ".' )
13+ items = [f for f in os .listdir (parent_dir ) if os .path .isdir (os .path .join (parent_dir , f )) and not f .startswith ('.' )]
14+ process_items (parent_dir , items , name_pattern )
2315
2416if __name__ == '__main__' :
2517 try :
You can’t perform that action at this time.
0 commit comments