Paws/PythonScripts/rename_backslash.py
Aroy-Art 9fdacb5d9d
All checks were successful
/ test (push) Successful in 22s
Add: executable permissions
2024-03-22 23:40:59 +01:00

42 lines
1.6 KiB
Python
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/python3
import os
import sys
# Function to replace backslashes in filenames within a directory
def replace_backslash(directory):
# Traverse through the directory and its subdirectories
for root, _, files in os.walk(directory):
for filename in files:
# Check if the filename contains a backslash
if "\\" in filename:
# Construct the full file path
file_path = os.path.join(root, filename)
# Replace backslashes with a special character ("") in the filename
new_filename = filename.replace("\\", "")
# Construct the new file path with the modified filename
new_file_path = os.path.join(root, new_filename)
# Rename the file with the modified filename
os.rename(file_path, new_file_path)
# Print the renaming operation details
print(f"Renamed {file_path} to {new_file_path}")
# Entry point of the script
if __name__ == "__main__":
# Check if the correct number of command-line arguments is provided
if len(sys.argv) != 2:
print("Usage: python rename_backslash.py <directory_path>")
else:
directory_path = sys.argv[1] # Get the directory path from command-line argument
# Check if the specified directory exists
if not os.path.exists(directory_path):
print("Directory not found.")
else:
# Call the function to replace backslashes in filenames within the directory
replace_backslash(directory_path)