quick program to delete files on desktop or another folder that you no longer need import datetime import tkinter as tk from tkinter import filedialog, messagebox from pathlib import Path
def browse_folder(): folder_selected = filedialog.askdirectory() folder_path.set(folder_selected)
def preview_files(): file_list.delete(0, tk.END) folder = Path(folder_path.get())
try:
target_date = datetime.datetime.strptime(date_entry.get(), "%Y-%m-%d")
except ValueError:
messagebox.showerror("Invalid Date", "Use YYYY-MM-DD format.")
return
if not folder.exists():
messagebox.showerror("Error", "Folder does not exist.")
return
for file_path in folder.iterdir():
if file_path.is_file():
file_mtime = datetime.datetime.fromtimestamp(file_path.stat().st_mtime)
if condition.get() == "Before" and file_mtime < target_date:
file_list.insert(tk.END, file_path.name)
elif condition.get() == "After" and file_mtime > target_date:
file_list.insert(tk.END, file_path.name)
def execute_cleanup(): folder = Path(folder_path.get())
try:
target_date = datetime.datetime.strptime(date_entry.get(), "%Y-%m-%d")
except ValueError:
messagebox.showerror("Invalid Date", "Use YYYY-MM-DD format.")
return
confirm = messagebox.askyesno(
"Confirm Deletion",
"This will permanently delete the listed files. This cannot be undone. Continue?"
)
if not confirm:
return
deleted_files = []
for file_path in folder.iterdir():
if file_path.is_file():
file_mtime = datetime.datetime.fromtimestamp(file_path.stat().st_mtime)
if (condition.get() == "Before" and file_mtime < target_date) or \
(condition.get() == "After" and file_mtime > target_date):
file_path.unlink()
deleted_files.append(file_path.name)
messagebox.showinfo("Complete", f"{len(deleted_files)} files permanently deleted.")
file_list.delete(0, tk.END)
root = tk.Tk() root.title("Grand Cru File Date Utility") root.geometry("600x450")
folder_path = tk.StringVar() condition = tk.StringVar(value="Before")
tk.Label(root, text="Select Folder").pack(pady=5) tk.Entry(root, textvariable=folder_path, width=60).pack() tk.Button(root, text="Browse", command=browse_folder).pack(pady=5)
tk.Label(root, text="Enter Date (YYYY-MM-DD)").pack() date_entry = tk.Entry(root) date_entry.pack()
tk.Label(root, text="Condition").pack() tk.Radiobutton(root, text="Before Date", variable=condition, value="Before").pack() tk.Radiobutton(root, text="After Date", variable=condition, value="After").pack()
tk.Button(root, text="Preview Files", command=preview_files).pack(pady=10)
file_list = tk.Listbox(root, width=80, height=10) file_list.pack()
tk.Button(root, text="Delete Files", command=execute_cleanup, bg="red", fg="white").pack(pady=10)
root.mainloop()