-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileCleaner.py
More file actions
116 lines (92 loc) · 4.19 KB
/
FileCleaner.py
File metadata and controls
116 lines (92 loc) · 4.19 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
import os
import shutil
from mutagen import File
import tkinter as tk
from tkinter import filedialog, messagebox
def run_script():
folder_path = folder_entry.get()
badwords_file = words_entry.get()
try:
min_duration = int(duration_entry.get())
except ValueError:
messagebox.showerror("Error", "Duration must be a number.")
return
check_short_audio = var_short_audio.get()
if not folder_path or not os.path.isdir(folder_path):
messagebox.showerror("Error", "Invalid folder path.")
return
if not badwords_file or not os.path.isfile(badwords_file):
messagebox.showerror("Error", "Invalid bad words file.")
return
# Container folder on Desktop
desktop = os.path.join(os.path.expanduser("~"), "Desktop")
container_folder = os.path.join(desktop, "FileCleanerOutput")
os.makedirs(container_folder, exist_ok=True)
# Destination folders inside container
dump1 = os.path.join(container_folder, "Dump_1")
dump2 = os.path.join(container_folder, "Dump_2")
dump3 = os.path.join(container_folder, "Dump_3")
log_folder = os.path.join(container_folder, "Log")
for folder in [dump1, dump2, dump3, log_folder]:
os.makedirs(folder, exist_ok=True)
log_file = os.path.join(log_folder, "moved_log.txt")
# Load bad words
with open(badwords_file, "r", encoding="utf-8") as f:
bad_words = [line.strip().lower() for line in f if line.strip()]
# Collect files
all_files = []
for root, dirs, files in os.walk(folder_path):
for filename in files:
all_files.append((root, filename))
total_files = len(all_files)
moved_log = []
for i, (root, filename) in enumerate(all_files, start=1):
filepath = os.path.join(root, filename)
lower_name = filename.lower()
badword_match = any(word in lower_name for word in bad_words)
short_audio_match = False
if check_short_audio and lower_name.endswith((".mp3", ".wav", ".flac", ".ogg")):
try:
audio = File(filepath)
if audio.info.length < min_duration:
short_audio_match = True
except:
pass
if badword_match and short_audio_match:
dst_path = os.path.join(dump3, filename)
elif badword_match:
dst_path = os.path.join(dump1, filename)
elif short_audio_match:
dst_path = os.path.join(dump2, filename)
else:
continue
shutil.move(filepath, dst_path)
moved_log.append((filepath, dst_path))
print(f"Moved: {filename} -> {dst_path}")
# Write log
with open(log_file, "w", encoding="utf-8") as f:
if moved_log:
for src, dst in moved_log:
f.write(f"Moved from: {src}\n to: {dst}\n\n")
else:
f.write("No files matched the criteria.\n")
messagebox.showinfo("Done", f"Processed {total_files} files.\nLog saved to: {log_file}")
# --- GUI ---
root = tk.Tk()
root.title("File Cleaner")
tk.Label(root, text="Folder to Scan:").grid(row=0, column=0, sticky="e")
folder_entry = tk.Entry(root, width=50)
folder_entry.grid(row=0, column=1)
tk.Button(root, text="Browse", command=lambda: folder_entry.delete(0, tk.END) or folder_entry.insert(0, filedialog.askdirectory())).grid(row=0, column=2)
tk.Label(root, text="Bad Words File:").grid(row=1, column=0, sticky="e")
words_entry = tk.Entry(root, width=50)
words_entry.grid(row=1, column=1)
tk.Button(root, text="Browse", command=lambda: words_entry.delete(0, tk.END) or words_entry.insert(0, filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")]))).grid(row=1, column=2)
var_short_audio = tk.BooleanVar(value=True)
tk.Checkbutton(root, text="Check short audio", variable=var_short_audio).grid(row=2, column=1, sticky="w")
tk.Label(root, text="Duration (seconds):").grid(row=3, column=0, sticky="e")
duration_entry = tk.Entry(root)
duration_entry.insert(0, "20")
duration_entry.grid(row=3, column=1, sticky="w")
tk.Button(root, text="Run", command=run_script).grid(row=4, column=1, pady=10)
root.mainloop()