-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSYS-WIN.TRUNCATE.v1.py
81 lines (81 loc) · 3.94 KB
/
SYS-WIN.TRUNCATE.v1.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#--
#-- ************************************************************************************************************:
#-- ********************************************** TRUNCATE FILES **********************************************:
#-- ************************************************************************************************************:
#-- Author: JBALLARD (JEB) :
#-- Date: 2023.9.12 :
#-- Script: SYS-WIN.TRUNCATE.py :
#-- Purpose: A python script that scans directory paths for files/folders older than x days & purges them. :
#-- Version: 1.0 :
#-- ************************************************************************************************************:
#-- ************************************************************************************************************:
#--
#-- *************************************************:
#-- DEFINE PARAMS, CONFIG PATHS, IMPORT CLASSES :
#-- *************************************************:
import os
import shutil
import datetime
#--
#-- DIRECTORIES TO PURGE:
DIRPaths = [
'C:/WINDOWS/TEMP/',
'C:/WINDOWS/Prefetch/',
'C:/WINDOWS/SoftwareDistribution/DOWNLOAD/',
'C:/Users/jballard/AppData/Local/Downloaded Installations/',
'C:/Users/jballard/AppData/Local/TEMP/',
'C:/Users/jballard/Pictures/',
'C:/Users/jballard/Videos/',
'C:/Users/jballard-admin/AppData/Local/TEMP/',
'C:/Users/jballard-admin/AppData/Local/Downloaded Installations/',
'C:/Users/jballard-admin/Pictures/',
'C:/Users/jballard-admin/Videos/',
]
#--
#-- AGE OF FILES/FOLDERS TO PURGE:
MAXAgeDays = 0
#--
#-- CALCULATE CUTOFF DATE & ITERATE THROUGH FILES WITHIN DIRECTORY::
CUTOFFDate = datetime.datetime.now() - datetime.timedelta(days=MAXAgeDays)
#-- ITERATE THROUGH EACH DIRECTORY PATH:
for DIRPath in DIRPaths:
#--
#-- VERIFY DIR EXISTS:
if os.path.exists(DIRPath):
#-- ITERATE THROUGH ITEMS IN DIR:
for item in os.listdir(DIRPath):
ITEMPath = os.path.join(DIRPath, item)
#--
#-- VERIFY ITEM IS A DIR:
if os.path.isdir(ITEMPath):
#-- RETRIEVE LAST MOD TIME OF DIR:
DIRMTime = datetime.datetime.fromtimestamp(os.path.getmtime(ITEMPath))
#--
#-- COMPARE MOD TIME WITH CUTOFF DATE:
if DIRMTime < CUTOFFDate:
try:
#-- PURGE DIR(s):
shutil.rmtree(ITEMPath)
print(f" SUCCESS - TRUNCATED DIRECTORY - {item} (LAST MODIFIED - {DIRMTime})")
except Exception as e:
print(f" FAILURE - TRUNCATING DIRECTORY - {item}: {e}")
#--
#-- VERIFY ITEM IS A FILE:
elif os.path.isfile(ITEMPath):
#-- RETRIEVE LAST MOD TIME OF FILE:
FILEMTime = datetime.datetime.fromtimestamp(os.path.getmtime(ITEMPath))
#--
#-- COMPARE MOD TIME WITH CUTOFF DATE:
if FILEMTime < CUTOFFDate:
try:
#-- PURGE FILE(s):
os.remove(ITEMPath)
print(f" SUCCESS - TRUNCATED FILE - {item} (LAST MODIFIED - {FILEMTime})")
except Exception as e:
print(f" FAILURE - TRUNCATING FILE - {item}: {e}")
else:
print(f" NOTE - DIRECTORY NOT FOUND: {DIRPath}")
#--
#-- *************************************************:
#-- END OF SCRIPT :
#-- *************************************************: