none
Help creating batch file that will delete all user profile temp files on logoff. RRS feed

  • Question

  • Hi folks. I want to put a batch file together that will delete all files at the user profile in the following directories when my users log off:

    C:\Users\USER\AppData\Local\Temp
    C:\Users\USER\AppData\Local\Microsoft\Windows\Temporary Internet Files
    C:\Windows\Temp

    Help would be appreciated!  Thanks.
    Thursday, October 21, 2010 2:46 PM

Answers

  • A simple del command line may not work, because you do not have access on the temp files of other users. If you still encounter problems after trying this suggestion, I recommend that you discuss it in our Script Center, you may need a VB script that can ignore UAC.

    The Official Scripting Guys Forum! 

    Arthur Xie
    TechNet Subscriber Support in forum  
    If you have any feedback on our support, please contact tngfb@microsoft.com.


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
    Friday, October 22, 2010 6:38 AM
    Moderator

All replies

  • I have one that cleans different temp folders in windows.. like CCleaner but for all users.

    not sure if it only works in XP, but it should be easy to modify if so.. you could run it in sheduled tasks (taskschd.msc)

    @echo off
    
    SET SRC1=C:\Documents and Settings
    SET SRC2=Local Settings\Temporary Internet Files\Content.IE5
    SET SRC3=Local Settings\History
    SET SRC4=Local Settings\Temp
    SET SRC5=Cookies
    
    echo cleaning temporary internet files
    FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC2%\*.*") DO RMDIR /S /Q "%%Y"
    
    echo cleaning history
    FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC3%\*.*") DO RMDIR /S /Q "%%Y"
    
    echo cleaning windows temp files
    FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC4%\*.*") DO RMDIR /S /Q "%%Y"
    FOR /D %%X IN ("%SRC1%\*") DO FOR %%Y IN ("%%X\%SRC3%\*.*") DO DEL /F /S /Q "%%Y"
    FOR /D %%X IN ("%SRC1%\*") DO FOR %%Y IN ("%%X\%SRC4%\*.*") DO DEL /F /S /Q "%%Y"
    
    echo cleaning Cookies
    FOR /D %%X IN ("%SRC1%\*") DO FOR %%Y IN ("%%X\%SRC5%\*.*") DO DEL /F /S /Q "%%Y"
    
    del /f /s /q "%windir%\temp"
    FOR /D %%X IN ("%windir%\temp\*") DO RMDIR /S /Q "%%X"

    Thursday, October 21, 2010 6:27 PM
  • I have one that cleans different temp folders in windows.. like CCleaner but for all users.

    not sure if it only works in XP, but it should be easy to modify if so.. you could run it in sheduled tasks (taskschd.msc)

     

    @echo off
    
    SET SRC1=C:\Documents and Settings
    SET SRC2=Local Settings\Temporary Internet Files\Content.IE5
    SET SRC3=Local Settings\History
    SET SRC4=Local Settings\Temp
    SET SRC5=Cookies
    
    echo cleaning temporary internet files
    FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC2%\*.*") DO RMDIR /S /Q "%%Y"
    
    echo cleaning history
    FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC3%\*.*") DO RMDIR /S /Q "%%Y"
    
    echo cleaning windows temp files
    FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC4%\*.*") DO RMDIR /S /Q "%%Y"
    FOR /D %%X IN ("%SRC1%\*") DO FOR %%Y IN ("%%X\%SRC3%\*.*") DO DEL /F /S /Q "%%Y"
    FOR /D %%X IN ("%SRC1%\*") DO FOR %%Y IN ("%%X\%SRC4%\*.*") DO DEL /F /S /Q "%%Y"
    
    echo cleaning Cookies
    FOR /D %%X IN ("%SRC1%\*") DO FOR %%Y IN ("%%X\%SRC5%\*.*") DO DEL /F /S /Q "%%Y"
    
    del /f /s /q "%windir%\temp"
    FOR /D %%X IN ("%windir%\temp\*") DO RMDIR /S /Q "%%X"

     

    WOW.  Does it really have to be that complicated? Can't it be done using the del command?
    Thursday, October 21, 2010 7:17 PM
  • A simple del command line may not work, because you do not have access on the temp files of other users. If you still encounter problems after trying this suggestion, I recommend that you discuss it in our Script Center, you may need a VB script that can ignore UAC.

    The Official Scripting Guys Forum! 

    Arthur Xie
    TechNet Subscriber Support in forum  
    If you have any feedback on our support, please contact tngfb@microsoft.com.


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
    Friday, October 22, 2010 6:38 AM
    Moderator
  • Thank you so much! It works for me. :)
    Wednesday, December 1, 2010 12:20 PM
  • I use this script found here:

    http://notes.ponderworthy.com/clean-up-all-system-and-user-profile-temp-folders

    Monday, June 2, 2014 9:56 PM
  • I know that this is an old post, this script works wonders.

    Thank you!!!

    Friday, November 22, 2019 7:53 PM