Cleanup Windows & Java Temporary Files using a Batch Script

Recently, there was a breakout of malware tricking people to install a fake “anti-virus” software, which in turn infected their machines. Mind you, the primary reason machines get infected in the first place is because people are browsing dodgy websites so BE CAREFUL or get PROTECTED!

The infected files are usually downloaded to a temporary internet files folder or java temporary folder and are executed/installed from there. It’s always good practice to clear out these temporary files to prevent malicious activity as well as save you some disk space. This batch script can be used as a scheduled task or deployed through your favourite monitoring tool to “cleanup” these temporary files.

This script should work on all Windows platforms so happy cleaning 🙂

Delete All Temporary Files

Note: copy and paste the below code into notepad and save as the text file as a .bat file. Change the “Save as type” to All Files and make sure you add a .bat to the end of the name e.g. Cleanup.batThere is a tutorial post located here.

@echo off
::Written by Samontech
::Determine which version of Windows is installed
::Set Variable Version to unknown

SET Version=Unknown

VER | FINDSTR /IL “5.0” > NUL
IF %ERRORLEVEL% EQU 0 SET Version=”Windows 2000″

VER | FINDSTR /IL “5.1.” > NUL
IF %ERRORLEVEL% EQU 0 SET Version=”Windows XP”

VER | FINDSTR /IL “5.2.” > NUL
IF %ERRORLEVEL% EQU 0 SET Version=”Windows 2003″

VER | FINDSTR /IL “6.0.” > NUL
IF %ERRORLEVEL% EQU 0 SET Version=”Windows Vista”

VER | FINDSTR /IL “6.1.” > NUL
IF %ERRORLEVEL% EQU 0 SET Version=”Windows 7″

VER | FINDSTR /IL “6.2.” > NUL
IF %ERRORLEVEL% EQU 0 SET Version=”Windows 8″

VER | FINDSTR /IL “6.3.” > NUL
IF %ERRORLEVEL% EQU 0 SET Version=”Windows 8.1″

ECHO The version of Windows found is %VERSION%

IF %VERSION% == “Windows XP” GOTO XP
IF %VERSION% == “Windows 2000” GOTO XP
IF %VERSION% == “Windows 2003” GOTO XP
IF %VERSION% == “Windows Vista” GOTO WIN7
IF %VERSION% == “Windows 7” GOTO WIN7
IF %VERSION% == “Windows 8” GOTO WIN7
IF %VERSION% == “Windows 8.1” GOTO WIN7

::If no versions are found go to UNKNOWN
GOTO UNKNOWN

:WIN7
ECHO WIN7 script to execute
cd /D C:\users
ECHO Clean Temp Folder
for /D %%a in (*.*) do DEL /F /S /Q “%%a\AppData\Local\Temp\*.*”
for /D %%a in (*.*) do FOR /D %%b IN (“%%a\AppData\Local\Temp\*.*”) DO RMDIR /S /Q “%%b”
ECHO Clean IE Cache
for /D %%a in (*.*) do DEL /F /S /Q “%%a\AppData\Local\Microsoft\Windows\Temporary Internet Files\*.*”
for /D %%a in (*.*) do FOR /D %%b IN (“%%a\AppData\Local\Microsoft\Windows\Temporary Internet Files\*.*”) DO RMDIR /S /Q “%%b”
ECHO Clean JAVA Cache
for /D %%a in (*.*) do DEL /F /S /Q “%%a\AppData\LocalLow\Sun\Java\Deployment\cache\*.*”
for /D %%a in (*.*) do FOR /D %%b IN (“%%a\AppData\LocalLow\Sun\Java\Deployment\cache\*.*”) DO RMDIR /S /Q “%%b”

GOTO FINISH

:XP
ECHO XP script to execute
cd /D “C:\Documents and Settings”
ECHO Clean Temp Folder
for /D %%a in (*.*) do DEL /F /S /Q “%%a\Local Settings\Temp\*.*”
for /D %%a in (*.*) do FOR /D %%b IN (“%%a\Local Settings\Temp\*.*”) DO RMDIR /S /Q “%%b”
ECHO Clean IE Cache
for /D %%a in (*.*) do DEL /F /S /Q “%%a\Local Settings\Temporary Internet Files\*.*”
for /D %%a in (*.*) do FOR /D %%b IN (“%%a\Local Settings\Temporary Internet Files\*.*”) DO RMDIR /S /Q “%%b”
ECHO Clean JAVA Cache
for /D %%a in (*.*) do DEL /F /S /Q “%%a\Application Data\Sun\Java\Deployment\cache\*.*”
for /D %%a in (*.*) do FOR /D %%b IN (“%%a\Application Data\Sun\Java\Deployment\cache\*.*”) DO RMDIR /S /Q “%%b”

GOTO FINISH

:FINISH
ECHO Temporary files successfully deleted
GOTO END

:UNKNOWN
ECHO Operating System Unknown

:END

 

Edit: Added Windows 8 paramater 🙂 special thanks to wampbox for that!

Share on Facebook0Share on LinkedIn0Tweet about this on TwitterShare on Google+0Email this to someone
Posted in Scripting, Terminal Server, Windows, Windows Server and tagged , , , , , , , , , , , , , , , .

15 Comments

  1. Thank you, your script just saved our hospital over 150 tech support calls. I added these lines to both the WIN7 and XP sections kill Java first so it clears all of the Java cache even if they had Java running:

    ECHO Killing Java processes
    taskkill /F /IM jusched.exe
    taskkill /F /IM jqs.exe
    taskkill /F /IM javaw.exe
    taskkill /F /IM java.exe

    Thanks again!!

  2. Thank you for this really useful script.

    For users of Windows 8 who wish to use this batch script you need to match the correct version of the OS.

    This is the version for Windows 8:
    Microsoft Windows [Version 6.2.9200]

    So, just include the following to find the correct string in the version call:

    VER | FINDSTR /IL "6.2." > NUL
    IF %ERRORLEVEL% EQU 0 SET Version="Windows 8"

    Next, add the IF statement to match the Windows 8 Label %VERSION%
    ..
    IF %VERSION% == "Windows 8" GOTO WIN8

    Now, create the Windows 8 Code block which will fire up once the correct %VERSION% of windows is matched. — This case WIN8

    :WIN8
    ECHO WIN8 script to execute
    ...
    ...

    * Use the Windows 7 section as a guide but it may need a little tweaking to get it to work for win8.
    One problem I faced was read-only permissions errors on certain directory’s which are targeted by this script.

    You may need to clears some folder attributes:

    ... attrib -R -H

    HTH

    • As Andy suggested, I think it would be best to add it after :WIN7 and :WINXP. Or you could do it right at the beginning if you don’t want to have to many lines in your batch file.

  3. Thanks for the example !! He has very quick answering of calls to technical support in our department.

  4. Pingback: samontech | Batch script to determine 32 or 64 bit Windows Operating Systems and perform an action

  5. This script doesn’t work for me. When ran from Admin shell within Windows 7 64, it just says:
    The version of Windows found is Unknown
    Operating System Unknown.

    What am I missing?

    • Hi Joshua,

      Thanks for the comment. What do you get when you type in “VER” in command prompt? The script is designed to use the VER information outputted from CMD to execute the necessary parts of the script that are relevant to your operating system.

      Thanks!

      • Hello samontech,
        I am having the same issue as Joshua. When I type the VER command it states Microsoft Windows [Version 6.1.7601] and comes up “unknown”. Any help would be greatly appreciated because I find that this script would be very useful for me.

        Thanks,
        Tony

      • When you copy and paste this script, you need to save it as plain text. I tried and got the same thing and had to do it again but save it and make sure all the items in quotes were using standard quote marks.

    • Typically, you won’t need to restart when running the script. The only time you’d need to restart is if there’s an open file and it’s in use. Then you’d re-run the script after a restart. Hope that helps.

Leave a Reply

Your email address will not be published. Required fields are marked *