-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile_Organization.py
More file actions
56 lines (44 loc) · 2.15 KB
/
Copy pathFile_Organization.py
File metadata and controls
56 lines (44 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import shutil
from tkinter import Tk, Button, Label, Entry, StringVar, OptionMenu
def organize_files():
folder_path = folder_path_var.get()
organize_by = organize_by_var.get()
if not os.path.exists(folder_path):
info_label.config(text="Invalid folder path.")
return
alphabet_folders = {}
for char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
alphabet_folders[char] = os.path.join(folder_path, char)
if not os.path.exists(alphabet_folders[char]):
os.makedirs(alphabet_folders[char])
for file_name in os.listdir(folder_path):
if os.path.isfile(os.path.join(folder_path, file_name)):
if organize_by == "Alphabetical":
first_char = file_name[0].upper()
if first_char in alphabet_folders:
shutil.move(os.path.join(folder_path, file_name), alphabet_folders[first_char])
elif organize_by == "Date":
modified_time = os.path.getmtime(os.path.join(folder_path, file_name))
modified_date = time.strftime('%Y-%m-%d', time.localtime(modified_time))
year = modified_date.split('-')[0]
month = modified_date.split('-')[1]
destination_folder = os.path.join(alphabet_folders[year], month)
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
shutil.move(os.path.join(folder_path, file_name), destination_folder)
info_label.config(text="Files organized successfully.")
# GUI
root = Tk()
root.title("File Organizer")
folder_path_var = StringVar()
organize_by_var = StringVar()
Label(root, text="Folder Path:").grid(row=0, column=0)
Entry(root, textvariable=folder_path_var, width=50).grid(row=0, column=1)
Label(root, text="Organize By:").grid(row=1, column=0)
OptionMenu(root, organize_by_var, "Alphabetical", "Date").grid(row=1, column=1)
organize_button = Button(root, text="Organize Files", command=organize_files)
organize_button.grid(row=2, columnspan=2)
info_label = Label(root, text="")
info_label.grid(row=3, columnspan=2)
root.mainloop()