Skip to content

Commit 80a19a9

Browse files
committed
Fix some problems based on reviewer's comments.
1. Add some common formats of video and picture. 2. Calculate the number dynamically based on the amount of files to be renamed. 3. Fix the bugs of optional parameters.
1 parent 6ae833a commit 80a19a9

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

commands/system/rename-vedios-pictures.py commands/system/rename-videos-pictures.py

+29-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
# Optional parameters:
99
# @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 }
1113

1214
# Documentation:
1315
# @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:
2426
def __init__(self, root_directory):
2527
self.root_directory = os.path.expanduser(root_directory)
2628
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')
2931

3032
def rename_files(self, file_type='all', dry_run=False):
3133
current_date = datetime.datetime.now().strftime("%m%d")
@@ -39,13 +41,13 @@ def rename_files(self, file_type='all', dry_run=False):
3941
def _is_already_renamed(self, filename, current_date):
4042
"""Check if the file has been named according to the target format"""
4143
base_name = os.path.splitext(filename)[0]
42-
pattern = re.compile(f'^.*-{current_date}-\\d{{2}}$')
44+
pattern = re.compile(f'^.*-{current_date}-\\d+$')
4345
return bool(pattern.match(base_name))
4446

4547
def _get_max_sequence_number(self, directory, current_date):
4648
"""Get the largest serial number existing in the current directory"""
4749
max_seq = 0
48-
pattern = re.compile(f'^.*-{current_date}-(\\d{{2}}).*$')
50+
pattern = re.compile(f'^.*-{current_date}-(\\d+)$')
4951

5052
for entry in os.listdir(directory):
5153
match = pattern.match(os.path.splitext(entry)[0])
@@ -73,11 +75,13 @@ def _process_directory(self, directory, current_date, file_type, dry_run):
7375
if files_to_rename:
7476
# Get the largest serial number existing in the current directory
7577
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))
7680

7781
# Rename collected files
7882
for entry in sorted(files_to_rename): # Sort to ensure consistent rename order
7983
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()}"
8185
new_path = os.path.join(directory, new_name)
8286

8387
if dry_run:
@@ -99,16 +103,25 @@ def _get_supported_extensions(self, file_type):
99103
return self.video_extensions + self.image_extensions
100104

101105
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)
112125

113126
if __name__ == '__main__':
114127
main()

0 commit comments

Comments
 (0)