Announcement

Collapse
No announcement yet.

Deleting files older than 6 hours

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

  • Deleting files older than 6 hours

    I would like to write a script that can be set up as a Scheduled Task to delete files older than 7 hours from a few folders
    The folders are:
    D:\SDMAPP\DomInfo\Plans
    D:\SDMAPP\DomInfo\Hourly Metars
    D:\SDMAPP\DomInfo\Hourly Tafs
    D:\SDMAPP\IntlInfo\Plans
    D:\SDMAPP\IntlInfo\Hourly Metars
    D:\SDMAPP\IntlInfo\Hourly Tafs
    D:\SDMAPP\IntlInfo\Notam Briefs
    D:\SDMAPP\IntlInfo\Wx Briefs

    I would also like to log how many files are deleted from each folder.

    I found this vbscript through Google, and am not sure if this is the best way of doing it, and how do I change the script to do what I need:

    strFolder = "C:\Folder\"
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(strFolder)
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    For Each strFileName in objFolder.Items
    If len(objFSO.GetExtensionName(strFileName)) > 0 Then
    Set objFile = objFSO.GetFile(strFolder & strFileName.Name)
    If DateDiff("N",objFile.DateLastModified,Now()) > 360 Then
    objFSO.DeleteFile(strFolder & strFileName.Name),True
    End If
    End If
    Next

  • #2
    Re: Deleting files older than 6 hours

    ok so, what you should do, is create a tst folder, with some random, copied data.

    It will take a little while to setup, but when you get to work tomorrow, add some files to your test folder.
    an hour later, add some3 more.
    an hour after that, add some more.

    change the line here:
    If DateDiff("N",objFile.DateLastModified,Now()) > 360 Then

    and use a different length of time. (360 is seconds i beleive)
    then kick your script off, in your test location.
    Please do show your appreciation to those who assist you by leaving Rep Point https://www.petri.com/forums/core/im.../icon_beer.gif

    Comment


    • #3
      Re: Deleting files older than 6 hours

      cheers tehcamel
      Do i have to write a similar script for each folder and start it with a batch file?
      How do I add the logging?

      Comment


      • #4
        Re: Deleting files older than 6 hours

        krokkie,
        you can try "Forfiles.exe" in a batch.

        http://forums.petri.com/showpost.php...90&postcount=2

        Comment


        • #5
          Re: Deleting files older than 6 hours

          you would need to make it recursive at some point.. but i'd make sure your timing part works correctly first.
          Please do show your appreciation to those who assist you by leaving Rep Point https://www.petri.com/forums/core/im.../icon_beer.gif

          Comment


          • #6
            Re: Deleting files older than 6 hours

            Originally posted by krokkie View Post
            cheers tehcamel
            Do i have to write a similar script for each folder and start it with a batch file?
            How do I add the logging?
            You don't have to rewrite the same code for each target folder. In a script you can make a call to a subroutine section and passing arguments.

            Or, in your case, you could also create iteration by adding the specific folders in to an array and use a For-Next statement to process every item in the array.
            _

            You can write the script so that is able to create/append a logfile OR, you could also run the vbscript from a command line (or from a shortcut file) and redirect the results you normaly would see on the screen to a txt file.
            _

            You mensioned lauching the vbscript(s) from a bach file - maybe that was because you was looking for a way to run multiple vbs files in one run? or, do you prefere running a batch rather than directly starting the vbscript?

            Below is a batch sample (in case you prefere executing a batch).
            Code:
            :: this is a dos batch sample. save the file with the cmd extension.
            
            @GOTO:start
            
            ::> arrFolders = Array("D:\SDMAPP\DomInfo\Plans", _
            ::>                    "D:\SDMAPP\DomInfo\Hourly Metars", _
            ::>                    "D:\SDMAPP\DomInfo\Hourly Tafs", _
            ::>                    "D:\SDMAPP\IntlInfo\Plans", _
            ::>                    "D:\SDMAPP\IntlInfo\Hourly Metars", _
            ::>                    "D:\SDMAPP\IntlInfo\Hourly Tafs", _
            ::>                    "D:\SDMAPP\IntlInfo\Notam Briefs", _
            ::>                    "D:\SDMAPP\IntlInfo\Wx Briefs")
            ::> 
            ::> Set objShell = CreateObject("Shell.Application")
            ::> Set objFSO = CreateObject("Scripting.FileSystemObject")
            ::> dt = Now()
            ::> 
            ::> wscript.echo "reference date-time", dt
            ::> 
            ::> for i=0 to Ubound(arrFolders)
            ::> 
            ::>    strFolder = arrFolders(i)
            ::>    wscript.echo
            ::>    
            ::>    If objFSO.FolderExists(strFolder) Then
            ::>      Set objFolder = objFSO.GetFolder(strFolder)
            ::>           wscript.echo "Folder:", objFolder.Path
            ::>      Set colFiles = objFolder.Files
            ::>      k = 0 : l = 0
            ::>      For Each objFile in colFiles
            ::>        k = k + 1
            ::>        lstMod = objFile.DateLastModified
            ::>        If DateDiff("h",LstMod,dt) >= 7 Then
            ::>          l = l + 1
            ::>          wscript.echo "   Delete file: """ _
            ::>                  & objFile.Name & """  [" &  LstMod & "]"
            ::>          objFile.delete()
            ::>        End If
            ::>      Next
            ::>      wscript.echo k, "files /", l, "deleted."
            ::>    Else
            ::>      wscript.echo "ERROR, folder not found:", strFolder 
            ::>    End If
            ::> Next
            ::> wscript.echo
            ::> wscript.echo
            
               :runvbs (creating the vbs file with the above code)
               >"%temp%.\$vbs" echo.
               For /f "tokens=1,*" %%x in (
               'findstr /rb "::> " "%*"') do (
                  call set "line=echo.%%y"
                  for /f "tokens=*" %%* in ('"echo."%%line%%""') do (%%~*)
               ) >>"%temp%.\$vbs"
               cscript.exe /nologo /e:vbscript "%temp%.\$vbs" >>"%logfile%" 2>&1
               exit /b 0
            
            :start
            @echo off & cls
            color 9a & title Folder Cleaning.
            Set "logfile=c:\$$log.txt"
            echo. Do not close this box.....
            
            Call:runvbs %~0
            It is possible to just copy the vbs code part (without the leading "::> ") from this batch and paste it to a vbs file. Then create a shortcut that fires the script and redirect the output to a file.
            e.g.,
            cmd /c cscript.exe /nologo "c:\path\file.vbs" >>"%logfile%" 2>&1
            _

            More about DateDiff and its first parameter (=interval) you can read here: http://www.w3schools.com/vbscript/func_datediff.asp

            /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: Deleting files older than 6 hours

              Thanks you for all the info, it is much appreciated

              Comment


              • #8
                Re: Deleting files older than 6 hours

                Why not using powershell? You might be able to do this with just a couple of lines.
                Marcel
                Technical Consultant
                Netherlands
                http://www.phetios.com
                http://blog.nessus.nl

                MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
                "No matter how secure, there is always the human factor."

                "Enjoy life today, tomorrow may never come."
                "If you're going through hell, keep going. ~Winston Churchill"

                Comment


                • #9
                  Re: Deleting files older than 6 hours

                  I have no knowledge of Powershell

                  Comment


                  • #10
                    Re: Deleting files older than 6 hours

                    REM

                    I copied the script you provided and saved it as .cmd
                    When i run it, nothing happens, the screen comes up that says Do not close this, but nothing else.
                    The log file is created, but is blank.
                    I checked all the folders, and some of them have old files

                    Comment


                    • #11
                      Re: Deleting files older than 6 hours

                      Originally posted by krokkie View Post
                      REM

                      I copied the script you provided and saved it as .cmd
                      When i run it, nothing happens, the screen comes up that says Do not close this, but nothing else.
                      The log file is created, but is blank.
                      I checked all the folders, and some of them have old files
                      for troubleshooting purpose change the lines
                      Code:
                         cscript.exe /nologo /e:vbscript "%temp%.\$vbs" >>"%logfile%" 2>&1
                         exit /b 0
                      to
                      Code:
                         start ""/b /w cscript.exe /e:vbscript "%temp%.\$vbs"
                         pause
                         exit /b 0
                      Do you see any error messages appear in the box?

                      What Windows version is on the machine?


                      /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


                      • #12
                        Re: Deleting files older than 6 hours

                        Powershell example:
                        http://ict-freak.nl/2009/03/25/remov...ays-with-posh/
                        this is days though, but could be easily modfied to minutes.

                        Code:
                        $Now = Get-Date
                        $Days = "3"
                        $TargetFolder = "C:\LiveUpdate"
                        $LastWrite = $Now.AddDays(-$days)
                        $Files = get-childitem $TargetFolder -include *.zip, *.x86 -recurse `
                            | Where {$_.LastWriteTime -le "$LastWrite"} 
                        
                        foreach ($File in $Files)
                        {write-host "Deleting File $File" -foregroundcolor "Red"; `
                            Remove-Item $File | out-null}
                        Marcel
                        Technical Consultant
                        Netherlands
                        http://www.phetios.com
                        http://blog.nessus.nl

                        MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
                        "No matter how secure, there is always the human factor."

                        "Enjoy life today, tomorrow may never come."
                        "If you're going through hell, keep going. ~Winston Churchill"

                        Comment


                        • #13
                          Re: Deleting files older than 6 hours

                          Thanks Dumber

                          I will surely have a look at your suggestion.

                          Comment


                          • #14
                            Re: Deleting files older than 6 hours

                            REM

                            Let me see if I understand you correctly:

                            I need to copy the whole script you provided and save as .cmd

                            or

                            I need to copy the vbs part and run it with the command you've provided

                            Comment


                            • #15
                              Re: Deleting files older than 6 hours

                              Dumber
                              What would I save this Poewershell script as?
                              And how would I run it?

                              Comment

                              Working...
                              X