feat: update script to support multiple file and directory inputs for suffix stripping

This commit is contained in:
tigerenwork 2026-04-19 01:27:47 +08:00
parent 9683c94e6a
commit 657fed1ef0
1 changed files with 23 additions and 17 deletions

View File

@ -20,41 +20,47 @@ def strip_duplicate_suffix(filename):
def main():
# 1. Check arguments
if len(sys.argv) < 2:
print("Usage: python strip_suffix.py <path_to_folder> [--apply]")
print("Usage: python strip_suffix.py <path_to_file_or_folder> [more_files...] [--apply]")
print(" By default, runs in dry-run mode (preview only).")
print(" Add --apply to actually rename the files.")
return
# 2. Parse arguments
raw_input = sys.argv[1].strip(' "\'')
input_dir = os.path.abspath(raw_input)
apply_mode = "--apply" in sys.argv
args = [arg.strip(' "\'') for arg in sys.argv[1:] if arg != "--apply"]
if not os.path.isdir(input_dir):
print(f"Error: '{input_dir}' is not a directory.")
target_files = []
for arg in args:
abs_arg = os.path.abspath(arg)
if os.path.isfile(abs_arg):
target_files.append(abs_arg)
elif os.path.isdir(abs_arg):
# Find all files in the directory
for f in os.listdir(abs_arg):
full_path = os.path.join(abs_arg, f)
if os.path.isfile(full_path):
target_files.append(full_path)
else:
print(f"Warning: '{arg}' does not exist.")
if not target_files:
print("Error: No valid files found to process.")
return
# 3. Find all files in the directory
files = os.listdir(input_dir)
files.sort()
rename_count = 0
skip_count = 0
print(f"Scanning: {input_dir}")
print(f"Mode: {'APPLY (will rename files)' if apply_mode else 'DRY-RUN (preview only)'}")
print("-" * 50)
for filename in files:
# Only process files, not directories
filepath = os.path.join(input_dir, filename)
if not os.path.isfile(filepath):
continue
for filepath in target_files:
directory = os.path.dirname(filepath)
filename = os.path.basename(filepath)
clean_name = strip_duplicate_suffix(filename)
if clean_name != filename:
clean_path = os.path.join(input_dir, clean_name)
clean_path = os.path.join(directory, clean_name)
# Check if the target name already exists
if os.path.exists(clean_path):