7
7
8
8
# Optional parameters:
9
9
# @raycast.icon 📂
10
- # @raycast.argument1 { "type": "text", "placeholder": "Placeholder" }
10
+ # @raycast.argument1 { "type": "text", "placeholder": "Directory's absolute path", "optional": false }
11
+ # @raycast.argument2 { "type": "text", "placeholder": "Type (video/image/all)", "optional": true }
12
+ # @raycast.argument3 { "type": "text", "placeholder": "Dry Run (true/false)", "optional": true }
11
13
12
14
# Documentation:
13
15
# @raycast.description This is a simple Python script for recursively renaming video and picture files within a directory. Type the root directory's absolute path, and it will scan all the video and picture files in it and rename them according to the folder where they are located as the format `<folder_name>-<current_date (MMDD)>-<incremental_number>`.
@@ -24,8 +26,8 @@ class RenameFilesAsDate:
24
26
def __init__ (self , root_directory ):
25
27
self .root_directory = os .path .expanduser (root_directory )
26
28
self .error_files = {}
27
- self .video_extensions = ('.mp4' , '.avi' , '.mov' , '.mkv' , '.wmv' )
28
- self .image_extensions = ('.jpg' , '.jpeg' , '.png' , '.gif' , 'webp' )
29
+ self .video_extensions = ('.mp4' , '.avi' , '.mov' , '.mkv' , '.wmv' , '.flv' , '.webm' , '.m4v' , '.3gp' )
30
+ self .image_extensions = ('.jpg' , '.jpeg' , '.png' , '.gif' , '. webp' , '.bmp' , '.tiff' , '.svg' , '.heic ' )
29
31
30
32
def rename_files (self , file_type = 'all' , dry_run = False ):
31
33
current_date = datetime .datetime .now ().strftime ("%m%d" )
@@ -39,13 +41,13 @@ def rename_files(self, file_type='all', dry_run=False):
39
41
def _is_already_renamed (self , filename , current_date ):
40
42
"""Check if the file has been named according to the target format"""
41
43
base_name = os .path .splitext (filename )[0 ]
42
- pattern = re .compile (f'^.*-{ current_date } -\\ d{{2}} $' )
44
+ pattern = re .compile (f'^.*-{ current_date } -\\ d+ $' )
43
45
return bool (pattern .match (base_name ))
44
46
45
47
def _get_max_sequence_number (self , directory , current_date ):
46
48
"""Get the largest serial number existing in the current directory"""
47
49
max_seq = 0
48
- pattern = re .compile (f'^.*-{ current_date } -(\\ d{{2}}).* $' )
50
+ pattern = re .compile (f'^.*-{ current_date } -(\\ d+) $' )
49
51
50
52
for entry in os .listdir (directory ):
51
53
match = pattern .match (os .path .splitext (entry )[0 ])
@@ -73,11 +75,13 @@ def _process_directory(self, directory, current_date, file_type, dry_run):
73
75
if files_to_rename :
74
76
# Get the largest serial number existing in the current directory
75
77
count = self ._get_max_sequence_number (directory , current_date ) + 1
78
+ total_files = len (files_to_rename )
79
+ num_digits = len (str (total_files + count - 1 ))
76
80
77
81
# Rename collected files
78
82
for entry in sorted (files_to_rename ): # Sort to ensure consistent rename order
79
83
entry_path = os .path .join (directory , entry )
80
- new_name = f"{ folder_name } -{ current_date } -{ count :02d } { os .path .splitext (entry )[1 ].lower ()} "
84
+ new_name = f"{ folder_name } -{ current_date } -{ count :0{ num_digits }d } { os .path .splitext (entry )[1 ].lower ()} "
81
85
new_path = os .path .join (directory , new_name )
82
86
83
87
if dry_run :
@@ -99,16 +103,25 @@ def _get_supported_extensions(self, file_type):
99
103
return self .video_extensions + self .image_extensions
100
104
101
105
def main ():
102
- parser = argparse .ArgumentParser (description = 'Rename video and image files with date pattern' )
103
- parser .add_argument ('directory' , help = 'Root directory to process' )
104
- parser .add_argument ('--type' , choices = ['video' , 'image' , 'all' ],
105
- default = 'all' , help = 'File type to process' )
106
- parser .add_argument ('--dry-run' , action = 'store_true' ,
107
- help = 'Show what would be done without actually renaming' )
108
-
109
- args = parser .parse_args ()
110
- renamer = RenameFilesAsDate (args .directory )
111
- renamer .rename_files (args .type , args .dry_run )
106
+ args = sys .argv [1 :]
107
+
108
+ directory = None
109
+ file_type = 'all'
110
+ dry_run = False
111
+
112
+ if len (args ) >= 1 :
113
+ directory = args [0 ]
114
+ if len (args ) >= 2 and args [1 ]:
115
+ file_type = args [1 ]
116
+ if len (args ) >= 3 and args [2 ]:
117
+ dry_run = args [2 ].lower () == 'true'
118
+
119
+ if not directory :
120
+ print ("Error: Directory argument is required." )
121
+ sys .exit (1 )
122
+
123
+ renamer = RenameFilesAsDate (directory )
124
+ renamer .rename_files (file_type , dry_run )
112
125
113
126
if __name__ == '__main__' :
114
127
main ()
0 commit comments