ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Automated System Cleanup Script

Batch

Goal -- WPM

Ready
Exercise Algorithm Area
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
Algorithm description viewbox

Automated System Cleanup Script

Algorithm description:

This script performs a comprehensive system cleanup by removing temporary files, old log files, and empty directories. It allows for a 'dry run' mode to preview actions without making changes. This script is vital for maintaining system performance, freeing up disk space, and managing historical data by regularly clearing out unnecessary files.

Algorithm explanation:

The script employs a modular design with helper functions for logging and safe deletion of files and directories. It first defines configuration variables for directories and retention periods. The `DeleteFileSafely` and `DeleteDirectorySafely` functions handle the actual deletion, incorporating the `DRY_RUN` flag and checking for file/directory existence and emptiness respectively. The main logic then calls these helpers to clean temporary files (older than 1 day), old log files (older than `DAYS_TO_KEEP_LOGS`), and empty subdirectories within these locations. The `forfiles` command is extensively used to identify files and directories based on age and path. The script prioritizes safety by using `DRY_RUN` and explicit checks before deletion. This scenario provides extensive practice in file processing pipelines, system cleanup, conditional logic, and the use of external commands like `forfiles`.

Pseudocode:

1. Define configuration variables: temporary directory, log directory, log retention days, dry run flag.
2. Implement helper functions:
   a. `LogMessage(message)`: Logs a message with timestamp.
   b. `LogWarning(message)`: Logs a warning message with timestamp.
   c. `LogError(message)`: Logs an error message with timestamp.
   d. `DeleteFileSafely(filePath, logPrefix)`:
      i. If dry run is enabled, log a 'would delete' message and return.
      ii. Check if the file exists; if not, log a warning and return.
      iii. Log the deletion attempt.
      iv. Delete the file.
      v. Log success or failure.
   e. `DeleteDirectorySafely(dirPath, logPrefix)`:
      i. If dry run is enabled, log a 'would delete' message and return.
      ii. Check if the directory exists; if not, log a warning and return.
      iii. Check if the directory is empty; if yes:
         - Log the deletion attempt.
         - Delete the directory.
         - Log success or failure.
      iv. If the directory is not empty, log a warning and return.
3. Log the start of the cleanup process.
4. If dry run is enabled, log a notification.
5. Cleanup Temporary Files:
   a. Log the start of temp file cleanup.
   b. Check if the temp directory exists; if not, log a warning.
   c. If it exists:
      i. Change current directory to temp directory.
      ii. Use `forfiles` to find files older than 1 day and call `DeleteFileSafely` for each.
      iii. Use `forfiles` to find directories older than 1 day (for empty subdirs) and call `DeleteDirectorySafely` for each.
      iv. Change back to the original directory.
6. Cleanup Old Log Files:
   a. Log the start of log file cleanup.
   b. Check if the log directory exists; if not, log a warning.
   c. If it exists:
      i. Change current directory to log directory.
      ii. Use `forfiles` to find log files older than `DAYS_TO_KEEP_LOGS` and call `DeleteFileSafely` for each.
      iii. Use `forfiles` to find directories older than `DAYS_TO_KEEP_LOGS` (for empty subdirs) and call `DeleteDirectorySafely` for each.
      iv. Change back to the original directory.
7. Log completion of the cleanup process.