3

Basically, we have numerous customers running XP and 7 with a few Vista machines.

I've found this batch script, but it is limited to the current user (uses the %userprofile% environment variable). I'm looking for something like this, but which would run for all user accounts on the computer. The script would be run as administrator.

For XP, it would delete the contents of:
Local Settings\Temp
Local Settings\Temporary Internet Files

For Vista/7, it would delete the contents of:
AppData\Local\Temp
AppData\Local\Microsoft\Windows\Temporary Internet Files

I am relatively inexperienced with scripting, and I'm not sure if a batch file can do this. Has anyone gone down this path and found a solution?

Chris
  • 39
  • 1
  • 1
  • 3
  • 2
    Are you on a domain using AD? If so you can integrate it into their logoff script to go through and clear those files using the %USERPROFILE% variable. – Split71 Jan 10 '12 at 18:14
  • Beware when wiping out temp files -- Make sure they're old (>30 days is pretty much a standard in the Unix world) so you don't accidentally blow away something somebody needs. – voretaq7 Jan 11 '12 at 07:21
  • @voretaq7 Just curious but why add the Windows-7 Tag over the XP and Vista tags? OR not add all 3? – MaskedPlant Jan 11 '12 at 15:07
  • @MaskedPlant no particular reason - it was like 3 AM and it made sense at the time :-) – voretaq7 Jan 11 '12 at 16:00
  • @voretaq7 ok cool. I'm pretty new and just trying to learn from super users like you. 3AM makes plenty of sense. – MaskedPlant Jan 11 '12 at 16:22
  • @Split71 We are using AD for most customers. Logoff script is a good idea, but it would not delete temp files remaining for users who no longer touch a particular computer. We're aiming to run this script every few months (with warning to users not to store data in temp folders). – Chris Jan 12 '12 at 17:46

3 Answers3

6

I've used this to some success. You may need to edit it for your environment, but for me it works for XP Vista and 7. Couple of things, make sure it runs at a time with the least impact, and understand that it is as intrusive as you can really get, since it removes the folders and re creates them. You could change the rmdir to del /f and add a \ to the end of the file paths and then remove the mkdir line if you would prefer to not remove the folders and just delete the contents.

This DELETES a ton of stuff, use at your own risk.

@echo off

IF EXIST c:\windows\temp\ del /f /s /q c:\windows\temp\

DEL /f /s /q %temp%\

IF EXIST "C:\Documents and Settings\" (
    for /D %%x in ("C:\Documents and Settings\*") do ( 
        rmdir /s /q "%%x\Local Settings\Temporary Internet Files" 
        mkdir "%%x\Local Settings\Temporary Internet Files" 
    )
)

IF EXIST "C:\Documents and Settings\" (
    for /D %%x in ("C:\Documents and Settings\*") do ( 
        rmdir /s /q "%%x\Local Settings\Temp" 
        mkdir "%%x\Local Settings\Temp" 
    )
)

IF EXIST "C:\Users\" (
    for /D %%x in ("C:\Users\*") do ( 
        rmdir /s /q "%%x\AppData\Local\Temp" 
        mkdir "%%x\AppData\Local\Temp" 
    )
)

IF EXIST "C:\Users\" (
    for /D %%x in ("C:\Users\*") do ( 
        rmdir /s /q "%%x\AppData\Local\Microsoft\Windows\Temporary Internet Files" 
        mkdir "%%x\AppData\Local\Microsoft\Windows\Temporary Internet Files" 
    )
)

Note this separates out the different folders, mostly for clarity but if you wanted to condense it you could compress it to only 2 loops. An example would be:

IF EXIST "C:\Users\" (
    for /D %%x in ("C:\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" 
    )
)

Per request, compressed and using delete command.

@echo off

IF EXIST c:\windows\temp\ del /f /s /q c:\windows\temp\

DEL /f /s /q %temp%\

IF EXIST "C:\Users\" (
    for /D %%x in ("C:\Users\*") do ( 
        del /f /s /q "%%x\AppData\Local\Temp\" 
        del /f /s /q "%%x\AppData\Local\Microsoft\Windows\Temporary Internet Files\" 
    )
)

IF EXIST "C:\Documents and Settings\" (
    for /D %%x in ("C:\Documents and Settings\*") do ( 
        del /f /s /q "%%x\Local Settings\Temp\" 
        del /f /s /q "%%x\Local Settings\Temporary Internet Files\" 
    )
)
MaskedPlant
  • 415
  • 1
  • 3
  • 8
  • Awesome - this is exactly what I was looking for. I'm aware that it wipes out a lot of files. I'll try it on a few machines and report back with results. – Chris Jan 12 '12 at 17:42
  • I will modify this to delete folder contents rather than re-create folders, because we're running the script as administrator. I don't want to create folders with improper default permissions and break things as a result. – Chris Jan 12 '12 at 17:54
-2

cd C:\Users\%username%\AppData\Local rmdir /S /Q Temp

del C:\Windows\Prefetch*.* /Q

del C:\Windows\Temp*.* /Q

del C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Recent Items*.* /Q pause

-2

For me this commands works fine, try this, it will delete all temp files

cd \
Del *.tmp /s
Sven
  • 97,248
  • 13
  • 177
  • 225