-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeclutterer.py
More file actions
197 lines (173 loc) · 5.91 KB
/
Declutterer.py
File metadata and controls
197 lines (173 loc) · 5.91 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
import os
import shutil
import requests
from bs4 import BeautifulSoup
import time
from pathlib import Path
from tqdm import tqdm,trange
import sys
import keyboard
file_types = {
".txt": "Text",
".docx": "Word",
".xlsx": "Excel",
".pdf": "PDF",
".html": "Webpage.html",
".js": "JavaScript",
".css": "Stylesheet",
".py": "Python Script",
".java": "Java Source Code",
".c": "C Source Code",
".cpp": "C++ Source Code",
".rb": "Ruby Script",
".php": "PHP Script",
".pl": "Perl Script",
".swift": "Swift Source Code",
".go": "Go Source Code",
".kt": "Kotlin Source Code",
".ts": "TypeScript",
".json": "JSON Data",
".xml": "XML Document",
".sql": "SQL Database",
".md": "Markdown Document",
".log": "Log File",
".bat": "Batch Script",
".ps1": "PowerShell Script",
".sh": "Shell Script",
".csv": "CSV File",
".tsv": "TSV File",
".yaml": "YAML Document",
".ini": "INI Configuration",
".rtf": "Rich Text Format",
".tex": "LaTeX Document",
".aspx": "ASP.NET Page",
".jar": "Java Archive",
".lnk": "Windows Shortcut",
".out": "Unix Executable",
".bin": "Binary Data",
".mp3": "MP3 Audio",
".mp4": "MP4 Video",
".jpg": "JPEG Image",
".png": "PNG Image",
".gif": "GIF Image",
".bmp": "Bitmap Image",
".wav": "WAV Audio",
".avi": "AVI Video",
".svg": "SVG Image",
".doc": "Legacy Word Document",
".xls": "Legacy Excel",
".ppt": "Legacy PowerPoint",
".odt": "OpenDocument Text",
".ods": "OpenDocument Spreadsheet",
".odp": "OpenDocument Presentation",
}
file_types_set = set(file_types.keys())
def countFiles(dir):
count = 0
for item in os.listdir(dir):
item_path = os.path.join(dir, item)
if os.path.isfile(item_path):
count += 1
return count
def create_move(folder_name,dir,f,x):
newpath = os.path.join(dir,folder_name)
#print("NewPath1000: ",newpath)
#if not os.path.exists(newpath):
isExist = os.path.exists(newpath)
if not isExist:
try:
os.mkdir(newpath)
except OSError as error:
print(error)
else:
#print("NewPath",newpath)
shutil.move(f,newpath)
namefinal = os.path.join(newpath,x)
if os.path.exists(namefinal):
#print("Move Successful \nPAth: ",namefinal)
pass
def identify_file(ftype):
search_url = f'https://fileinfo.com/extension/{ftype[1:]}'
try:
response = requests.get(search_url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
descript = soup.find('div', {'class': 'entryHeader'}).get_text()
web_extract = descript.strip()
file_name = web_extract.split("Developer")
return file_name[0].strip()
except requests.exceptions.RequestException:
print("Request Error")
pass
return "Unknown"
def printDot(reps,delay):
for i in range(0,reps):
time.sleep(delay)
print(".",end = ' ',flush = True)
time.sleep(delay)
def olderExe(fname,ftype,f,x):
#print("Check 1 older exe",f,"\n>>>>>>>>>>>>>>>>>>>>",x)
current_time = time.time()
folder_name = identify_file(ftype)
exePath = os.path.join(dir,folder_name)
modification_time = os.path.getmtime(os.path.join(exePath,x))
#print(fname)
file_life = current_time - modification_time
if file_life > 15*24*60*60:
#print("Check 2 older exe")
print("ExePath: ",exePath)
trash_folder = "OLD FILES"
create_move(trash_folder,exePath,os.path.join(exePath,x),x)
#print("Check 3 older exe")
def sort(dir):
total_files = countFiles(dir)
print("Total Files: ",total_files)
if total_files == 0:
print("No file to sort.\nProcess Ended")
#progress_bar.close()
else:
progress_bar = tqdm(total = total_files, unit = "file",desc="Sorting")
for x in os.listdir(dir):
f = os.path.join(dir,x)
#print("CurrentFilePath",f)
if os.path.isfile(f):
fname , ftype = os.path.splitext(f)
#print(fname," ",ftype)
#print("CurrentFilePath",f," ","FileName",fname," ","Ftype",ftype)
if ftype in file_types_set:
folder_name = file_types[ftype]
create_move(folder_name,dir,f,x)
else:
folder_name = identify_file(ftype)
create_move(folder_name,dir,f,x)
# if ftype == ".exe":
# olderExe(fname,ftype,f,x)
progress_bar.update(1)
progress_bar.close()
print("\nSorted Successfully")
def deleteExe(dir):
name = identify_file(".exe")
destination = os.path.join(dir,name)
trash = os.path.join(destination,"OLD FILES")
ch = input("Do you want to delete exe older than 15 days (Y/N):...?")
if ch.upper() == 'Y':
for x in os.listdir(trash):
f = os.path.join(trash,x)
#print("CurrentFilePath",f)
if os.path.isfile(f):
fname , ftype = os.path.splitext(f)
#print(fname," ",ftype)
#print("CurrentFilePath",f," ","FileName",fname," ","Ftype",ftype)
os.remove(f)
else:
print("OK, Files Sorted to folder OLDER FILES inside", identify_file(".exe"))
dirx = input("Enter the Parent Directory: ")
dir = rf"{dirx}"
print("Reading Directory...", end = ' ')
#printDot(3,2)
print("\n")
#or i in tqdm (range (100), desc="Loading..."):
sort(dir)
#deleteExe(dir);
print("Press any key to exit...")
keyboard.read_event()