Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Delete the .BAK files in a folder that are more than 7 days old  (Read 4710 times)

0 Members and 1 Guest are viewing this topic.

whosyourdaddy

    Topic Starter


    Rookie
    How Can I Delete All the .BAK Files in a Folder That Are More Than 7 Days Old?

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Delete the .BAK files in a folder that are more than 7 days old
    « Reply #1 on: April 02, 2007, 06:44:31 AM »
    There are third party DOS utilities that can do date handling, but left alone, batch code is somewhat lacking in many features...date handling among them.

    This little script should help you out:


    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder("your foldergoeshere")
    Set colSubFolders = f.SubFolders

    For Each objFolder in colSubFolders
            ShowFiles objFolder
    Next

    Sub ShowFiles(Fld)
            Set k = fso.GetFolder(Fld)
            Set s = k.SubFolders
            Set kf = k.Files

            For Each objFile In kf
                    If objFile.DateCreated < date - 7 Then
                       If objFile.Extension = "bak" Then
                            Wscript.Echo objFile & " " & objFile.DateCreated
                    End If
            Next
    End Sub


    As written the script only lists files. Change the highlighted line to fso.DeleteFile(objFile). Be sure to change yourfoldergoeshere with something appropriate (ie: C:\Backup). Save the script with a vbs extension and run as cscript scriptname.vbs

     8)
    « Last Edit: April 02, 2007, 12:39:10 PM by Sidewinder »
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    whosyourdaddy

      Topic Starter


      Rookie
      Re: Delete the .BAK files in a folder that are more than 7 days old
      « Reply #2 on: April 03, 2007, 03:48:09 AM »
      I tried to modify your script as below but failed, could you help me to check. Thank you very much.

      Set fso = CreateObject("Scripting.FileSystemObject")
      Set f = fso.GetFolder("F:\test\backup")
      Set colSubFolders = f.SubFolders

      For Each objFolder in colSubFolders
              ShowFiles objFolder
      Next

      Sub ShowFiles(Fld)
              Set k = fso.GetFolder(Fld)
              Set s = k.SubFolders
              Set kf = k.Files

              For Each objFile In kf
                      If objFile.DateCreated < date - 7 Then
                         If objFile.Extension = "bak" Then
                              fso.DeleteFile(objFile)
                         End If
                      End If
              Next
      End Sub

      street1 (RIP)

      • R.I.P.


      • Egghead

      • I Triple Dog Dare You!!! LOL
      • Thanked: 14
        • Obituary
      • Experience: Beginner
      • OS: Windows XP
      Re: Delete the .BAK files in a folder that are more than 7 days old
      « Reply #3 on: April 03, 2007, 04:05:40 AM »
      whosyourdaddy

      whosyourbuddy............. ;D
      Sorry,The USA has ruined the language The United Kingdom loaned us. We do our best not to type gibberish. I Hope you can forgive us.

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: Delete the .BAK files in a folder that are more than 7 days old
      « Reply #4 on: April 03, 2007, 05:37:45 AM »
      I may have inadvertently introducied a property that does not exist. On  the other hand it would have been helpful had you pointed out the source of the error. In any case the original script was overwritten and used recursion to delete all bak files in the directory and it's subdirectories.

      Code: [Select]
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set f = fso.GetFolder("F:\test\backup")
      Set colFiles = f.Files

      For Each objFile in colFiles
        If objFile.DateCreated < date - 7 Then
          If Right(objFile.Name, 4) = ".bak" Then
            fso.DeleteFile(objFile)
          End If
        End If
      Next

      Note : if "f:\test\backup" does not exist, the script will fail

      Good luck.  8)
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      whosyourdaddy

        Topic Starter


        Rookie
        Re: Delete the .BAK files in a folder that are more than 7 days old
        « Reply #5 on: April 04, 2007, 02:40:40 AM »
        Does vbs is a freeware? Need to purchase license or not?

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: Delete the .BAK files in a folder that are more than 7 days old
        « Reply #6 on: April 04, 2007, 05:02:54 AM »
        No commercial license is needed. VBScript is installed on all Windows machines except the earlier versions of Win95. There are commercial editors available for VBScript but Notepad works just fine. The current version of the Windows Script Host (as it is officially known) is 5.6

        There is a treasure of information about VBScript at the Script Center including free scripts, free downloads, online lessons and sample scripts for doing just about anything.

        Good luck.  8)
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        whosyourdaddy

          Topic Starter


          Rookie
          Re: Delete the .BAK files in a folder that are more than 7 days old
          « Reply #7 on: April 22, 2007, 01:14:39 AM »
          I get the answer from website as below:

          Set ObjFSO = CreateObject ("Scripting.FileSystemObject")

          'Pull current date
          DateInfo = Now

          'Delete files older than X number of days
          OlderThan = "7"

          'Remove number of days from date
          DateInfo = DateInfo - OlderThan

          WScript.Echo "Deleting files older than " & DateInfo

          'Folder we're interested in
          strFolder = "F:\test"

          'WScript.Echo DateInfo 'Testing


          'Get folder for collection

          NumberOfFiles = "0"
          DeletedFiles = "0"

          Set objFolder = objFSO.GetFolder(strFolder)
          Set colFiles = objFolder.Files
          For each objFile in colFiles
          'WScript.Echo objFile.DateCreated 'Testing
          'WScript.Echo objFile.Name 'Testing
          NumberOfFiles = NumberOfFiles + 1
          If objFile.DateCreated < DateInfo Then
          WScript.Echo "Deleting " & objFile.Name
          objFSO.DeleteFile(objFile.Path)
          DeletedFiles = DeletedFiles + 1
          End If
          Next
          WScript.Echo "Complete"
          WScript. Echo NumberOfFiles & " file(s) in directory"
          WScript.Echo DeletedFiles & " file(s) deleted"

          Sidewinder



            Guru

            Thanked: 139
          • Experience: Familiar
          • OS: Windows 10
          Re: Delete the .BAK files in a folder that are more than 7 days old
          « Reply #8 on: April 22, 2007, 05:36:33 AM »
          I'm glad the Scripting Guys were able to help. One nice thing is if you don't find a script to do exactly want you want, you can always borrow pieces of multiple scripts to satisfy your inner scriptwriter.

          Unfortunately folks have been known to take this a bit too literally and built a frankenscript which turned their PC into a brick.

           ;D
          The true sign of intelligence is not knowledge but imagination.

          -- Albert Einstein