C: drive cleanup batch script style...


Recently, I have been going through a number of my departments procedures. It seems that during our windows patching cycles updates continuously fail on servers due to insufficient C: drive space. While cleaning up the necessary files for patching to complete is far from difficult, it is relatively monotonous. With this in mind I set out to write a simple script to clear many of the temporary file locations on Windows 2003, 2008 and 2012 servers, thus automating the C drive cleanup process for my team and I:

NOTE: After copying and pasting this script into a .txt file please make sure to rename/save as filename.bat (filename can be changed to whatever title you would like the script to be named - ed.). This process might require you to "show hidden files and paths" to verify the file is named correctly.  Also, make sure to run this script  as administrator from the server you wish to cleanup. This can be done simply by right clicking the script ([filename].bat) and selecting "run as administrator"

REM remove any dump files or temp files
DEL %SystemDrive%\*.TMP /S
DEL %SystemDrive%\*.DMP /S


REM delete past KB updates from Microsoft
forfiles /p "%WinDir%\$hf_mig$" /s /m *.* /c "cmd /c Del @path" /d -180

REM delete Performance Information and Tools logs and Reliability and Performance Monitor logs
forfiles /p "%SystemDrive%\perflogs" /s /m *.* /c "cmd /c Del @path" /d -180

REM remove old installation of TSM backup agent installation
rmdir /s /q %SystemDrive%\tsm_images

REM remove all files found in the windows temporary file location
del /f /q "%temp%\*.*"

REM remove file used for past Windows service pack installations
if exist "%WinDir%\servicepackfiles\i386" DEL "%WinDir%\servicepackfiles\i386\*.*" /s /q

REM if the user folder exists remove specific file types if they are found to clear up space. If not jump to skip.
if not exist "%WinDir%\Users\*.*" goto skip
if exist "%WinDir%\Users\*.zip" del "%WinDir%\Users\*.zip" /f /q
if exist "%WinDir%\Users\*.gif" del "%WinDir%\Users\*.gif" /f /q
if exist "%WinDir%\Users\*.jpg" del "%WinDir%\Users\*.jpg" /f /q
if exist "%WinDir%\Users\*.png" del "%WinDir%\Users\*.png" /f /q
if exist "%WinDir%\Users\*.bmp" del "%WinDir%\Users\*.bmp" /f /q
if exist "%WinDir%\Users\*.avi" del "%WinDir%\Users\*.avi" /f /q
if exist "%WinDir%\Users\*.mpg" del "%WinDir%\Users\*.mpg" /f /q
if exist "%WinDir%\Users\*.mpeg" del "%WinDir%\Users\*.mpeg" /f /q
if exist "%WinDir%\Users\*.ra" del "%WinDir%\Users\*.ra" /f /q
if exist "%WinDir%\Users\*.ram" del "%WinDir%\Users\*.ram"/f /q
if exist "%WinDir%\Users\*.mp3" del "%WinDir%\Users\*.mp3" /f /q
if exist "%WinDir%\Users\*.mov" del "%WinDir%\Users\*.mov" /f /q
if exist "%WinDir%\Users\*.qt" del "%WinDir%\Users\*.qt" /f /q
if exist "%WinDir%\Users\*.asf" del "%WinDir%\Users\*.asf" /f /q

REM if the users\users folder exists remove specific file types if they are found to clear up space. If not go jump to tempFiles
:skip
if not exist %WinDir%\Users\Users\*.* goto tempFiles /f /q
if exist %WinDir%\Users\AppData\Temp\*.zip del %WinDir%\Users\Users\*.zip /f /q
if exist %WinDir%\Users\AppData\Temp\*.gif del %WinDir%\Users\Users\*.gif /f /q
if exist %WinDir%\Users\AppData\Temp\*.jpg del %WinDir%\Users\Users\*.jpg /f /q
if exist %WinDir%\Users\AppData\Temp\*.png del %WinDir%\Users\Users\*.png /f /q
if exist %WinDir%\Users\AppData\Temp\*.bmp del %WinDir%\Users\Users\*.bmp /f /q
if exist %WinDir%\Users\AppData\Temp\*.avi del %WinDir%\Users\Users\*.avi /f /q
if exist %WinDir%\Users\AppData\Temp\*.mpg del %WinDir%\Users\Users\*.mpg /f /q
if exist %WinDir%\Users\AppData\Temp\*.mpeg del %WinDir%\Users\Users\*.mpeg /f /q
if exist %WinDir%\Users\AppData\Temp\*.ra del %WinDir%\Users\Users\*.ra /f /q
if exist %WinDir%\Users\AppData\Temp\*.ram del %WinDir%\Users\Users\*.ram /f /q
if exist %WinDir%\Users\AppData\Temp\*.mp3 del %WinDir%\Users\Users\*.mp3 /f /q
if exist %WinDir%\Users\AppData\Temp\*.asf del %WinDir%\Users\Users\*.asf /f /q
if exist %WinDir%\Users\AppData\Temp\*.qt del %WinDir%\Users\Users\*.qt /f /q
if exist %WinDir%\Users\AppData\Temp\*.mov del %WinDir%\Users\Users\*.mov /f /q

REM remove temporary debug and cache files for IIS installations
:tempFiles
del "%SystemDrive%\inetpub\wwwroot\cache\*.*" /f /q
del "%SystemDrive%\debugdiag\*.*" /f /q

REM remove any un needed past Intel installation files from computer.
if exist %SystemDrive%\Intel rmdir /S /Q %SystemDrive%\Intel
if exist %SystemDrive%\i386 rmdir /S /Q %SystemDrive%\i386

REM removes patch catch folder location found in Windows Server 2008 and above
rmdir /s /q "%WinDir%\Installer\$PatchCache$\"

REM If the location exists recurse through all User folders and cleanup temporary file locations(Win 7, 8)
IF EXIST "%SystemDrive%\Users\" (
    for /D %%x in ("%SystemDrive%\Users\*") do (
        rmdir /s /q "%%x\AppData\Local\Temp"
        mkdir "%%x\AppData\Local\Temp"
  rmdir /s /q "%%x\AppData\Local\Microsoft\Windows\Temporary Internet Files"
  mkdir "%%x\AppData\Local\Microsoft\Windows\Temporary Internet Files"
    )
)

REM If the location exists recurse through all documents and settings files and cleanup temporary file locations(Win XP)
IF EXIST "%SystemDrive%\Documents and Settings\" (
    for /D %%x in ("%SystemDrive%\Documents and Settings\*") do (
        rmdir /s /q "%%x\Local Settings\Temporary Internet Files"
        mkdir "%%x\Local Settings\Temporary Internet Files"
  rmdir /s /q "%%x\Local Settings\Temp"
        mkdir "%%x\Local Settings\Temp"\
  forfiles "%%x\application data\vulscan" /s /m *.log* /c "cmd /c Del @path" /d -90
  forfiles "%%x\application data\vulscan" /s /m *mergedgetvulnerabilities* /c "cmd /c Del @path" /d -10
    )
)

REM run windows disk cleanup on the C drive.
cleanmgr /sagerun:1 /c


This script is now being used by the majority of my team members and has taken a job that can take up to 10 minutes and reduced it down to under 1. Our server monitoring team is even looking into pushing this script every time it is determined that a servers main drive falls under 800MB of free space which could save our team even more time.
Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment