7

I'm looking for a good batch script that would quickly find & clean all the known safe temporary folders/files from Windows (as many variants as possible) machines (e.g. the windows temp folder, all users IE temp folders, etc.). I'm fond of UI tools like CCleaner (over Cleanmgr.exe), but when I'm trying to clean several computers quickly and/or with minimal involvement, it would be nice to have a script.

Plus with a script, I could chain several scripts together. Maybe one to then fire up various antivirus and/or malware detectors.

Anyone have a good one or can point to a good resource?

1

6 Answers 6

7

Here's a little script I wrote that I keep on my USB utility drive... GREAT for citrix servers :)

@echo off
Echo Started %time%
Echo Started %time% >> temps.txt
Echo Running for XP... >> temps.txt
Echo Running for XP...
FOR /F "tokens=*" %%G IN ('DIR /B /AD') DO IF EXIST "%%G\Local Settings\Temp\" (
    RMDIR /S /Q "%%G\Local Settings\Temp"
    MKDIR "%%G\Local Settings\Temp"
    Echo Cleared %%G\Local Settings\Temp
    Echo Cleared %%G\Local Settings\Temp >> temps.txt
)
FOR /F "tokens=*" %%G IN ('DIR /B /AD') DO IF EXIST "%%G\Local Settings\Temporary Internet Files\" (
    RMDIR /S /Q "%%G\Local Settings\Temporary Internet Files\"
    MKDIR "%%G\Local Settings\Temporary Internet Files\"
    Echo Cleared %%G\Local Settings\Temporary Internet Files\
    Echo Cleared %%G\Local Settings\Temporary Internet Files\ >> temps.txt
)
Echo Done.
Echo Running for Vista >> temps.txt
Echo Running for Vista...
FOR /F "tokens=*" %%G IN ('DIR /B /AD') DO IF EXIST "%%G\AppData\Local\Temp\" (
    RMDIR /S /Q "%%G\AppData\Local\Temp\"
    MKDIR "%%G\AppData\Local\Temp\"
    Echo Cleared %%G\AppData\Local\Temp\
    Echo Cleared %%G\AppData\Local\Temp\ >> temps.txt
)
FOR /F "tokens=*" %%G IN ('DIR /B /AD') DO IF EXIST "%%G\AppData\Local\Microsoft\Windows\Temporary Internet Files\" (
    RMDIR /S /Q "%%G\AppData\Local\Microsoft\Windows\Temporary Internet Files\"
    MKDIR "%%G\AppData\Local\Microsoft\Windows\Temporary Internet Files\"
    Echo Cleared %%G\Local Settings\Temporary Internet Files\
    Echo Cleared %%G\Local Settings\Temporary Internet Files\ >> temps.txt
)
Echo Done.
Echo Ended %time%
Echo Ended %time% >> temps.txt

Use at your own risk, etc... THIS DELETES STUFF.

I run this from the Documents and Settings folder or Users on Vista. You could easily throw a CD command up top to run this from anywhere you want.

cd %userprofile%
cd ..

Also, it's generally safe to clear the temp folder at any time in my experience. Programs using files in temp will lock them, and this script will error on that file and keep going.

The IF EXIST line is particularly nice here, it keeps the script from creating folders in NetworkService and similar folders, and if you aren't running it on Vista or XP that section goes by super fast.

Creates temps.txt logfile where ever you run the script from

Edit: Advice from ##windows-server on Freenode: Q: Why don't you detect which OS it's running on in the begining and run the appropriate section? A: I use the script frequently on offline media, such as a hard drive that's been extracted.

2
  • if there's lock issue I'm pretty sure that the command will end without deleting the others non lock files.
    – Hapkido
    Jun 6, 2009 at 9:50
  • Not when using rd /q /s Here's a log: Temp\~DF27D9.tmp - The process cannot access the file because it is being used b y another process. Temp\~DFDBF4.tmp - Access is denied.
    – Garrett
    Jun 12, 2009 at 21:14
3

Be careful about trashing temporary files. Some software installers stash files there between boots. An old boss of mine stored important files there (but, then, he wasn't too bright about some things).

In a batch file, do:

DEL /S /Q "%TEMP%\*.*"

Will do what you want. Running that as a logon-script (or while logged-on, in general) will clear the per-user temp directory. Running it as a startup script (while running as as .DEFAULT) will clear the per-machine temp directory.

I have a VBScript that I run to clean out temporary files on boot and on logon based on their age. It's something that belongs to a Customer so I can't post it here (wrote it on their dime), but it's something that a scripter could put together in a few minutes for you.

I'm not aware of a supported API to clean out the IE "Temporary Internet Files". You could just delete them, I suppose, but I wouldn't.

1
  • I will also add the /F parameter in case there's any read only file.
    – Hapkido
    Jun 6, 2009 at 9:51
3

The best safe way to clean up the temp directories is with cleanmgr. The downside is that you have to either run sageset on each machine or (and far easier) is to build an ADM file with the registry entries and use group policy to push them out to client desktops. the registry key to look at is: HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches

See also How to Automate the Disk Cleanup Tool in Windows XP

1
  • (+1 if I had the rep) thanks, the adm file is a good tip for machines that i can push it out too otherwise i wasn't really considering cleanmgr for the 1st reason you mentioned. Jun 4, 2009 at 16:44
1

You're all making it way too hard. This is Windows XP he's asking about. Here's some useful ones:

DEL /Q /S "%USERPROFILE%\Local Settings\Temp\*.*"
DEL /F /Q /S "%USERPROFILE%\Local Settings\Temporary Internet Files\*.*" >NUL
DEL /F /Q /S "%WinDir%\Temp\*.*"
0

Why not just use Ccleaner in your script, since it already has all the functions you need. And then use Psexec to push your script, which calls Ccleaner, and then delete Ccleaner when done. You can add a .ini file that contains all of the settings you want in Ccleaner. This way, u don't have to reinvent the wheel. It's all about Re-Use.

0

I'd rather delete the files than the folder on XP; because we have an application that stores LOTS of temp-files and leave them there. I do want to delete the files in all user profiles.

FOR /F "tokens=1" %%G IN ('DIR /B /AD "c:\documents and settings"') DO IF EXIST "c:\documents and settings\%%G\Local Settings\Temp\" (del "c:\documents and settings\%%G\local settings\temp\*.*"/Q)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .