Edited, Thanks Olaf for the reminder about redirection,
For years I used a simple app named DeleteGUI to delete TEMP files off a PCs hard drive from within PE. I used it before imaging, or just as a general cleanup utility. Alas, it is not compatible for Windows Vista / 7 / 8 and the author is no where to be found. So I wrote a simple batch file to do it with. It deletes USERS and SYSTEM temp files, Temporary Internet files, the Recycle.bin and deletes the page file and hibernation file. I wanted something small and lean and without working through something like Runscanner. Not a full feature cleaner tool but does what it needs to to eliminate useless cruft.
Here are some issues:
I can't suppress all error messages despite driving output into NULFixed - thanks Olaf!I can't seem to actually DELETE the $RECYCLE.BIN, I can empty it but not deletes it, this causes a corruption error the first time Windows is started.Fixed, goofy RD syntax- 2 dependencies: ICACLS.EXE to enable deletion of the hiberfil.sys file, else access is denied, and ATTRIB.EXE, both must be in your build.
- I run into some mysterious HIDDEN or READ ONLY files occasionally in profiles, this is why the extra step of using ATTRIB was added
- You must enter the REAL Windows OS drive a the prompt or command line. Simply take a peek in Explorer to determine this, a quick confirmation was added to verify the USERS folder just in case.
So guys-n-gals, tear it apart, make it better or use it just the way it is. If we can get it looking better then I'll wrap it in a Winbuilder script to make it build ready.
@echo off :: DelTempFiles Script, V3 MAR 29 2013 by Rootman :: DelTempFiles Script, deletes USERS, and SYSTEM TEMP files, USERS INTERNET TEMPoORARY FILES, $Recycle.Bin, Hiberfil.sys and pagefile.sys :: DEsigned to run under Windows PE on WIndows Vista, 7 and 8, can run directly under Windows but won't delete IN USE files :: Requires ATTRIB.EXE and ICACLS.EXE in your PE build IF NOT "%1"=="" GOTO :SpecDrive echo. set /p DrvLet= Which drive holds the real Windows OS? e.g. C:, D:, E:, F: G: ? ^>^ set /A DrvLet-%DrvLet% :SpecDrive echo. if not exist %DrvLet%\Users Echo Not a valid Drive letter, try again! && echo. && pause && goto :EOF %DrvLet% cd \Users dir echo. SET /P ASKYN= Does the above show the USERS folders? Y or N ^>^ echo. if "%ASKYN%"=="Y" GOTO STARTDEL if "%ASKYN%"=="y" GOTO STARTDEL GOTO :EOF :STARTDEL :: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo. echo Deleting TEMP files and folders, this may take a while, please wait echo. :: Crawl the USER directories entries and deleted all TEMP and TEMPORARY INTERNET files :: Occasionally some files and folders will have weird attributes preventing DEL and RD from deleting them, so ATTRIB is run for /D %%a in (*.*) do ATTRIB -H -S -R /s /D "%%a\AppData\Local\Temp\*.*" >> nul 2>&1 :: RD will not remove a folder's CONTENTS only , it insists on blowing away the FOLDER itself, :: and it won't take the *.* paramter so you have to get inventive with scripting, :: below parses just subfolder names and RDs each one in turn for /D %%a in (*.*) do FOR /D %%b IN ("%%a\AppData\Local\Temp\*.*") DO RD /S /Q "%%b" >> nul 2>&1 :: Finally we delete the actual FILES in the temp folder for /D %%a in (*.*) do DEL /F /S /Q "%%a\AppData\Local\Temp\*.*" >> nul 2>&1 :: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo. echo Deleting TEMPORARY INTERNET files and folders, please wait echo. :: Fix and attribute issues for /D %%a in (*.*) do ATTRIB -H -S -R /s /D "%%a\AppData\Local\Microsoft\Windows\Temporary Internet Files\*.*" >> nul 2>&1 :: Delete the subfolders for /D %%a in (*.*) do FOR /D %%b IN ("%%a\AppData\Local\Microsoft\Windows\Temporary Internet Files\*.*") DO RD /S /Q "%%b" >> nul 2>&1 :: Delete files for /D %%a in (*.*) do DEL /F /S /Q "%%a\AppData\Local\Microsoft\Windows\Temporary Internet Files\*.*" >> nul 2>&1 :: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo. echo Deleting SYSTEM temp files and folders, please wait echo. IF NOT EXIST \TEMP GOTO SKIPROOTTEMP :: Fix attributes ATTRIB -H -S -R /S /D \temp\*.* >> nul 2>&1 :: SubFOlders FOR /D %%b IN ("\Temp\*.*") DO RD /S /Q "%%b" >> nul 2>&1 :: Files DEL /F /S /Q \temp\*.* >> nul 2>&1 :SKIPROOTTEMP :: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ IF NOT EXIST \Windows\TEMP GOTO SKIPWINTEMP :: Fix attributes ATTRIB -H -S -R /S /D \Windows\temp\*.* >> nul 2>&1 :: SubFolders FOR /D %%b IN ("\Windows\Temp\*.*") DO RD /S /Q "%%b" >> nul 2>&1 :: Files DEL /F /S /Q \Windows\temp\*.* >> nul 2>&1 :SKIPWINTEMP :: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :: Clean the RECYCLE.BIN echo. echo Deleting Recycle.bin, Pagefile and Hiberfil echo. :: Might not have correct rights to enable deletion, grant full control to EVERYONE icacls \$Recycle.bin /t /grant everyone:F >> nul 2>&1 :: Fix attributes attrib -H -S -R /S /D \$Recycle.bin\*.* >> nul 2>&1 :: Simply delete the whole Recycle.bin, it will be recreated when Windows restarts RD /S /Q \$Recycle.Bin\ >> nul 2>&1 :: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ IF NOT EXIST \pagefile.sys goto SKIPPAGEFILE Echo Delete the pagefile.sys :: Might not have correct rights to enable deletion, grant full control to EVERYONE icacls \pagefile.sys /grant everyone:F >> nul 2>&1 :: Make it visible for deletion attrib -H -S \pagefile.sys >> nul 2>&1 :: Delete it, will be recreated on restart DEL /F /Q \pagefile.sys >> nul 2>&1 :SKIPPAGEFILE :: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ IF NOT EXIST \hiberfil.sys goto SKIPHIBERFIL Echo Delete the hiberfil.sys :: Fix the rights icacls \hiberfil.sys /grant everyone:F >> nul 2>&1 :: Fix attributes attrib -H -S \hiberfil.sys >> nul 2>&1 :: Delete it, will be recreated on restart DEL /F /Q \hiberfil.sys >> nul 2>&1 :SKIPHIBERFIL :: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo. echo. echo. Echo All done . . . echo. pause