-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.py
More file actions
433 lines (257 loc) · 10.2 KB
/
QuickSort.py
File metadata and controls
433 lines (257 loc) · 10.2 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# ==========================================================
# QuickSort PRO - File Size Organizer
# Professional Desktop Tool
# ==========================================================
import os
import sys
import shutil
import threading
import time
import traceback
from queue import Queue, Empty
from tkinter import filedialog, messagebox
import tkinter as tk
import ttkbootstrap as tb
from ttkbootstrap.constants import *
from tkinterdnd2 import DND_FILES, TkinterDnD
# =================== APP CONFIG ===================
APP_NAME = "QuickSort - File Size Organizer"
APP_VERSION = "1.0.0"
# =================== APP ===================
app = TkinterDnD.Tk()
app.title(f"{APP_NAME} {APP_VERSION}")
app.geometry("1120x650")
tb.Style("darkly")
# =================== UTILITY ===================
def resource_path(file_name):
base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, file_name)
def log_error():
with open("error.log", "a", encoding="utf-8") as f:
f.write(traceback.format_exc() + "\n")
def show_about():
messagebox.showinfo(
f"About {APP_NAME}",
f"{APP_NAME} v{APP_VERSION}\n\n"
"Professional File Size Organizer\n\n"
"Features:\n"
"• Drag & Drop files or folders\n"
"• Folder scanning\n"
"• Sort files by size ranges\n"
"• Move or Copy files\n"
"• Pause / Stop processing\n"
"• Live progress tracking\n"
"• Detailed logging\n\n"
"Built with Python + Tkinter + ttkbootstrap\n"
"© 2026 Mate Technologies\n"
"https://matetools.gumroad.com"
)
try:
app.iconbitmap(resource_path("logo.ico"))
except:
pass
# =================== MENU ===================
menubar = tb.Menu(app)
help_menu = tb.Menu(menubar, tearoff=0)
help_menu.add_command(label="About", command=show_about)
menubar.add_cascade(label="Help", menu=help_menu)
app.config(menu=menubar)
# =================== FLAGS ===================
stop_flag = False
pause_flag = False
ui_queue = Queue()
file_list = []
output_path = tb.StringVar()
operation_mode = tb.StringVar(value="Move")
# =================== TITLE ===================
tb.Label(
app,
text=APP_NAME,
font=("Segoe UI", 24, "bold")
).pack(pady=(10, 2))
tb.Label(
app,
text="Professional File Size Organizer – Smart File Sorting",
font=("Segoe UI", 10, "italic"),
foreground="#9ca3af"
).pack(pady=(0, 10))
# =================== FRAME: FILE SELECTION ===================
frame1 = tb.Labelframe(app, text="Files & Folders", padding=10)
frame1.pack(fill="x", padx=10, pady=6)
file_frame = tb.Frame(frame1)
file_frame.pack(fill="x", pady=6)
file_listbox = tk.Listbox(file_frame, height=7, selectmode="extended")
file_listbox.pack(side="left", fill="x", expand=True)
scroll = tb.Scrollbar(file_frame, command=file_listbox.yview)
scroll.pack(side="right", fill="y")
file_listbox.config(yscrollcommand=scroll.set)
# =================== FILE FUNCTIONS ===================
def add_files():
files = filedialog.askopenfilenames(title="Select Files")
for f in files:
if f not in file_list:
file_list.append(f)
ui_queue.put(("add", f))
def add_folder():
folder = filedialog.askdirectory(title="Select Folder")
if not folder:
return
for root, dirs, files in os.walk(folder):
for name in files:
path = os.path.join(root, name)
if path not in file_list:
file_list.append(path)
ui_queue.put(("add", path))
def clear_list():
file_list.clear()
ui_queue.put(("clear", None))
def set_output_folder():
folder = filedialog.askdirectory()
if folder:
output_path.set(folder)
# =================== SIZE LOGIC ===================
def size_category(size):
if size < 1 * 1024 * 1024:
return "0-1MB"
elif size < 10 * 1024 * 1024:
return "1-10MB"
elif size < 100 * 1024 * 1024:
return "10-100MB"
elif size < 1024 * 1024 * 1024:
return "100MB-1GB"
else:
return "1GB+"
# =================== ORGANIZER ===================
def organize_files():
global stop_flag, pause_flag
stop_flag = False
pause_flag = False
extract_btn.config(state="disabled")
pause_btn.config(state="normal")
stop_btn.config(state="normal")
total = len(file_list)
if total == 0:
messagebox.showerror("Error", "No files selected.")
extract_btn.config(state="normal")
pause_btn.config(state="disabled")
stop_btn.config(state="disabled")
return
out_dir = output_path.get() or os.path.dirname(file_list[0])
ui_queue.put(("log", f"Starting processing {total} files..."))
for idx, file in enumerate(file_list, 1):
if stop_flag:
ui_queue.put(("log", "Process stopped by user."))
break
while pause_flag:
time.sleep(0.2)
try:
size = os.path.getsize(file)
folder = size_category(size)
dest_dir = os.path.join(out_dir, folder)
os.makedirs(dest_dir, exist_ok=True)
dest = os.path.join(dest_dir, os.path.basename(file))
if operation_mode.get() == "Move":
shutil.move(file, dest)
else:
shutil.copy2(file, dest)
ui_queue.put(("log", f"✔ {os.path.basename(file)} -> {folder}"))
except Exception:
log_error()
ui_queue.put(("log", f"❌ Failed: {file}"))
percent = int((idx / total) * 100)
ui_queue.put(("progress", percent))
ui_queue.put(("complete", "Sorting finished."))
# =================== CONTROL BUTTONS ===================
tb.Button(frame1, text="Add Files", command=add_files, bootstyle="success").pack(side="left", padx=4)
tb.Button(frame1, text="Add Folder", command=add_folder, bootstyle="info").pack(side="left", padx=4)
tb.Button(frame1, text="Clear List", command=clear_list, bootstyle="danger-outline").pack(side="left", padx=4)
tb.Label(frame1, text="Output Folder:", width=13).pack(side="left", padx=(12, 0))
tb.Entry(frame1, textvariable=output_path, width=40).pack(side="left", padx=6)
tb.Button(frame1, text="Browse", command=set_output_folder).pack(side="left", padx=4)
tb.Label(frame1, text="Mode:").pack(side="left", padx=(10, 2))
tb.OptionMenu(frame1, operation_mode, "Move", "Move", "Copy").pack(side="left")
extract_btn = tb.Button(frame1, text="📂 Organize", bootstyle="success")
pause_btn = tb.Button(frame1, text="⏸ Pause", bootstyle="warning-outline", state="disabled")
stop_btn = tb.Button(frame1, text="🛑 Stop", bootstyle="danger-outline", state="disabled")
extract_btn.pack(side="left", padx=6)
pause_btn.pack(side="left", padx=4)
stop_btn.pack(side="left", padx=4)
# =================== PROGRESS ===================
frame2 = tb.Labelframe(app, text="Progress", padding=8)
frame2.pack(fill="x", padx=10)
progress_var = tb.IntVar()
tb.Progressbar(
frame2,
variable=progress_var,
maximum=100,
length=500
).pack(side="left", padx=10)
status_lbl = tb.Label(frame2, text="Status: Ready")
status_lbl.pack(side="left", padx=10)
# =================== LOG ===================
frame3 = tb.Labelframe(app, text="Processing Log", padding=8)
frame3.pack(fill="both", expand=True, padx=10, pady=6)
log_text = tk.Text(frame3, height=10)
log_text.pack(side="left", fill="both", expand=True)
log_scroll = tk.Scrollbar(frame3, command=log_text.yview)
log_scroll.pack(side="right", fill="y")
log_text.config(yscrollcommand=log_scroll.set, state="disabled")
# =================== UI QUEUE ===================
def process_ui_queue():
try:
while True:
cmd, data = ui_queue.get_nowait()
if cmd == "add":
file_listbox.insert("end", data)
elif cmd == "clear":
file_listbox.delete(0, "end")
elif cmd == "progress":
progress_var.set(data)
elif cmd == "log":
log_text.config(state="normal")
log_text.insert("end", data + "\n")
log_text.see("end")
log_text.config(state="disabled")
elif cmd == "complete":
progress_var.set(100)
status_lbl.config(text=f"Status: {data}")
extract_btn.config(state="normal")
pause_btn.config(state="disabled")
stop_btn.config(state="disabled")
except Empty:
pass
app.after(100, process_ui_queue)
# =================== BUTTON COMMANDS ===================
def toggle_pause():
global pause_flag
pause_flag = not pause_flag
pause_btn.config(text="▶ Resume" if pause_flag else "⏸ Pause")
def stop_process():
global stop_flag
stop_flag = True
status_lbl.config(text="Status: Stopping...")
extract_btn.config(
command=lambda: threading.Thread(target=organize_files, daemon=True).start()
)
pause_btn.config(command=toggle_pause)
stop_btn.config(command=stop_process)
# =================== DRAG & DROP ===================
def drop(event):
files = app.tk.splitlist(event.data)
for f in files:
if os.path.isfile(f):
if f not in file_list:
file_list.append(f)
ui_queue.put(("add", f))
elif os.path.isdir(f):
for root, dirs, names in os.walk(f):
for name in names:
path = os.path.join(root, name)
if path not in file_list:
file_list.append(path)
ui_queue.put(("add", path))
file_listbox.drop_target_register(DND_FILES)
file_listbox.dnd_bind("<<Drop>>", drop)
# =================== START UI ===================
app.after(100, process_ui_queue)
app.mainloop()