Announcement

Collapse
No announcement yet.

Script to delete Windows temp files via Scheduled Tasks

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to delete Windows temp files via Scheduled Tasks

    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 Settings\Temporary Internet Files.

    Here is my script:
    Code:
    @echo off
    CLS
    cd \
    del c:\temp\*.* /F /S /Q
    del c:\windows\temp\*.* /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 Settings\History"
    del "Local Settings\History\*.*" /F /S /Q
    attrib -s -h -r "Local Settings\Temporary Internet Files"
    del "Local Settings\Temporary Internet Files\*.*" /F /S /Q
    del "Local Settings\Temp\*.*" /F /S /Q
    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 Settings\Temp" && (
       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 Settings\Temporary Internet Files" && (
       ATTRIB -S -H -R -A /D /S >nul & (
      For /f "Tokens=*" %%* in ('dir /B') Do (
       RD "%%*" /S /Q ||Del /F "%%*" )))>nul)
     )
    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.

  • #2
    Re: Script to delete Windows temp files via Scheduled Tasks

    The first script that you showed is expanding %USERPROFILE%, this will only return the profile path of the account that is running the script (= the account lauching the scheduled task!). If that would be the logged on user s/he probably is not allowed to clean the c:\Windows\temp folder too. Note it is not recommended to delete recent files from the Windows\temp folder!

    Note, when launching a script by the task scheduler you can not use drive letters of mapped drives - not in the job and not in the script.


    If the script will be started in the logged on user's security context, you can try this script to clear that user's IE cache,
    Code:
    @echo off
    
    set "regkey=HKCU\Software\Microsoft\Windows\CurrentVersion"
    set "regkey=%regkey%\Explorer\shell folders"
    For /f "tokens=2,*" %%A in (
        'reg.exe query "%regkey%" /v "Cache"'
      ) do Call set "Target=%%B\Content.IE5"
    
    pushD "%Target%" &&(
    rmdir /s /q .
    popD)2>nul
    
    start ""/w "%windir%\system32\RunDll32.exe" InetCpl.cpl,ClearMyTracksByProcess 255
    
    pushD "%Temp%" &&(
    rmdir /s /q .
    popD)2>nul
    /Rems
    Last edited by Rems; 29th April 2011, 10:48.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Script to delete Windows temp files via Scheduled Tasks

      Thanks for the reply Rems. You mention several things, so I'll go through one by one.
      1. The script runs via Scheduled Tasks with a domain acct credentials. In the first script it's only cleaning the currently logged on acct. In the 2nd it's cleaning all profiles.
      2. I directed SchedTasks to point to the UNC path. The account that I'm testing on does have that UNC path mapped. Would that cause a problem? Also, permissions for the account that I'm testing with has full permissions to the path and script.
      3. You say,
        If the script will be started in the logged on user's security context
        . How would you run it with other credentials?

      It's going to be a busy day for me today. I'll try to respond as quickly as I can. Thanks again.

      Comment


      • #4
        Re: Script to delete Windows temp files via Scheduled Tasks

        Originally posted by j0shu4 View Post
        • The script runs via Scheduled Tasks with a domain acct credentials.
        • The account that I'm testing on does have that UNC path mapped.. Would that cause a problem?
        Assuming the "domain acct credentials" used and the "account that I'm testing on" are two different accounts...
        can you show the command line. And what OS is running on the clients?


        Originally posted by j0shu4 View Post
        permissions for the account that I'm testing with has full permissions to the path and script.
        Since the Scheduled Job (using the secondairy logon service) runs with the "domain acct credentials", then that'll be the account that should have read-and-execute access to this network folder. And that is also the account whom's temp folders will be is cleaned.
        Is the account that's starting the job a member of the local administrators group?


        btw. Why you like to use a scheduled task to launch the script and not simply using a logonscript or startupscript?


        /Rems
        Last edited by Rems; 29th April 2011, 19:32.

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Script to delete Windows temp files via Scheduled Tasks

          Assuming the "domain acct credentials" used and the "account that I'm testing on" are two different accounts...
          Yes, they are two distinct accounts. "domain acct credentials" = a generic domain admin & the credentials that I'm creating the task with.

          "account that I'm testing on" = my personal domain admin acct & and the acct that I'm currently logged on with.

          The OS is XP SP3.

          The generic domain admin does have access to the share that the script is stored in. But, it is not the account that I'm trying to clean the temp files from. Yes, the account that's starting the job is a member of the local admin group.

          If making this a logon script would simplify the process I'm all for that. I hadn't thought of it. I was asked if I could clean the files at a certain time so I was approaching it from that angle. Cleaning on logon would work just as well and would be applied across domain, which would be a bonus for me wherever this account logs on.

          Before we approach this from a logon script way, the user that I want to clean the temp files from is a non-local admin user. Just an FYI for you. (I've only been testing using my account.)

          Comment


          • #6
            Re: Script to delete Windows temp files via Scheduled Tasks

            Originally posted by j0shu4 View Post
            If making this a logon script would simplify the process I'm all for that. I hadn't thought of it. I was asked if I could clean the files at a certain time so I was approaching it from that angle. Cleaning on logon would work just as well and would be applied across domain, which would be a bonus for me wherever this account logs on.

            Before we approach this from a logon script way, the user that I want to clean the temp files from is a non-local admin user. Just an FYI for you. (I've only been testing using my account.)
            The batch sample I posted earlier can be used for the user logon script (or user logoff script). It works also for non-local admins.

            If you would prefere using a scheduled task, and run the job as an Administrator that cleans temp files of all the users profiles (incl. index.dat files!), try this:
            Code:
            :: Works on Win XP  -and-  on Win 7
            
            @echo off
            
            Set "RegKey=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
            set "regkey2=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\shell folders"
            
            call:getspecialfolders "Cache, History, Cookies"
            
            For /f "tokens=*" %%? in (
             'Reg.exe QUERY "%RegKey%" ^|findstr /ric:"\S-1-5-21-[0-9]*-[0-9]*-[0-9]*-[0-9]*$"'
             ) do (
             For /f "tokens=2,*" %%A in (
             'Reg.exe QUERY "%%?" /v ProfileImagePath ^|find /i "ProfileImagePath"'
             ) do call:Go %%B
            )
            
            start ""/w "%windir%\system32\RunDll32.exe" InetCpl.cpl,ClearMyTracksByProcess 255
            
            :end ***
            
            
            goto:EOF
            :Go
               call Set "Target=%*"
               If EXIST "%Target%" call:Clear "%Target%"
            exit /b 0
            
            :Clear
            rem echo.&echo.%~1\%$$Cache%\Content.IE5
               pushD "%~1\%$$Cache%\Content.IE5" &&(
               rmdir /s /q .
               popD)2>nul
            
            rem echo.&echo.%~1\%$$History%\History.IE5
               pushD "%~1\%$$History%\History.IE5" &&(
               rmdir /s /q .
               popD)2>nul
            
            rem echo.&echo.%~1\%$$History%
               pushD "%~1\%$$History%" &&(
               rmdir /s /q .
               popD)2>nul
            
            rem echo.&echo.%~1\%$$Cookies%
               pushD "%~1\%$$Cookies%" &&(
               rmdir /s /q .
               popD)2>nul
            
            rem echo.&echo.%~1\%$$Cache%
               pushD "%~1\%$$Cache%" &&(
               rmdir /s /q .
               popD)2>nul
            
            rem echo.&echo.%~1\%$$temp%
               pushD "%~1\%$$temp%" &&(
               rmdir /s /q .
               popD)2>nul
            exit /b 0
            
            :getspecialfolders
               Set "FoldersToClear=%~1"
            
               For %%* in (%FoldersToClear%) Do (
                 For /f "tokens=2,*" %%A in (
                 'reg.exe query "%regkey2%" /v %%* ^|find /i "%%~*"'
                 ) do Call:sf1 "%%~B" "%%~*"
               )
               Call:sf2 "%temp%" "temp" "%userprofile%"
            exit /b 0
            
            :sf1
               Call set "sf=%~1"
               Call set "$$%~2=%%sf:%userprofile%\=%%"
            exit /b 0
            
            :sf2
               Call set "sf=%~1"
               call Set "usr=%~dpns3"
               Call set "$$%~2=%%sf:%usr%\=%%"
            exit /b 0
            /Rems

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment


            • #7
              Re: Script to delete Windows temp files via Scheduled Tasks

              Thanks Rems, I'll give that a try and let you know how it goes.

              Comment


              • #8
                Re: Script to delete Windows temp files via Scheduled Tasks

                Rems, I went with the login script. Once it finally replicated around (AD having some issues recently) it worked beautifully. Thanks for the help; I appreciate it. Now tell me how this reputation stuff works and I'll send some your way!

                Comment


                • #9
                  Re: Script to delete Windows temp files via Scheduled Tasks

                  Originally posted by j0shu4 View Post
                  Rems, I went with the login script. Once it finally replicated around (AD having some issues recently) it worked beautifully. Thanks for the help; I appreciate it. Now tell me how this reputation stuff works and I'll send some your way!
                  Just click the yin/yang symbol on any post REMS made.

                  Comment


                  • #10
                    Re: Script to delete Windows temp files via Scheduled Tasks

                    Originally posted by akitafan View Post
                    Just click the yin/yang symbol on any post REMS made.
                    I missed that - Thanks Steven!

                    j0shu4, it is prefered to click the yinyang that is on the post that was helpfull to you. Although clicking any post of a user will raise his/her reputation points, but it is nice for the person to be able to review what post of his/her it was.

                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    __________________

                    ** Remember to give credit where credit's due **
                    and leave Reputation Points for meaningful posts

                    Comment


                    • #11
                      Re: Script to delete Windows temp files via Scheduled Tasks

                      Done! Thanks guys. It's been working beautifully.

                      Comment

                      Working...
                      X