Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Temp File Cleanup script

Domain Controllers

Temp File Cleanup script

by  hgate73  Posted    (Edited  )
Hi, I wrote this script in order to keep temp files to a minimum on our Windows Server 2008 machines. We also run it at startup (part of the login process) on all workstations in the domain.

Make sure to read the script before running it, so you're aware of what it deletes, and in case it clears out some areas you use (all .exe files in the root of C:\ for example).

To use this script: Copy and paste into a text file and rename to TempFileCleanup.bat

Script defaults:

[li]Logs what it deletes to C:\Logs\<name_of_computer>_TempFileCleanup.log. If you want to change this, just edit the LOGPATH and LOGFILENAME variables (Lines 36-37)[/li]
[li]All BAT, TXT, LOG, JP*, TMP, BAK, BACKUP, EXE files from the root of C:\ (Lines 107-114)[/li]
[li]All files out of the System temp folder as well as the current users' temp folder[/li]
[li]Removes all built-in Windows wallpapers (Line 125)[/li]
[li]Removes all built-in Dell wallpapers (Line 126)[/li]
[li]Removes C:\i386 - Microsoft Windows installation cache, this is a copy of the Windows installation files. Usually eats up 600 MB+ of space[/li]
[li]Removes C:\MSOCache - Microsoft Office installation cache[/li]
[li]Removes C:\Intel - Intel driver cache[/li]
[li]Removes C:\Temp - Created by some programs and not deleted[/li]
[li]Removes all Adobe Flash cookies (Line 129)[/li]
[li]Removes Windows XP "Guided Tour" annoyance (Lines 132-134)[/li]

[li]Finally, it removes all Hotfix Uninstallers on Windows XP and Windows Server 2003 (Lines 141-174), since these eat up a lot of space.[/li]

Any of that stuff you don't want to remove, just delete that line from the batch file.

Code:
:: 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
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top