Script to delete Windows temp files via Scheduled Tasks

Home Forums Scripting General Scripting Script to delete Windows temp files via Scheduled Tasks

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    j0shu4
    Member
    #154426

    Ok, I’ve been beating my head against this all day long. I’ve run across some good info here, which is why I joined and I’m posting, but it’s not working right.

    What I’m wanting to do is write a script that deletes all temp files, store that script in a share, then schedule that script to run via Scheduled Tasks. My problems are:

    1. When storing the script in a share, Scheduled Tasks says that it ran with error code 0, but nothing is ever deleted. When running the script locally, it deletes most temp files just fine.
    2. My script will not delete the files under %userprofile%Local SettingsTemporary Internet Files.

    Here is my script:

    @echo
    off
    CLS
    cd
    del c:temp*.* /F /S /Q
    del c:windowstemp*.* /F /S /Q
    cd %USERPROFILE%
    del cookies*.* /F /S /Q
    del recent*.* /F /S /Q
    del temp*.* /F /S /Q
    attrib -s -h -r “Local SettingsHistory”
    del “Local SettingsHistory*.*” /F /S /Q
    attrib -s -h -r “Local SettingsTemporary Internet Files”
    del “Local SettingsTemporary Internet Files*.*” /F /S /Q
    del “Local SettingsTemp*.*” /F /S /Q[/CODE]The error it gives on the TIF folder states that index.dat is in use. Since it can’t delete that file it doesn’t delete the others.

    So I decided to use my Google-fu to see what I could find and ran across this, petri.co.il/forums/showthread.php?t=20755, which is awesome. The problem is this doesn’t delete the contents of the TIF folder either and gives the same error. I’ve modified the code to this:
    [CODE]::Set Search directory to “Documents and Settings” folder
    (Set Target=%AllUsersProfile:~0,-10%)

    ::title,Finding the Temp subfolders in %Target%&COLOR 9E
    title,Finding the Temp subfolders in %Target%

    If EXIST “%Target%”,(
    For /f “Tokens=*” %%* in (‘dir “%Target%” /B’) Do (
    cd/D “%target%%%*Local SettingsTemp” && (
    ATTRIB -S -H -R -A /D /S >nul & (
    For /f “Tokens=*” %%* in (‘dir /B’) Do (
    RD “%%*” /S /Q ||Del /F “%%*” )))>nul)
    )

    If EXIST “%Target%”,(
    For /f “Tokens=*” %%* in (‘dir “%Target%” /B’) Do (
    cd/D “%target%%%*Local SettingsTemporary Internet Files” && (
    ATTRIB -S -H -R -A /D /S >nul & (
    For /f “Tokens=*” %%* in (‘dir /B’) Do (
    RD “%%*” /S /Q ||Del /F “%%*” )))>nul)
    )[/CODE]I really don’t care which script I use. I prefer the 2nd one because it gets all profiles. I also don’t have to store the script on the network; it would be nice though. I can always copy it out and then setup the scheduled task from there.

    Thanks for reading, and thanks in advance for any help you can provide.[CODE]@echo off
    CLS
    cd
    del c:temp*.* /F /S /Q
    del c:windowstemp*.* /F /S /Q
    cd %USERPROFILE%
    del cookies*.* /F /S /Q
    del recent*.* /F /S /Q
    del temp*.* /F /S /Q
    attrib -s -h -r “Local SettingsHistory”
    del “Local SettingsHistory*.*” /F /S /Q
    attrib -s -h -r “Local SettingsTemporary Internet Files”
    del “Local SettingsTemporary Internet Files*.*” /F /S /Q
    del “Local SettingsTemp*.*” /F /S /Q[/CODE]The error it gives on the TIF folder states that index.dat is in use. Since it can’t delete that file it doesn’t delete the others.

    So I decided to use my Google-fu to see what I could find and ran across this, petri.co.il/forums/showthread.php?t=20755, which is awesome. The problem is this doesn’t delete the contents of the TIF folder either and gives the same error. I’ve modified the code to this:
    ::Set Search directory to “Documents and Settings” folder
    (Set Target=%AllUsersProfile:~0,-10%)

    ::title,Finding the Temp subfolders in %Target%&COLOR 9E
    title,Finding the Temp subfolders in %Target%

    If EXIST “%Target%”,(
    For /f “Tokens=*” %%* in (‘dir “%Target%” /B’) Do (
    cd/D “%target%%%*Local SettingsTemp” && (
    ATTRIB -S -H -R -A /D /S >nul & (
    For /f “Tokens=*” %%* in (‘dir /B’) Do (
    RD “%%*” /S /Q ||Del /F “%%*” )))>nul)
    )

    If EXIST “%Target%”,(
    For /f “Tokens=*” %%* in (‘dir “%Target%” /B’) Do (
    cd/D “%target%%%*Local SettingsTemporary Internet Files” && (
    ATTRIB -S -H -R -A /D /S >nul & (
    For /f “Tokens=*” %%* in (‘dir /B’) Do (
    RD “%%*” /S /Q ||Del /F “%%*” )))>nul)
    )[/CODE]I really don’t care which script I use. I prefer the 2nd one because it gets all profiles. I also don’t have to store the script on the network; it would be nice though. I can always copy it out and then setup the scheduled task from there.

    Thanks for reading, and thanks in advance for any help you can provide.[CODE]::Set Search directory to “Documents and Settings” folder
    (Set Target=%AllUsersProfile:~0,-10%)

    ::title,Finding the Temp subfolders in %Target%&COLOR 9E
    title,Finding the Temp subfolders in %Target%

    If EXIST “%Target%”,(
    For /f “Tokens=*” %%* in (‘dir “%Target%” /B’) Do (
    cd/D “%target%%%*Local SettingsTemp” && (
    ATTRIB -S -H -R -A /D /S >nul & (
    For /f “Tokens=*” %%* in (‘dir /B’) Do (
    RD “%%*” /S /Q ||Del /F “%%*” )))>nul)
    )

    If EXIST “%Target%”,(
    For /f “Tokens=*” %%* in (‘dir “%Target%” /B’) Do (
    cd/D “%target%%%*Local SettingsTemporary Internet Files” && (
    ATTRIB -S -H -R -A /D /S >nul & (
    For /f “Tokens=*” %%* in (‘dir /B’) Do (
    RD “%%*” /S /Q ||Del /F “%%*” )))>nul)
    )[/CODE]I really don’t care which script I use. I prefer the 2nd one because it gets all profiles. I also don’t have to store the script on the network; it would be nice though. I can always copy it out and then setup the scheduled task from there.

    Thanks for reading, and thanks in advance for any help you can provide.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.