Last active
July 31, 2024 18:25
-
-
Save DarkCoder28/cc7351e88984b1e6ef571c4a753b4310 to your computer and use it in GitHub Desktop.
This is a python script that fixes borked folders. These folders can be the result of a stupid old system or a borked automation script or whathaveyou... The way this works is by removing the spaces that some scenarios can leave at the end of folder names. This script will rename those folders to the correct name and ignore all other folders and…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import tkinter as tk | |
from tkinter import filedialog, messagebox | |
import threading | |
def rename_directories(search_path): | |
work = [] | |
for root, dirs, files in os.walk(search_path): | |
for directory in dirs: | |
old_name = directory + os.sep | |
new_name = directory.strip() + os.sep | |
full_path = os.path.join(root, old_name) | |
if os.path.exists(full_path): | |
try: | |
if old_name != new_name: | |
os.rename(full_path, os.path.join(root, new_name)) | |
print(f"Renamed '{old_name}' to '{new_name}'") | |
work.append(f"Renamed '{old_name}' to '{new_name}'") | |
else: | |
pass | |
except Exception as e: | |
print(f"Error renaming '{old_name}': {e}") | |
else: | |
print(f"Directory '{old_name}' does not exist.") | |
messagebox.showinfo("Completion", "Folder renaming complete!") | |
messagebox.showinfo("Work", '\n'.join(work)) | |
def select_folder(): | |
folder_path = filedialog.askdirectory() | |
if folder_path: | |
current_folder_label.config(text=f"Current Folder: {folder_path}") | |
# Run the renaming process in a background thread | |
threading.Thread(target=rename_directories, args=(folder_path,)).start() | |
messagebox.showinfo("Completion", "Folder renaming started in the background!") | |
# Create the main window | |
root = tk.Tk() | |
root.title("Folder Name Fixer") | |
# Center the main window | |
window_width = 256 | |
window_height = 128 | |
screen_width = root.winfo_screenwidth() | |
screen_height = root.winfo_screenheight() | |
x_position = (screen_width - window_width) // 2 | |
y_position = (screen_height - window_height) // 2 | |
root.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}") | |
# Add widgets | |
select_folder_button = tk.Button(root, text="Select Folder", command=select_folder) | |
current_folder_label = tk.Label(root, text="Current Folder: Not selected") | |
# Pack widgets | |
select_folder_button.pack(pady=10) | |
current_folder_label.pack() | |
# Start the GUI event loop | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment