Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- def replace_string_in_filenames_and_folders(folder_path, target_string):
- try:
- # Traverse directories in a bottom-up manner
- for root, dirs, files in os.walk(folder_path, topdown=False):
- for filename in files:
- if target_string in filename:
- new_filename = filename.replace(target_string, "")
- src = os.path.join(root, filename)
- dst = os.path.join(root, new_filename)
- os.rename(src, dst)
- if filename.endswith(".cue"):
- cue_file_path = os.path.join(root, filename)
- with open(cue_file_path, 'r') as cue_file:
- cue_file_content = cue_file.read()
- new_cue_file_content = cue_file_content.replace(target_string, "")
- with open(cue_file_path, 'w') as cue_file:
- cue_file.write(new_cue_file_content)
- for dirname in dirs:
- if target_string in dirname:
- new_dirname = dirname.replace(target_string, "")
- src = os.path.join(root, dirname)
- dst = os.path.join(root, new_dirname)
- os.rename(src, dst)
- print("String replacement in filenames, folders, and .cue files completed successfully.")
- except OSError as e:
- print(f"Error occurred: {e}")
- if __name__ == "__main__":
- user_string = input("Enter the string you want to replace: ")
- folder_directory = input("Enter the folder directory: ")
- replace_string_in_filenames_and_folders(folder_directory, user_string)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement