Remove temporary files from all profiles via script

Reading Time: < 1 minute

The below script will remove temporary internet files and temp files from user profiles located on a Windows PC or server.

Copy the below code and rename with a .bat extension. e.g. remove.bat

Please test on a PC before providing to a live environment.

REM @echo off
REM – This script searches through profiles and removes temporary internet files and temp files.

set HDRIVE=c:
set HPATH=Documents and Settings

%HDRIVE%
cd \%HPATH%
REM – This line sets up the loop for the script
for /d %%i in (*) do call :ifthen %%i

goto end

REM – The below command checks for the temporary internet files directory.
:ifthen

 if exist “%HDRIVE%\%HPATH%\%1\Local Settings\Temporary Internet Files\Content.IE5” call :ContentDelete1 %1
 if exist “%HDRIVE%\%HPATH%\%1\windows\Temporary Internet Files\Content.IE5” call :ContentDelete2 %1
 if exist “%HDRIVE%\%HPATH%\%1\Local Settings\temp” call :TempDelete %1
goto :EOF
REM – The below code removes all folders located at ‘Profile\Local Settings\Temporary Internet Files\Content.IE5’
REM – It then returns back to the line that it was called from.
:ContentDelete1

 cd “%HDRIVE%\%HPATH%\%1\Local Settings\Temporary Internet Files\Content.IE5”
 if exist “%HDRIVE%\%HPATH%\%1\Local Settings\Temporary Internet Files\Content.IE5” for /d %%n in (*) do rd /s /q “%%n”
 if exist “%HDRIVE%\%HPATH%\%1\Local Settings\Temporary Internet Files\Content.IE5” del /q “%HDRIVE%\%HPATH%\%1\Local Settings\Temporary Internet Files\Content.IE5\*”
 goto :EOF

REM – The below code removes all folders located at ‘windows\Temporary Internet Files\Content.IE5’
:ContentDelete2
 cd “%HDRIVE%\%HPATH%\%1\windows\Temporary Internet Files\Content.IE5”
 if exist “%HDRIVE%\%HPATH%\%1\windows\Temporary Internet Files\Content.IE5” for /d %%n in (*) do rd /s /q “%%n”
 if exist “%HDRIVE%\%HPATH%\%1\windows\Temporary Internet Files\Content.IE5” del /q “%HDRIVE%\%HPATH%\%1\windows\Temporary Internet Files\Content.IE5\*”

goto :EOF

REM – The below code removes all folders located at ‘windows\Temporary Internet Files\Content.IE5’
:TempDelete
 cd “%HDRIVE%\%HPATH%\%1\Local Settings\Temp”
 if exist “%HDRIVE%\%HPATH%\%1\Local Settings\Temp” for /d %%n in (*) do rd /s /q “%%n”
 if exist “%HDRIVE%\%HPATH%\%1\Local Settings\Temp” del /q “%HDRIVE%\%HPATH%\%1\Local Settings\Temp\*”

goto :EOF

:end

Please follow and like us:

5 thoughts on “Remove temporary files from all profiles via script

  1. Thankyou for a working script.
    I have looked all over but the ones i find never work.

    Worked first time round, thankyou

  2. This informal summary assited me very much! Bookmarked your blog, extremely great topics everywhere that I read here! I really appreciate the information, thank you.

  3. Great script!!!!

    I took your script and modified this for windows 7 if anyone is interested. Add more directories in the script if you wish. This is better than ccleaner because it goes through all the profiles rather than the one you are logged into

    REM @echo off
    REM – This script searches through profiles and removes temporary internet files and temp files.

    set HDRIVE=c:
    set HPATH=Users

    %HDRIVE%
    cd \%HPATH%
    REM – This line sets up the loop for the script
    for /d %%i in (*) do call :ifthen %%i

    goto end

    REM – The below command checks for the temporary internet files directory.
    :ifthen

    if exist “%HDRIVE%\%HPATH%\%1\AppData\Local\Microsoft\Windows\Temporary Internet Files” call :ContentDelete1 %1
    if exist “%HDRIVE%\%HPATH%\%1\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5” call :ContentDelete2 %1
    if exist “%HDRIVE%\%HPATH%\%1\AppData\Local\Microsoft\Windows\Temporary Internet Files\Low\Content.IE5” call :ContentDelete3 %1
    if exist “%HDRIVE%\%HPATH%\%1\AppData\Local\Temp” call :ContentDelete4 %1

    goto :EOF

    :ContentDelete1

    cd “%HDRIVE%\%HPATH%\%1\AppData\Local\Microsoft\Windows\Temporary Internet Files”
    if exist “%HDRIVE%\%HPATH%\%1\AppData\Local\Microsoft\Windows\Temporary Internet Files” del /q /f “%HDRIVE%\%HPATH%\%1\AppData\Local\Microsoft\Windows\Temporary Internet Files\*”
    goto :EOF

    :ContentDelete2

    cd “%HDRIVE%\%HPATH%\%1\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5”
    if exist “%HDRIVE%\%HPATH%\%1\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5” for /d %%n in (*) do rd /s /q “%%n”
    if exist “%HDRIVE%\%HPATH%\%1\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5” del /q /s /f “%HDRIVE%\%HPATH%\%1\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\*”
    goto :EOF

    :ContentDelete3

    cd “%HDRIVE%\%HPATH%\%1\AppData\Local\Microsoft\Windows\Temporary Internet Files\Low\Content.IE5”
    if exist “%HDRIVE%\%HPATH%\%1\AppData\Local\Microsoft\Windows\Temporary Internet Files\Low\Content.IE5” for /d %%n in (*) do rd /s /q “%%n”
    if exist “%HDRIVE%\%HPATH%\%1\AppData\Local\Microsoft\Windows\Temporary Internet Files\Low\Content.IE5” del /q /s /f “%HDRIVE%\%HPATH%\%1\AppData\Local\Microsoft\Windows\Temporary Internet Files\Low\Content.IE5\*”
    goto :EOF

    :ContentDelete4

    cd “%HDRIVE%\%HPATH%\%1\AppData\Local\Temp”
    if exist “%HDRIVE%\%HPATH%\%1\AppData\Local\Temp” for /d %%n in (*) do rd /s /q “%%n”
    if exist “%HDRIVE%\%HPATH%\%1\AppData\Local\Temp” del /q /s /f “%HDRIVE%\%HPATH%\%1\AppData\Local\Temp\*”
    goto :EOF

    :end

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.