Clean.bat Utility — Automate Disk Cleanup in One Script

Build & Customize Your Clean.bat Utility for Routine Maintenance

What it is

A Clean.bat utility is a Windows batch script that automates routine system maintenance tasks — deleting temporary files, clearing Recycle Bin, removing browser caches, and cleaning log files — to free disk space and improve performance.

Core features to include

  • Delete temp folders (%TEMP%, C:\Windows\Temp)
  • Empty Recycle Bin
  • Clear common browser caches (optional commands or calls to browser tools)
  • Remove old log files by age
  • Compress or archive important logs before deletion
  • Run Disk Cleanup (cleanmgr) with saved settings
  • Log actions and errors to a timestamped report
  • Dry-run mode to preview actions without deleting
  • Scheduled runs via Task Scheduler with user-specified frequency

Example structure (components)

  1. Initialization: set variables, check for admin rights, create log folder.
  2. Safety checks: exclude important paths, confirm free space threshold.
  3. Cleanup routines: modular functions for each target (temp, recycle bin, browser, logs).
  4. Archiving: optional zip/archive step for specified directories.
  5. Reporting: write deletion summary, errors, and runtime to log.
  6. Scheduling: instructions to create a Task Scheduler entry.

Basic example commands

  • Remove temp files: del /s /q “%TEMP%*” 2>nul for /d %%x in (“%TEMP%*”) do @rd /s /q “%%x” 2>nul
  • Empty Recycle Bin: PowerShell -Command “Clear-RecycleBin -Force”
  • Delete files older than 30 days: forfiles /p “C:\Path\To\Logs” /s /m . /d -30 /c “cmd /c del @path” 2>nul
  • Run Disk Cleanup: cleanmgr /sagerun:1

Safety tips

  • Always include exclusions for system folders and user data.
  • Start with a dry-run mode that logs what would be deleted.
  • Keep backups of critical files and test on a non-production machine.
  • Use admin elevation only when necessary.

How to customize

  • Add/modify paths and file age thresholds.
  • Integrate with 7-Zip for archiving: “C:\Program Files\7-Zip\7z.exe” a -tzip “%LOGDIR%\logs-%date%.zip” “C:\Logs*”
  • Use PowerShell snippets

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *