Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
:: Purpose: Temp file cleanup
:: Requirements: Admin access helps but is not required
:: Author: hgate73
:: Version: 2.5 + Improved detection of operating system and added new OS detection section near script start
:: + Added section to remove C:\Windows\Media on Server-based operating systems
:: - Comment cleanup and removal of unneeded error piping (2>&1)
:: 2.4d / Switched to CUR_DATE format to be consistent with all other scripts
:: / LOGFILE variable is no longer appended with .log, instead now just uses log name that we specify at beginning of script
:: 2.4c + Added section to remove Dell builtin wallpaper
:: + Added SETLOCAL command to prevent system-wide variable changes
:: 2.4b / Minor comment cleanup
:: 2.4 + Added deletion of .exe files in C:\ at startup
:: 2.3 / Smarter log file rotation (uses size limit to determine rotating)
:: 2.2 + Log files now rotate and delete old versions
:: 2.1 + Small updates. Added version display while cleaning
:: 2.0 * Major re-write
:: + Added section to test for and delete hotfix uninstallers on XP
:: + Added log file
:: 1.9 / Fixed deleting of the Windows Tour section
:: 1.8 / Split into USER and SYSTEM subsections
:: 1.7 + Added section to delete Windows update log files and built-in .bmp files
:: 1.6 / Changed some delete flags to /F /S /Q instead of just /F /Q
:: The "/S" flag says to recurse into subdirectories.
:: 1.5 + Added new areas to clean -- %TEMP%\ folder
:: 1.0 Initial write
:: Prep
SETLOCAL
@echo off
color f0
set VERSION=2.5
%SystemDrive% && cd\ && cls
title [TEMP FILE CLEANUP v%VERSION%]
:::::::::::::::
:: VARIABLES ::
:::::::::::::::
:: Set your paths here. !! Don't use trailing slashes (\) in directory paths
set LOGPATH=%SystemDrive%\Logs
set LOGFILENAME=%COMPUTERNAME%_TempFileCleanup.log
:: Max log file size allowed in bytes before rotation and archive. 1048576 bytes is one megabyte
set LOG_MAX_SIZE=2097152
:: \/ Don't touch these two variables. If you do, you will break something.
set CUR_DATE=%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%
set IS_SERVER_OS=no
:::::::::::::::::::::::
:: LOG FILE HANDLING ::
:::::::::::::::::::::::
:: Make the logfile if it doesn't exist
if not exist %LOGPATH% mkdir %LOGPATH%
if not exist %LOGPATH%\%LOGFILENAME% echo. > %LOGPATH%\%LOGFILENAME%
:: Check log size. If it's less than our max, then jump to the cleanup section
for %%R in (%LOGPATH%\%LOGFILENAME%) do IF %%~zR LSS %LOG_MAX_SIZE% goto os_version_detection
:: If the log was too big, go ahead and rotate it.
pushd %LOGPATH%
del %LOGFILENAME%.ancient 2>NUL
rename %LOGFILENAME%.oldest %LOGFILENAME%.ancient 2>NUL
rename %LOGFILENAME%.older %LOGFILENAME%.oldest 2>NUL
rename %LOGFILENAME%.old %LOGFILENAME%.older 2>NUL
rename %LOGFILENAME% %LOGFILENAME%.old 2>NUL
popd
::::::::::::::::::::::::::
:: OS VERSION DETECTION ::
::::::::::::::::::::::::::
:os_version_detection
:: Check Windows version. If it's a Server OS set the variable "IS_SERVER_OS" to yes. This affects what we do later
wmic os get name | findstr /i "Server" > nul
IF %ERRORLEVEL%==0 set IS_SERVER_OS=yes
::::::::::::::::::::::::::
:: USER CLEANUP SECTION ::
::::::::::::::::::::::::::
:: Create the log header for this job
echo ----------------------------------------------------------------------------------->> %LOGPATH%\%LOGFILENAME%
echo Starting temp file cleanup as %USERDOMAIN%\%USERNAME% on %CUR_DATE% at %TIME% >> %LOGPATH%\%LOGFILENAME%
echo ----------------------------------------------------------------------------------->> %LOGPATH%\%LOGFILENAME%
title [CLEANING TEMP FILES v%VERSION%]
:: Status message to the user
echo.
echo Starting temp file cleanup at %TIME%.
echo ------------------------------------------
echo.
echo Cleaning USER temp files...
:: This is ugly but it creates the log line.
echo. >> %LOGPATH%\%LOGFILENAME% && echo ! Cleaning USER temp files... >> %LOGPATH%\%LOGFILENAME% && echo. >> %LOGPATH%\%LOGFILENAME%
:: User temp files, history, and random My Documents stuff
del "%TEMP%\*" /F /S /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del "%SystemDrive%%HOMEPATH%\Local Settings\Temp\*.*" /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del "%SystemDrive%%HOMEPATH%\Recent\*.*" /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del "%SystemDrive%%HOMEPATH%\Local Settings\Temporary Internet Files\*.*" /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del "%SystemDrive%%HOMEPATH%\Local Settings\Application Data\ApplicationHistory\*.*" /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del "%SystemDrive%%HOMEPATH%\My Documents\*.tmp" >> %LOGPATH%\%LOGFILENAME% 2>&1
echo.
echo Done.
echo. >> %LOGPATH%\%LOGFILENAME% && echo ! Done. >> %LOGPATH%\%LOGFILENAME% && echo. >> %LOGPATH%\%LOGFILENAME%
::::::::::::::::::::::::::::
:: SYSTEM CLEANUP SECTION ::
::::::::::::::::::::::::::::
echo.
echo Cleaning SYSTEM temp files...
echo ! Cleaning SYSTEM temp files... >> %LOGPATH%\%LOGFILENAME% && echo. >> %LOGPATH%\%LOGFILENAME%
:: System temp files
del "%WINDIR%\TEMP\*" /F /S /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
:: Root drive garbage (usually C drive)
del "%SystemDrive%\*.bat" /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del "%SystemDrive%\*.txt" /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del "%SystemDrive%\*.log*" /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del "%SystemDrive%\*.jp*" /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del "%SystemDrive%\*.tmp" /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del "%SystemDrive%\*.bak" /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del "%SystemDrive%\*.backup" /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del "%SystemDrive%\*.exe" /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
rmdir /S /Q %SystemDrive%\Temp >> %LOGPATH%\%LOGFILENAME% 2>&1
rmdir /S /Q %SystemDrive%\MSOCache >> %LOGPATH%\%LOGFILENAME% 2>&1
rmdir /S /Q %SystemDrive%\Intel >> %LOGPATH%\%LOGFILENAME% 2>&1
rmdir /S /Q %SystemDrive%\i386 >> %LOGPATH%\%LOGFILENAME% 2>&1
:: Windows update logs & built-in backgrounds (space waste)
del %WINDIR%\*.log /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del %WINDIR%\*.txt /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del %WINDIR%\*.bmp /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del %WINDIR%\*.tmp /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
del %WINDIR%\Web\Wallpaper\*.* /F /Q >> %LOGPATH%\%LOGFILENAME% 2>&1
rmdir /S /Q %WINDIR%\Web\Wallpaper\Dell >> %LOGPATH%\%LOGFILENAME% 2>&1
:: Flash cookies
del %appdata%\Macromedia\Flash Player\#SharedObjects\* >> %LOGPATH%\%LOGFILENAME% 2>&1
:: Windows "guided tour" annoyance
del %WINDIR%\system32\dllcache\tourstrt.exe >> %LOGPATH%\%LOGFILENAME% 2>&1
del %WINDIR%\system32\dllcache\tourW.exe >> %LOGPATH%\%LOGFILENAME% 2>&1
rmdir /S /Q %WINDIR%\Help\Tours >> %LOGPATH%\%LOGFILENAME% 2>&1
echo.
echo Done.
echo. >> %LOGPATH%\%LOGFILENAME%
echo ! Done. >> %LOGPATH%\%LOGFILENAME%
echo. >> %LOGPATH%\%LOGFILENAME%
::::::::::::::::::::::::::::
:: Windows Server cleanup :: -- This section runs only if the OS is Windows Server 2000, 2003 or 2008
::::::::::::::::::::::::::::
:server_cleanup
:: 1. Check our operating system. If it's not a Server OS, skip this section.
IF %IS_SERVER_OS%==no goto xp_hotfix_cleanup
:: 2. If we made it here then we're on a Server OS, so go ahead and run server-specific tasks
echo.
echo ! Windows Server operating system detected.
echo Removing built-in media files (.wav, .midi, etc)...
echo.
echo. >> %LOGPATH%\%LOGFILENAME% && echo ! Windows Server operating system detected. Removing built-in media files (.wave, .midi, etc)... >> %LOGPATH%\%LOGFILENAME% && echo. >> %LOGPATH%\%LOGFILENAME%
:: 3. Take ownership of the files so we can actually delete them. By default even Administrators have Read-only rights.
echo ! Taking ownership of %WINDIR%\Media in order to delete files... && echo.
echo ! Taking ownership of %WINDIR%\Media in order to delete files... >> %LOGPATH%\%LOGFILENAME% && echo. >> %LOGPATH%\%LOGFILENAME%
takeown /f %WINDIR%\Media /r /d y >> %LOGPATH%\%LOGFILENAME% 2>&1 && echo. >> %LOGPATH%\%LOGFILENAME%
icacls %WINDIR%\Media /grant administrators:F /t >> %LOGPATH%\%LOGFILENAME% && echo. >> %LOGPATH%\%LOGFILENAME%
:: 4. Do the cleanup
rmdir /S /Q %WINDIR%\Media>> %LOGPATH%\%LOGFILENAME% 2>&1
:: 5. Finally, check if we're on Server 2008 specifically. If we are, then skip the following hotfix cleanup section
wmic os get name | findstr /i "Server 2008" > nul
IF %ERRORLEVEL%==0 goto complete
:::::::::::::::::::::::::::::::
:: Windows XP hotfix cleanup ::
:::::::::::::::::::::::::::::::
:xp_hotfix_cleanup
:: This section tests for Windows XP hotfixes and deletes them if they exist.
:: These hotfixes use a lot of space so clearing them out is beneficial.
:: Really we should use a tool that deletes their corresponding registry entries, but oh well.
:: 1. Clear the ERRORLEVEL in case something above triggered it and enter the Windows directory (usually C:\WINDOWS)
set ERRORLEVEL=0
pushd %WINDIR%
:: 2. Build the list of hotfix folders. "$" signs always surround their name, e.g. "$NtUninstall092330$" or "$hf_mg$"
dir /A /B %WINDIR%\$*$ > %TEMP%\hotfix_nuke_list.txt 2>&1
:: 3. If DIR didn't find any folders, it sets ERRORLEVEL to 1. If we detect that then we log it and skip this whole section
IF %ERRORLEVEL%==1 echo ! No Windows XP hotfix uninstallers detected, skipping hotfix cleanup. >> %LOGPATH%\%LOGFILENAME% && echo. >> %LOGPATH%\%LOGFILENAME% && GOTO complete
:: 4. If we made it here then we're doing the cleanup. Go ahead and notify the user and log it.
echo.
echo ! Windows XP hotfix uninstallers detected.
echo Removing...
echo.
echo. >> %LOGPATH%\%LOGFILENAME% && echo ! Windows XP hotfix uninstallers detected. Cleaning up. >> %LOGPATH%\%LOGFILENAME%
:: 5. Do the hotfix clean up
for /f %%i in (%TEMP%\hotfix_nuke_list.txt) do (
echo Deleting %%i...
echo Deleted folder %%i >> %LOGPATH%\%LOGFILENAME%
rmdir /s /q %%i >> %LOGPATH%\%LOGFILENAME% 2>&1
)
:: 6. Log that we are done with hotfix cleanup and leave the Windows directory
echo. >> %LOGPATH%\%LOGFILENAME% && echo ! Windows XP hotfix uninstaller cleanup complete. >> %LOGPATH%\%LOGFILENAME% && echo.>> %LOGPATH%\%LOGFILENAME%
del %TEMP%\hotfix_nuke_list.txt >> %LOGPATH%\%LOGFILENAME%
popd
::::::::::::::::::::::::::
:: Cleanup and complete ::
::::::::::::::::::::::::::
:complete
@echo off
echo ----------------------------------------------------------------------------------->> %LOGPATH%\%LOGFILENAME%
echo Finished temp file cleanup as %USERDOMAIN%\%USERNAME% on %CUR_DATE% at %TIME% >> %LOGPATH%\%LOGFILENAME%
echo ----------------------------------------------------------------------------------->> %LOGPATH%\%LOGFILENAME%
title [CLEANUP COMPLETE]
echo.
echo Cleanup complete.
echo.
echo Log saved at: %LOGPATH%\%LOGFILENAME%
echo.
color
ENDLOCAL