Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
1  
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 7: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

1 Answer 1

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\" 
    )
)
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.