Keeping your Windows system clean is essential for maintaining speed and performance. Over time, temporary files stored in the Prefetch, Windows Temp, and User Temp (%temp%) folders can accumulate and take up unnecessary space.
In this guide, you’ll learn how to create a powerful batch file that:- Deletes temporary files automatically
- Displays success and error messages
- Shows progress percentage during clean up
Getting Started
Temporary files are created by the system and applications for short-term use. However, they often remain even after they’re no longer needed.
Benefits of cleaning:- Frees up disk space
- Improves system performance
- Removes unnecessary clutter
- Helps fix minor software issues
Most users don’t manually write scripts instead, they rely on built-in Windows tools(Run Command) or simple actions to clean Prefetch, Temp, and %Temp% folders.
Cleaning up these folders manually is a lengthy and boring process. This batch file will help you clean all these files with a single click. The code and steps to create the batch file are provided below.
Code For Batch File
Here’s a simple Windows batch (.bat) script that deletes contents from Prefetch, Temp, and %Temp% folders safely. If you want to know how the batch file works and thier command then visit my prevous post.
@echo off
setlocal enabledelayedexpansion
echo ======================================
echo SMART TEMP CLEANUP SCRIPT
echo ======================================
echo.
set totalDeleted=0
set totalErrors=0
:: ---------- FUNCTION STYLE LABEL ----------
goto :start
:cleanupFolder
set folder=%~1
set deleted=0
set errors=0
echo Cleaning: %folder%
if not exist "%folder%" (
echo Folder not found!
echo.
goto :eof
)
for /r "%folder%" %%f in (*) do (
del /f /q "%%f" >nul 2>&1
if exist "%%f" (
set /a errors+=1
) else (
set /a deleted+=1
)
)
:: Remove empty directories
for /d %%d in ("%folder%\*") do rd /s /q "%%d" >nul 2>&1
:: Display result
if !deleted! gtr 0 (
echo SUCCESS: !deleted! files deleted.
) else (
echo No files deleted.
)
if !errors! gtr 0 (
echo WARN: !errors! files could not be removed.
)
echo.
:: Add to total
set /a totalDeleted+=deleted
set /a totalErrors+=errors
goto :eof
:start
:: ---------- CLEAN EACH FOLDER ----------
call :cleanupFolder "C:\Windows\Prefetch"
call :cleanupFolder "C:\Windows\Temp"
call :cleanupFolder "%temp%"
:: ---------- FINAL SUMMARY ----------
echo ======================================
echo CLEANUP SUMMARY
echo ======================================
echo Total Files Deleted: %totalDeleted%
echo Total Errors: %totalErrors%
echo ======================================
echo.
echo Script completed successfully.
pause
cmd /k
How to use:
- Open Notepad
- Paste the code above
- Save as:
cleanup.bat(choose All Files, not.txt) - Right-click → Run as Administrator (important for Prefetch + Windows Temp)
- Some files may not delete if they’re in use (that’s normal).
- Running as admin improves cleanup effectiveness.
- This won’t harm your system, but deleting Prefetch may slightly slow the first launch of apps temporarily.
- Count how many files were actually deleted
- Count failed deletions
- Show success only if something was deleted
- Avoid crashes (like division by zero)
- Keep the window open no matter what
Summary
Using a batch file to clean temporary folders is a simple yet effective way to keep your Windows system running smoothly. While batch scripting has limitations, adding features like progress tracking and error handling makes your cleanup process much more informative and reliable.
Thanks