Announcement

Collapse
No announcement yet.

Need help with editing a script (Delete files after x hours)

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

  • Need help with editing a script (Delete files after x hours)

    Rems posted a script that deletes files after X hours since last modified.
    His script works GREAT for me on windows xp, it deletes ALL file types perfectly fine.

    I would like to make this script only look for certain file types instead of all files.
    File types I would only like to delete are (.txt , .ini , .chk , .rem)

    OR another way that would work for my situation is to Delete everything EXCEPT .gpg file types.

    What portion of his script below would I change. I assume I need to add a mask somewhere, but I am a newb still.

    Any help would greatly be appreciated.

    Script below is written by Rems, I have edited the hours to 8 and the array to a folder on my C: called resize. This is all I have edited and it currently works but deletes ALL files.
    Code:
    arrFolders = Array("C:\resize")
    
    Set objShell = CreateObject("Shell.Application")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    dt = Now()
    
    Set objLogFile = objFSO.OpenTextFile("c:\test-log.txt", 8, True)
    
    dim k, l, ld
    
    rem wscript.echo "reference date-time", dt
    objLogFile.writeLine("reference date-time " & dt)
    for i=0 to Ubound(arrFolders)
       ld = "Folder: "
       strFolder = arrFolders(i)
       rem wscript.echo
       objLogFile.writeLine 
       If objFSO.FolderExists(strFolder) Then
         k = 0 : l = 0 
         enumerateFolders objFSO.GetFolder(strFolder)
         rem wscript.echo k, "files /", l, "deleted."
         objLogFile.writeLine k & " files / " & l & " deleted."
       Else
         rem wscript.echo "ERROR, folder not found:", strFolder
         objLogFile.writeLine "ERROR, folder not found: " & strFolder
       End If
    Next
    rem wscript.echo
    rem wscript.echo
    objLogFile.writeLine
    objLogFile.writeLine
    
    Sub enumerateFolders(Folder)
       enumerateFiles Folder.Path
       On Error Resume Next
       For Each Subfolder in Folder.SubFolders
         enumerateFolders Subfolder
       Next
    End Sub
    
    Sub enumerateFiles(sPath)
         Dim objFolder
         Set objFolder = objFSO.GetFolder(sPath)
         rem wscript.echo ld & objFolder.Path
         objLogFile.writeLine ld & objFolder.Path
         ld = "   Sub: "
         Set colFiles = objFolder.Files
         On Error Resume Next
         For Each objFile in colFiles
           k = k + 1
           lstMod = objFile.DateLastModified
           If DateDiff("h",LstMod,dt) >= 7 Then
             l = l + 1
             rem wscript.echo "   Delete file: """ _
             rem         & objFile.Name & """  [" &  LstMod & "]"
             objLogFile.writeLine "   Delete file: """ _
                     & objFile.Name & """  [" &  LstMod & "]"
             objFile.delete()
           End If
         Next
    End Sub
    Last edited by Rems; 10th August 2012, 22:21. Reason: added a link to other post

  • #2
    Re: Need help with editing Rems Script (delete files after x hours)

    Replace the last sub routine in the script with:

    Code:
    Sub enumerateFiles(sPath)
         Dim objFolder
         Set objFolder = objFSO.GetFolder(sPath)
         rem wscript.echo ld & objFolder.Path
         objLogFile.writeLine ld & objFolder.Path
         ld = "   Sub: "
         Set colFiles = objFolder.Files
         On Error Resume Next
         For Each objFile in colFiles
           k = k + 1
           Select case UCase(objFSO.GetExtensionName(objFile.Path))
    
             ' You must enter the file extensions in Uppercase, and w/out the dot!
             Case "TXT", "INI", "CHK", "REM"
    
               lstMod = objFile.DateLastModified
               If DateDiff("h",LstMod,dt) >= 7 Then
                 l = l + 1
                 rem wscript.echo "   Delete file: """ _
                 rem         & objFile.Name & """  [" &  LstMod & "]"
                 objLogFile.writeLine "   Delete file: """ _
                     & objFile.Name & """  [" &  LstMod & "]"
                 objFile.delete()
               End If
           End Select
         Next
    End Sub
    /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


    • #3
      Re: Need help with editing Rems Script (delete files after x hours)

      Rems THANK YOU VERY MUCH!!! That works great.

      IF you would like to help me some more I have another question...(I hope it is not too difficult...just trying different things.)

      I was wondering what if I would like these files MOVED rather than deleted.
      I thought I would just have to change
      Code:
      objFile.delete()
      to

      Code:
      objFile.move "c:\erasethis\"
      But that fails to move...it does log and writes to the log.
      Obviously I am way off I am sure. Is it my syntax?

      I have tried a couple of different ways such as
      objFile.movefile "c:\erasethis\"
      objFile.move ("c:\erasethis\")
      objfile.move , "c:\erasethis\"

      None work.
      Again if this is annoying you I am sorry. Just picking things apart seeing what they do.

      THANK YOU again for the file type part!!

      Comment


      • #4
        Re: Need help with editing Rems Script (delete files after x hours)

        Well it seems I got it to move the files by adding:

        strFolderPath = "C:\erasethis\"

        And this:

        objFSO.MoveFile objFile.Path, strFolderPath

        Which moves the files.


        Again thank you for your help.

        Comment

        Working...
        X