feat: update script to support multiple file and directory inputs for suffix stripping
This commit is contained in:
parent
9683c94e6a
commit
657fed1ef0
|
|
@ -20,41 +20,47 @@ def strip_duplicate_suffix(filename):
|
||||||
def main():
|
def main():
|
||||||
# 1. Check arguments
|
# 1. Check arguments
|
||||||
if len(sys.argv) < 2:
|
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(" By default, runs in dry-run mode (preview only).")
|
||||||
print(" Add --apply to actually rename the files.")
|
print(" Add --apply to actually rename the files.")
|
||||||
return
|
return
|
||||||
|
|
||||||
# 2. Parse arguments
|
# 2. Parse arguments
|
||||||
raw_input = sys.argv[1].strip(' "\'')
|
|
||||||
input_dir = os.path.abspath(raw_input)
|
|
||||||
apply_mode = "--apply" in sys.argv
|
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):
|
target_files = []
|
||||||
print(f"Error: '{input_dir}' is not a directory.")
|
|
||||||
|
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
|
return
|
||||||
|
|
||||||
# 3. Find all files in the directory
|
|
||||||
files = os.listdir(input_dir)
|
|
||||||
files.sort()
|
|
||||||
|
|
||||||
rename_count = 0
|
rename_count = 0
|
||||||
skip_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(f"Mode: {'APPLY (will rename files)' if apply_mode else 'DRY-RUN (preview only)'}")
|
||||||
print("-" * 50)
|
print("-" * 50)
|
||||||
|
|
||||||
for filename in files:
|
for filepath in target_files:
|
||||||
# Only process files, not directories
|
directory = os.path.dirname(filepath)
|
||||||
filepath = os.path.join(input_dir, filename)
|
filename = os.path.basename(filepath)
|
||||||
if not os.path.isfile(filepath):
|
|
||||||
continue
|
|
||||||
|
|
||||||
clean_name = strip_duplicate_suffix(filename)
|
clean_name = strip_duplicate_suffix(filename)
|
||||||
|
|
||||||
if clean_name != 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
|
# Check if the target name already exists
|
||||||
if os.path.exists(clean_path):
|
if os.path.exists(clean_path):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue