Advanced Asset Organizer Python GUI

👤 Sharing: AI
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import os
import shutil
import datetime

class AssetOrganizer:
    def __init__(self, master):
        self.master = master
        master.title("Advanced Asset Organizer")

        self.source_dir = tk.StringVar()
        self.destination_dir = tk.StringVar()

        # Source Directory
        ttk.Label(master, text="Source Directory:").grid(row=0, column=0, sticky=tk.W, padx=5, pady=5)
        ttk.Entry(master, textvariable=self.source_dir, width=50).grid(row=0, column=1, sticky=tk.W, padx=5, pady=5)
        ttk.Button(master, text="Browse", command=self.browse_source).grid(row=0, column=2, sticky=tk.W, padx=5, pady=5)

        # Destination Directory
        ttk.Label(master, text="Destination Directory:").grid(row=1, column=0, sticky=tk.W, padx=5, pady=5)
        ttk.Entry(master, textvariable=self.destination_dir, width=50).grid(row=1, column=1, sticky=tk.W, padx=5, pady=5)
        ttk.Button(master, text="Browse", command=self.browse_destination).grid(row=1, column=2, sticky=tk.W, padx=5, pady=5)

        # Organize By
        ttk.Label(master, text="Organize By:").grid(row=2, column=0, sticky=tk.W, padx=5, pady=5)
        self.organize_by = tk.StringVar(value="date") # Default value
        ttk.Radiobutton(master, text="Date", variable=self.organize_by, value="date").grid(row=2, column=1, sticky=tk.W, padx=5, pady=5)
        ttk.Radiobutton(master, text="File Type", variable=self.organize_by, value="filetype").grid(row=2, column=2, sticky=tk.W, padx=5, pady=5)

        # Organize Button
        ttk.Button(master, text="Organize", command=self.organize_files).grid(row=3, column=1, pady=10)

        # Status Label
        self.status_label = ttk.Label(master, text="Ready")
        self.status_label.grid(row=4, column=0, columnspan=3, sticky=tk.W, padx=5, pady=5)

        #Details Button
        ttk.Button(master, text="Details", command=self.show_details).grid(row=5, column=1, pady=10)


    def browse_source(self):
        dir = filedialog.askdirectory()
        self.source_dir.set(dir)

    def browse_destination(self):
        dir = filedialog.askdirectory()
        self.destination_dir.set(dir)

    def organize_files(self):
        source = self.source_dir.get()
        destination = self.destination_dir.get()
        organize_by = self.organize_by.get()

        if not source or not destination:
            messagebox.showerror("Error", "Please select both source and destination directories.")
            return

        try:
            self.status_label.config(text="Organizing...")
            self.master.update_idletasks()
            self.perform_organization(source, destination, organize_by)
            self.status_label.config(text="Organization Complete!")
            messagebox.showinfo("Success", "Files organized successfully!")
        except Exception as e:
            messagebox.showerror("Error", str(e))
            self.status_label.config(text="Error")

    def perform_organization(self, source, destination, organize_by):
        for filename in os.listdir(source):
            source_path = os.path.join(source, filename)
            if os.path.isfile(source_path):
                if organize_by == "date":
                    #Organize by modification date
                    mod_time = os.path.getmtime(source_path)
                    date_obj = datetime.datetime.fromtimestamp(mod_time)
                    date_dir = date_obj.strftime("%Y-%m-%d")
                    dest_dir = os.path.join(destination, date_dir)
                elif organize_by == "filetype":
                    #Organize by file type
                    file_ext = filename.split(".")[-1].lower() #Extract file extension
                    dest_dir = os.path.join(destination, file_ext)
                else:
                    raise ValueError("Invalid organization criteria.")

                #Create directory if it doesn't exist
                os.makedirs(dest_dir, exist_ok=True)
                dest_path = os.path.join(dest_dir, filename)

                #Move the file
                try:
                  shutil.move(source_path, dest_path)
                except Exception as e:
                  print(f"Error moving {filename}: {e}")

    def show_details(self):
      details_window = tk.Toplevel(self.master)
      details_window.title("Program Details")
      details_text = """Advanced Asset Organizer: This program helps you organize your files efficiently. You can choose to organize files by their modification date (year-month-day) or by their file type (e.g., 'pdf', 'jpg', 'txt'). Simply select the source directory containing the files you want to organize, and the destination directory where you want the organized files to be placed.  Choose your organizing criteria and click 'Organize'. Error messages will appear if something goes wrong.  This software could be used by photographers, video editors, or anyone that needs to organize files.
      """

      details_label = tk.Label(details_window, text=details_text, justify=tk.LEFT, padx=10, pady=10)
      details_label.pack()

root = tk.Tk()
organizer = AssetOrganizer(root)
root.mainloop()
👁️ Viewed: 7

Comments