Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for flushbin.bat

(view source code of flushbin.bat as plain text)

  1. @ECHO OFF
  2. :: First check for Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Enable delayed variable expansion
  6. SETLOCAL ENABLEDELAYEDEXPANSION
  7.  
  8. :: Check for command line arguments -- none allowed
  9. IF NOT "%~1"=="" GOTO Syntax
  10.  
  11. :: Second check for Windows version -- XP required
  12. VER | FIND.EXE "Windows XP" >NUL || GOTO Syntax
  13.  
  14. :: Set temporary file name
  15. SET RegFile="%Temp%.\~FlushBin.tmp"
  16.  
  17. :: Read the current settings for CleanMgr
  18. START /WAIT REGEDIT.EXE /E %RegFile% "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin"
  19.  
  20. :: Find an unused job number
  21. CALL :FindUnused
  22.  
  23. :: Create a temporary file with the new job settings
  24. >  %RegFile% ECHO REGEDIT4
  25. >> %RegFile% ECHO.
  26. :: These WILL be cleaned
  27. FOR %%A IN ("Internet Cache Files" "Old ChkDsk Files" "Recycle Bin" "Temporary Files") DO (
  28. 	>> %RegFile% ECHO [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\%%~A]
  29. 	>> %RegFile% ECHO "StateFlags%Unused%"=dword:00000002
  30. 	>> %RegFile% ECHO.
  31. )
  32. :: These WON'T be cleaned
  33. FOR %%A IN ("Active Setup Temp Folders" "Compress old files" "Content Indexer Cleaner" "Downloaded Program Files" "Memory Dump Files" "Microsoft_Event_Reporting_2.0_Temp_Files" "Office Setup Files" "Offline Files" "Offline Pages Files" "Remote Desktop Cache Files" "Setup Log Files" "Temporary Offline Files" "WebClient and WebPublisher Cache") DO (
  34. 	>> %RegFile% ECHO [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\%%~A]
  35. 	>> %RegFile% ECHO "StateFlags%Unused%"=dword:00000000
  36. 	>> %RegFile% ECHO.
  37. )
  38.  
  39. :: Import the job in the registry
  40. START /WAIT REGEDIT.EXE /S %RegFile%
  41.  
  42. :: Run the cleanup job
  43. START /WAIT CLEANMGR.EXE /SageRun:%Unused%
  44.  
  45. :: Create a temporary file to delete the new job
  46. >  %RegFile% ECHO REGEDIT4
  47. >> %RegFile% ECHO.
  48. FOR %%A IN ("Internet Cache Files" "Old ChkDsk Files" "Recycle Bin" "Temporary Files" "Active Setup Temp Folders" "Compress old files" "Content Indexer Cleaner" "Downloaded Program Files" "Memory Dump Files" "Microsoft_Event_Reporting_2.0_Temp_Files" "Office Setup Files" "Offline Files" "Offline Pages Files" "Remote Desktop Cache Files" "Setup Log Files" "Temporary Offline Files" "WebClient and WebPublisher Cache") DO (
  49. 	>> %RegFile% ECHO [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\%%~A]
  50. 	>> %RegFile% ECHO "StateFlags%Unused%"=-
  51. 	>> %RegFile% ECHO.
  52. )
  53.  
  54. :: Delete the new job from the registry
  55. START /WAIT REGEDIT.EXE /S %RegFile%
  56.  
  57. :: Delete the temporary file
  58. DEL %RegFile%
  59.  
  60. :: Done
  61. ENDLOCAL
  62. GOTO:EOF
  63.  
  64.  
  65.  
  66. :FindUnused
  67. SET Unused=1000
  68. FOR /L %%A IN (1001,1,1100) DO IF "!Unused!"=="1000" CALL :CheckIfUsed %%A
  69. IF %Unused% GTR 1000 GOTO:EOF
  70. FOR /L %%A IN (1101,1,2000) DO IF "!Unused!"=="1000" CALL :CheckIfUsed %%A
  71. IF %Unused% GTR 1000 GOTO:EOF
  72. FOR /L %%A IN (2001,1,9999) DO IF "!Unused!"=="1000" CALL :CheckIfUsed %%A
  73. GOTO:EOF
  74.  
  75.  
  76.  
  77. :CheckIfUsed
  78. IF %Unused% GTR 1000 GOTO:EOF
  79. TYPE %RegFile% | FINDSTR.EXE /R /B /I /C:".StateFlags%1.=dword:"
  80. IF ERRORLEVEL 1 SET Unused=%1
  81. GOTO:EOF
  82.  
  83.  
  84.  
  85. :Syntax
  86. ECHO.
  87. ECHO FlushBin.bat,  Version 1.00 for Windows XP
  88. ECHO Cleanup IE cache, ChkDsk files and temporary files, and empty the Recycle Bin
  89. ECHO.
  90. ECHO Usage:  FLUSHBIN
  91. ECHO.
  92. ECHO Notes:  This batch file uses CLEANMGR.EXE /SageRun:nnnn to run a cleanup job,
  93. ECHO         but creates the cleanup job by writing it directly to the registry
  94. ECHO         instead of using CLEANMGR.EXE /SageSet:nnnn
  95. ECHO         CLEANMGR.EXE will perform a slow, unattended (not silent) cleanup.
  96. ECHO         The cleanup job will be removed from the registry when finished.
  97. ECHO         More info can be found at http://support.microsoft.com/kb/253597
  98. ECHO         and http://forums.scotsnewsletter.com/index.php?showtopic=10239
  99. ECHO.
  100. ECHO Written by Rob van der Woude
  101. ECHO http://www.robvanderwoude.com
  102.  
  103. IF "%OS%"=="Windows_NT" ENDLOCAL
  104.  

page last modified: 2024-04-16; loaded in 0.0063 seconds