Announcement

Collapse
No announcement yet.

Script to delete all windows temp files help

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

  • Script to delete all windows temp files help

    I am new to scripting and as such have no idea how to do it.
    What I would like to accomplish is to delete all files and folders in users %temp% directory.

    Cheers

  • #2
    Re: Script to delete all windows temp files help

    Hope this might help: http://forums.petri.com/showthread.p...ht=delete+temp
    This is my signature.

    Comment


    • #3
      Re: Script to delete all windows temp files help

      How do want to execute this script?
      1. You can create a user Log-on Script (batch). This will delete all what is in the temp folder only of the current user.
      Or..
      2. When you want to delete the tempfiles of all the users in one time, you can write a script (batch) that can do this, but then it must be executed by a member of the local administrators goup -OR- you can run this script as a Computer Start-up Script or computer shutdown script.


      Batch sample for a user logon script that empties the current user's temp-folder:
      Code:
      ::\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
      ::
      :: Filename: EmptyFolder.cmd
      :: Author:   Remco Simons [NL] 2008
      ::
      :: ( http://forums.petri.com/showthread.php?t=20755 )
      ::
      :: This batch delete all files and subfolders in the %Target% folder
      :: It accept also UNC-paths. 
      ::
      :: syntax;
      :: EmptyFolder "path"
      ::
      ::\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
      
      @echo off
      
      (Set Target=%1) 
      
      :: Delete all files and subfolders
      (Set Target=%Target:"=%)
      title,DelTree %Target%&COLOR 9E
      If EXIST "%Target%",(
         PushD "%Target%" && (
         ATTRIB -S -H -R -A /D /S & (
         For /f "Tokens=*" %%* in ('dir "%Target%" /B') Do (
          RD "%Target%\%%*" /S /Q || Del /F "%Target%\%%*"))&PopD)
       )
      cls&(
      echo.UNABLE TO DELETE;&echo.-----------------&&Dir,"%Target%"/B/O|find /v ""||cls)
      
      rem pause
      The command-line for the batch above would be in your case:
      "<path>\EmptyFolder.cmd" %temp%
      You can auto exectute this command-line using; log-0n script, startup folder or run key in the registry (but NOT as a scheduled task).
      It is possible to write log of the files that were undeletable, this way:
      cls&(
      echo.UNABLE TO DELETE;&echo.-----------------&&Dir,"%Target%"/B/O|find /v ""||cls)>"c:\DelTemp-%username%.log"



      The sample batch below is specialy written to empty the temp folders of all the users in one run;
      (it is possible to schedule this batch)
      Code:
      @echo off
      :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
      :: clean the temp folders of all local profiles                                ::
      :: assumming the default location of these folders ! on the local computer !!! ::
      :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
      
      ::Set Search directory to "Documents and Settings" folder
      (Set Target=%AllUsersProfile:~0,-10%)
      
      title,Finding the Temp subfolders in %Target%&COLOR 9E
      
      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)
       )
      
      rem pause
      \Rems
      Last edited by Rems; 12th January 2008, 00:38. Reason: about using a 'task scheduler'

      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


      • #4
        Re: Script to delete all windows temp files help

        Worked a treat, thanks so much for the assistance

        Comment


        • #5
          Re: Script to delete all windows temp files help

          Nice to hear! thank you for the feedback.

          \Rems


          alternatively;
          From the Windows 2000 resource kit you could also download a command utility named: ForFiles
          This tool can be used to Delete files by age.
          more about this tool:
          - http://www.jsifaq.com/SF/Tips/Tip.aspx?id=5645
          - http://technet2.microsoft.com/Window...9381e1033.mspx
          - http://winhlp.com/node/180 (samples)

          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

          Working...
          X