Jump to content











Photo
- - - - -

Batch file to delete TEMP files and more


  • Please log in to reply
10 replies to this topic

#1 Rootman

Rootman

    Frequent Member

  • Advanced user
  • 382 posts
  • Location:USA

Posted 28 March 2013 - 06:38 PM

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 NUL Fixed - 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






#2 Olof Lagerkvist

Olof Lagerkvist

    Gold Member

  • Developer
  • 1448 posts
  • Location:Borås, Sweden
  •  
    Sweden

Posted 28 March 2013 - 08:01 PM

I can't suppress all error messages despite driving output into NUL

 

You seem to only redirect standard output and not standard error to NUL. You need to change > nul to either > nul 2> nul or to > nul 2>&1 . In any case, generally 2> redirection redirects error messages while > simply redirects non-error messages.



#3 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 28 March 2013 - 08:28 PM

And, AGAIN, it makes no sense to delete the contents of a directory to later remove the directory AND all it's contents :frusty:

If you RD /Q /S a directory tree, the directory and all it's contents will be removed.

BTW if you DEL /F /S /Q *.* you are NOT deleting ALL files, but only those corresponding to the mask *.* (i.e. only those that have an extension).

 

Imagine that you want to burn on a fire a number of boxes and their contents.

Normally you would put a box on the fire, and NOT take almost all the contents out of the box, put them on the fire, and later put the half empty box on it. :dubbio:

(let's see if put this way the message goes through :unsure:)

 

 

http://reboot.pro/to...der-windows-pe/

 

BTW, it's nice how you simplified the code snippet I provided you in the above (which BTW includes some 2>NUL redirections)  :whistling:

Just in case:

http://www.robvander...redirection.php

 

Seriously, if you need assistance, ask. :)

 

:cheers:

Wonko



#4 Rootman

Rootman

    Frequent Member

  • Advanced user
  • 382 posts
  • Location:USA

Posted 28 March 2013 - 11:04 PM

Wonko, I now understand what you meant,  I was looking at it wrong. I THINK I've sussed it out though, and some of the
script IS wrong.

Unfortunately the only way to delete a folders FILES and FOLDERS using RD directly without any tricky scripting is to delete the TEMP folder itself and then recreate it. To delete JUST the TEMP folders CONTENTS ONLY (file and folders) WITHOUT  destroying the TEMP folder itself is more difficult. Unfortunately too RD will not take the *.* parameter, so you have to be more creative.

In the batch file the DEL deletes all the files, leaving only the SUBFOLDERS in the TEMP folder for the RD lines tricky script to parse - and you're right, the /S for the DEL command is unnecessary as the next RD command deletes the subfolders AND their files:

for /D %%a in (*.*) do FOR /D %%b IN ("%%a\AppData\Local\Temp\*.*") DO RD /S /Q "%%b" >nul  

The above parses each subfolder name of USERNAME\AppData\Local\Temp\SUBFOLDER and issues the RD /S to delete them all without deleting the TEMPORARY INTERNET FILES folder..

In reality does it make a difference as I want to clean up the subfolders (and SUB-subfolders) too.  So DEL /S or RD /S - is
there any real difference?  Maybe the RD /s is quicker because it's not dealing with the possible hundreds or thousands of
filenames?  I am not sure. Maybe doing the RD first THEN the DEL will solve it as that all that will be left is the files in teh immediate TEMP folder.

So the *.* will not do *. too?  Will the RD lines *.* match FOLDERS without an extension too?  I may have to repeat each
line, once for *.* and once for *   .


This is becoming a freaking mess. Damn MS and it's crappy batch language :( .

It may be easier just to delete and recreate the temp folders.  Question is, will this screw up the NTFS permissions on the
folders as well?



#5 Rootman

Rootman

    Frequent Member

  • Advanced user
  • 382 posts
  • Location:USA

Posted 29 March 2013 - 03:05 AM

Ok, first post edited for a new version 2, redirection to suppress output and corrected syntax.  Works well on my system, script is well documented internally.



#6 DarkPhoeniX

DarkPhoeniX

    Frequent Member

  • Team Reboot
  • 452 posts
  • Location:In the middle of nowhere
  • Interests:Interesting Things
  •  
    South Africa

Posted 29 March 2013 - 09:59 AM

You must enter the REAL Windows OS drive a the prompt or command line, Wonko came up with a brilliant script to determine the REAL OS drive, but alas, I am too stupid to figure out how to combine it with this script.  Check it out

 

This is from a old script that has not failed me to date:

 

@echo off
Title Snapshot 2.2 Loader
echo Snapshot 2.2 Loader                               By DarkPhoeniX --- KGB Systems
echo --------------------------------------------------------------------------------
echo Credits:
echo Purrint.exe - TSR AngelDust
echo FreeImage
echo Original icons - Hide Itoh
Echo New icons from "On the Rain-Slick Precipice of Darkness - Episode 1"
echo --------------------------------------------------------------------------------

echo Change Directory to Target
cd "x:\Program Files\SnapShot"

echo Find windows Root

SET WinDrive=C

FOR %%? IN ( C D E F G H I J K L M N O P Q R S T U V W Y Z ) DO (
IF EXIST %%?:\Windows\Explorer.exe (
SET WinDrive=%%?
GOTO :FOUND
)
)

FOR %%? IN ( C D E F G H I J K L M N O P Q R S T U V W Y Z ) DO (
IF EXIST %%?:\Windows\System32\ping.exe (
SET WinDrive=%%?
GOTO :FOUND
)
)

FOR %%? IN ( C D E F G H I J K L M N O P Q R S T U V W Y Z ) DO (
IF EXIST %%?:\Windows\System32\hall.dll (
SET WinDrive=%%?
GOTO :FOUND
)
)

FOR %%? IN ( C D E F G H I J K L M N O P Q R S T U V W Y Z ) DO (
IF EXIST %%?:\boot.ini (
SET WinDrive=%%?
GOTO :FOUND
)
)

FOR %%? IN ( C D E F G H I J K L M N O P Q R S T U V W Y Z ) DO (
IF EXIST %%?:\pagefile.sys (
SET WinDrive=%%?
GOTO :FOUND
)
)
GOTO :notfound

:FOUND
Echo Windows Found on %WinDrive%:\
goto :readINF

:notfound
echo Not found, Using Fail Save drive %WinDrive%:\
goto :readINF

:readINF
echo Reading Purrint.ini
for /f "skip=1 tokens=2 delims==" %%a in ('find "FileName=" Purrint.ini') do set FileName=%%a
echo FileName %FileName%

Echo Adding Regestry Keys
regedit.exe /s Purrint.reg
nircmd regsetval sz "HKEY_USERS\.DEFAULT\Software\Angeldust\Purrint" "Filename" "%WinDrive%:\%FileName%" 
Echo Starting Purrint.exe
nircmd exec show "%cd%\purrint.exe"

::Please note that this file dose not get run at startup
::This is only a sorce file that was converted to .exe with "Bat To Exe Converter v1.4.1"
::iow. this is only a ref. to the .exe file changes to this file will result in nothing!

just cut out what you need...

Im using Spybot2 to remove tempfiles with a PE,but im not done with the script to upload it here as of yet...



#7 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 29 March 2013 - 11:19 AM

@Darkphoenix

Just so you know your batch suffers of the same issues I hinted on the other thread when you have one of those multicard readers.

The IF EXIST on such (empty) drives will cause a popup to appear.

There is a way out by using DIR (which does not provoke that) check a few posts starting from here:

http://www.msfn.org/...post__p__895119

 

The real issue with any "automatic" or "smart" script is taking care of the "rare" cases or of the "exceptions. :ph34r:

Your batch assumes that the %Winroot% directory is actually called "Windows", but this may not always be the case, the "Recovery Console like" check was most probably developed when it was common to have it named "WinNT" (and I do have a few machines where I have Windows 2000 installed to "Win2K" ;)).

 

 

@Rootman

I take back :blush: the note about *.* not matching files without extension (I just checked and it does work).

 

As a matter of fact with del /s even the single asterisk will do, so we can save two bytes.

 

Either I am getting old and forgetful (very possible) or the use of wildcard has changed and I didn't notice (also very possible) :(.

 

This said, if the scope is to delete all the subtree but not the "root" directory (and thus have to re-create it with possible permission/ownership issues), inverting the commands as you suggested :worship: is most probably the ideal (fastest) solution.

First RD any and all subdrectories, and then plainly delete what is left (only files in the root). :thumbup:

 

:cheers:

Wonko



#8 Rootman

Rootman

    Frequent Member

  • Advanced user
  • 382 posts
  • Location:USA

Posted 29 March 2013 - 02:23 PM

I suspected the * vs *.* wasn't an issue but didn't take the time to ensure that it worked on ALL commands, DEL, RD, ICACLS and ATTRIB, so I just doubled up.  I'll have to test each statement to ensure it works, till then it's safe, albeit slower using BOTH.  For now it does what I want it to do - clean up the cruft so I can image a drive, or cleans it up to reduce defrag time and general housekeeping.

 

Thanks for your help.



#9 DarkPhoeniX

DarkPhoeniX

    Frequent Member

  • Team Reboot
  • 452 posts
  • Location:In the middle of nowhere
  • Interests:Interesting Things
  •  
    South Africa

Posted 29 March 2013 - 03:35 PM

@Darkphoenix

Just so you know your batch suffers of the same issues I hinted on the other thread when you have one of those multicard readers.

The IF EXIST on such (empty) drives will cause a popup to appear.

There is a way out by using DIR (which does not provoke that) check a few posts starting from here:

http://www.msfn.org/...post__p__895119

 

The real issue with any "automatic" or "smart" script is taking care of the "rare" cases or of the "exceptions. :ph34r:

Your batch assumes that the %Winroot% directory is actually called "Windows", but this may not always be the case, the "Recovery Console like" check was most probably developed when it was common to have it named "WinNT" (and I do have a few machines where I have Windows 2000 installed to "Win2K" ;)).

 

 

@Rootman

I take back :blush: the note about *.* not matching files without extension (I just checked and it does work).

 

As a matter of fact with del /s even the single asterisk will do, so we can save two bytes.

 

Either I am getting old and forgetful (very possible) or the use of wildcard has changed and I didn't notice (also very possible) :(.

 

This said, if the scope is to delete all the subtree but not the "root" directory (and thus have to re-create it with possible permission/ownership issues), inverting the commands as you suggested :worship: is most probably the ideal (fastest) solution.

First RD any and all subdrectories, and then plainly delete what is left (only files in the root). :thumbup:

 

:cheers:

Wonko

This is a old script but i have not had a problem with this to date,

The multicard reader problem was solved in the ESET loader a long time ago using DIR just have a look at that script



#10 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 29 March 2013 - 03:45 PM

This is a old script but i have not had a problem with this to date,

The multicard reader problem was solved in the ESET loader a long time ago using DIR just have a look at that script

Yep :),  that's more or less the meaning of "rare" condition, I do know that 99.99% of Windows installs are in a "\Windows" directory ;).

Happy you already know about the possible issue, but the fact that in another script you already have a "more accurate" provision does not mean that running this script, it will not choke on a system with multicard reader.

 

:cheers:
Wonko



#11 DarkPhoeniX

DarkPhoeniX

    Frequent Member

  • Team Reboot
  • 452 posts
  • Location:In the middle of nowhere
  • Interests:Interesting Things
  •  
    South Africa

Posted 29 March 2013 - 06:06 PM

@Wonko

the other script is a bit complicted

This one is not

 

But nice to see you posting again, any way

 

I'm trying to learn Python for the past 2 weeks and that is why i dont post a lot, for now






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users