forked from itsmeshibintmz/Sort-Files-in-Folders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sort Files in Folders.py
67 lines (59 loc) · 3.12 KB
/
Sort Files in Folders.py
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
import os
import shutil
from datetime import datetime, timedelta
# Define the path to the directory to organize
path = "C:/Users/micah/Downloads/"
# Define the names of the folders to create and their associated file extensions
folders = {
"Documents": {".doc", ".docx", ".txt", ".md", ".opml", ".tex", ".bib"},
"Presentations": {".ppt", ".pptx", ".key", ".odp"},
"Spreadsheets": {".xls", ".xlsx", ".csv"},
"Images": {".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".webp", ".svg"},
"Vector Graphics": {".ai", ".eps"},
"pdfs": {".pdf"},
"Raster Graphics": {".psd", ".raw", ".cr2", ".nef", ".orf", ".sr2"},
"Video": {".avi", ".mp4", ".mov", ".wmv", ".mkv", ".flv", ".webm", ".mpg", ".mpeg", ".3gp"},
"Audio": {".mp3", ".wav", ".m4a", ".aac", ".ogg", ".flac", ".wma"},
"Code": {".py", ".ipynb", ".java", ".cpp", ".c", ".h", ".cs", ".xml", ".json", ".yaml", ".yml", ".sql", ".rb", ".pl", ".sh", ".bat", ".cmd", ".ps1", ".dockerfile", ".fig", ".js"},
"Web Files": {".html", ".css", ".js", ".php"},
"Database": {".sqlite", ".db", ".sql", ".kdbx"},
"Executable Files": {".exe", ".msi", ".deb", ".dmg", ".appimage", ".sh", ".apk", ".xpi"},
"System Files": {".ini", ".cfg", ".plist", ".log", ".env", ".desktop", ".folder", ".flatpakref"},
"Compressed Files": {".zip", ".tar", ".gz", ".7z", ".rar", ".deb", ".rpm"},
"Disk Images": {".iso", ".img", ".vmdk"},
"Other": {".tid", ".vcf"},
"Miscellaneous": set(),
"Old_Folders": set(),
"Meta": {" "}
}
# Create the folders defined in the `folders` dictionary
for folder_name, extensions in folders.items():
folder_path = os.path.join(path, folder_name)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# Set the time threshold to 15 days ago
threshold = timedelta(days=15)
# Loop over all files and directories in the path
for file in os.scandir(path):
file_path = file.path
file_modification_time = datetime.fromtimestamp(file.stat().st_mtime)
if file_modification_time > threshold:
continue
# Check if the file is older than the threshold
if file_modification_time <= threshold:
# Move the file to the appropriate folder
folder_name = None
for name, extensions in folders.items():
if file.name.endswith(extensions):
folder_name = name
break
if folder_name is None:
folder_name = "Miscellaneous"
shutil.move(file_path, os.path.join(path, folder_name, file.name))
# Check if the file is a directory and not in the 'folders' dictionary
elif file.is_directory() and file.name not in folders:
# Move the directory to the Old_Folders folder
shutil.move(file_path, os.path.join(path, "Old_Folders", file.name))
# Move the most recently modified file in the Old_Folders directory to the Miscellaneous directory
most_recent_file = max(os.listdir(os.path.join(path, "Old_Folders")), key=os.path.getmtime)
shutil.move(os.path.join(path, "Old_Folders", most_recent_file), os.path.join(path, "Miscellaneous", most_recent_file))