import os import sys import re def rename_files(directory): for root, dirs, files in os.walk(directory): for filename in files: original_filename = filename new_filename = filename # Step 1: Replace '_2F' with '%2F' if present if '_2F' in new_filename: new_filename = new_filename.replace('_2F', '%2F') # Step 2: Remove any characters between 'sound=' and 'files.catbox.moe' if 'files.catbox.moe' is present if 'sound=' in new_filename and 'files.catbox.moe' in new_filename: # Use regex to remove characters between 'sound=' and 'files.catbox.moe' new_filename = re.sub(r'(sound=)[^f]*?(files\.catbox\.moe)', r'\1\2', new_filename) # Proceed only if the filename has changed if new_filename != original_filename: original_path = os.path.join(root, original_filename) new_path = os.path.join(root, new_filename) if not os.path.exists(new_path): # Rename the file print(f"Renaming '{original_path}' to '{new_path}'") os.rename(original_path, new_path) else: # Delete the original file if the new filename already exists print(f"Deleting '{original_path}' because '{new_filename}' already exists.") os.remove(original_path) else: continue if __name__ == '__main__': if len(sys.argv) != 2: print("Usage: python3 rename_script.py ") sys.exit(1) directory = sys.argv[1] if not os.path.isdir(directory): print(f"The provided path '{directory}' is not a directory.") sys.exit(1) rename_files(directory)