1@echo off
2
3REM Configuration
4set TEMP_DIR=C:\Users\%USERNAME%\AppData\Local\Temp
5set LOG_DIR=C:\AppLogs
6set DAYS_TO_KEEP_LOGS=30
7set RECYCLE_BIN_PATH=C:\$Recycle.Bin
8set DRY_RUN=false
9
10REM --- Helper Functions ---
11
12:LogMessage
13echo [%date% %time%] %*
14goto :EOF
15
16:LogWarning
17echo [WARN] [%date% %time%] %*
18goto :EOF
19
20:LogError
21echo [ERROR] [%date% %time%] %*
22goto :EOF
23
24:DeleteFileSafely
25set "FILE_TO_DELETE=%~1"
26set "LOG_PREFIX=%~2"
27
28if "%DRY_RUN%"=="true" (
29call :LogMessage "%LOG_PREFIX% (Dry Run): Would delete file: %FILE_TO_DELETE%"
30goto :EOF
31)
32
33if not exist "%FILE_TO_DELETE%" (
34call :LogWarning "%LOG_PREFIX%: File not found, cannot delete: %FILE_TO_DELETE%"
35goto :EOF
36)
37
38echo Deleting file: %FILE_TO_DELETE%
39call :LogMessage "%LOG_PREFIX%: Deleting file: %FILE_TO_DELETE%"
40del "%FILE_TO_DELETE%"
41if errorlevel 1 (
42call :LogError "%LOG_PREFIX%: Failed to delete file: %FILE_TO_DELETE%"
43) else (
44call :LogMessage "%LOG_PREFIX%: Successfully deleted file: %FILE_TO_DELETE%"
45)
46goto :EOF
47
48:DeleteDirectorySafely
49set "DIR_TO_DELETE=%~1"
50set "LOG_PREFIX=%~2"
51
52if "%DRY_RUN%"=="true" (
53call :LogMessage "%LOG_PREFIX% (Dry Run): Would delete directory: %DIR_TO_DELETE%"
54goto :EOF
55)
56
57if not exist "%DIR_TO_DELETE%" (
58call :LogWarning "%LOG_PREFIX%: Directory not found, cannot delete: %DIR_TO_DELETE%"
59goto :EOF
60)
61
62REM Check if directory is empty before deleting
63pushd "%DIR_TO_DELETE%"
64if not exist *.* (
65echo Deleting empty directory: %DIR_TO_DELETE%
66call :LogMessage "%LOG_PREFIX%: Deleting empty directory: %DIR_TO_DELETE%"
67popd
68rmdir "%DIR_TO_DELETE%"
69if errorlevel 1 (
70call :LogError "%LOG_PREFIX%: Failed to delete empty directory: %DIR_TO_DELETE%"
71) else (
72call :LogMessage "%LOG_PREFIX%: Successfully deleted empty directory: %DIR_TO_DELETE%"
73)
74) else (
75call :LogWarning "%LOG_PREFIX%: Directory not empty, skipping deletion: %DIR_TO_DELETE%"
76popd
77)
78goto :EOF
79
80REM --- Main Cleanup Logic ---
81
82call :LogMessage "Starting system cleanup process."
83if "%DRY_RUN%"=="true" (
84call :LogMessage "*** DRY RUN MODE ENABLED - NO FILES OR DIRECTORIES WILL BE DELETED ***"
85)
86
87REM --- Cleanup Temporary Files ---
88call :LogMessage "Cleaning temporary files in '%TEMP_DIR%'..."
89if not exist "%TEMP_DIR%" (
90call :LogWarning "Temporary directory '%TEMP_DIR%' not found. Skipping."
91) else (
92pushd "%TEMP_DIR%"
93REM Delete files older than 1 day in Temp directory
94forfiles /p . /m *.* /d -1 /c "cmd /c call :DeleteFileSafely @path 'TempFiles'"
95REM Delete empty subdirectories in Temp directory
96forfiles /p . /s /d -1 /c "cmd /c call :DeleteDirectorySafely @path 'TempSubDirs'"
97popd
98)
99
100REM --- Cleanup Old Log Files ---
101call :LogMessage "Cleaning log files in '%LOG_DIR%' older than %DAYS_TO_KEEP_LOGS% days..."
102if not exist "%LOG_DIR%" (
103call :LogWarning "Log directory '%LOG_DIR%' not found. Skipping."
104) else (
105pushd "%LOG_DIR%"
106REM Delete log files older than DAYS_TO_KEEP_LOGS
107forfiles /p . /m *.log /d -%DAYS_TO_KEEP_LOGS% /c "cmd /c call :DeleteFileSafely @path 'LogFiles'"
108REM Delete empty subdirectories in Log directory (that might have been created by old logs)
109forfiles /p . /s /d -%DAYS_TO_KEEP_LOGS% /c "cmd /c call :DeleteDirectorySafely @path 'LogSubDirs'"
110popd
111)
112
113REM --- Cleanup Recycle Bin (Simulated - actual Recycle Bin manipulation is complex and risky in batch) ---
114REM Direct manipulation of the Recycle Bin is generally not recommended or straightforward in batch.
115REM This section is a placeholder to illustrate the concept of cleaning system-level items.
116REM A real implementation might use PowerShell or specific tools.
117
118
119call :LogMessage "Recycle Bin cleanup is a complex operation and is not fully implemented in this batch script."
120
121REM --- Cleanup Empty Directories (General Scan) ---
122REM This is a broad scan and might need refinement based on specific needs.
123REM We'll scan the current drive for empty directories. This can be slow.
124REM For simplicity, we'll focus on specific directories already defined.
125
126
127REM Example: Scan a specific directory for empty subdirs if not already covered.
128REM For this drill, we've covered Temp and Log dirs. A more general scan is omitted for brevity and safety.
129
130
131call :LogMessage "System cleanup process completed."
132goto :EOF