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

Author Topic: VB Scrript to delete old files and subfolders  (Read 9551 times)

0 Members and 1 Guest are viewing this topic.

jutpro

    Topic Starter


    Rookie

    VB Scrript to delete old files and subfolders
    « on: November 23, 2009, 07:30:56 AM »
    Hello,

    Can someone give me some help on the below VB script. Unfortunately I am not a VB programmer. I need this script to delete  files that are 30 days old from a folder call "test" and any subfolders found within the test folder. Once all the old files are deleted, I also want the empty subfolders to be deleted. I do not want the test folder to be deleted only the empty subfolders.

    I pasted together different portions of scripts and the below is the script I have. This script works in deleting all files that are 30 days old from the main folder, 'test",  and all subfolders. However, I cannot get it to delete the empty subfolders.

    Can someone be so kind as to look at the below script and let me know what needs to be modified to get it to delete the empty subfolders. 

    Thanks in advance for your help.

    *****************VB Script**************************
    Dim fso, startFolder, OlderThanDate

    Set fso = CreateObject("Scripting.FileSystemObject"
    startFolder = "c:\test"
    OlderThanDate = DateAdd("d", -30, Date) ' 30 days

    DeleteOldFiles startFolder, OlderThanDate

    Function DeleteOldFiles(folderName, BeforeDate)
    Dim folder, file, fileCollection, folderCollection, subFolder

    Set folder = fso.GetFolder(folderName)
    Set fileCollection = folder.Files
    For Each file In fileCollection
    If file.DateLastModified < BeforeDate Then
    fso.DeleteFile(file.Path)
    End If
    Next

    Set folderCollection = folder.SubFolders
    For Each subFolder In folderCollection
    DeleteOldFiles subFolder.Path, BeforeDate
    Next
    End Function

    Function DeleteOldfolder(foldername, BeforeDate)

    Set folderlist = fso.GetFolder(foldername)
    Set folderCollection = Folderlist.SubFolders
    For Each Folder In folderCollection
    If folder.DateLastModified <  BeforeDate Then
    fso.DeleteFolder(folder.Path)
    End If
    Next
    End Function
    *****************************************************************


    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: VB Scrript to delete old files and subfolders
    « Reply #1 on: November 23, 2009, 10:23:31 AM »
    Pasting portions of scripts can be more trouble than it's worth. Try as I might I can't find a reference to the DeleteOldfolder function which is probably a good thing since the logic is incorrect.

    I came up with a possible solution by creating a collection of files of each folder immediately after the aging/deleting of the files. If the collection count is zero, the folder is empty and you can delete the folder.

    Code: [Select]
    Dim fso, startFolder, OlderThanDate

    Set fso = CreateObject("Scripting.FileSystemObject"
    startFolder = "c:\test"
    OlderThanDate = DateAdd("d", -30, Date) ' 30 days

    DeleteOldFiles startFolder, OlderThanDate

    Function DeleteOldFiles(folderName, BeforeDate)
    Dim folder, file, fileCollection, folderCollection, subFolder

    Set folder = fso.GetFolder(folderName)
    Set fileCollection = folder.Files
    For Each file In fileCollection
    If file.DateLastModified < BeforeDate Then
    fso.DeleteFile(file.Path)
    End If
    Next

    Set f = fso.GetFolder(foldername)
    Set fc = f.files
    If fc.Count = 0
       If foldername.DateLastModified < BeforeDate Then
         fso.DeleteFolder foldername, True
            End If
    End If

    For Each subFolder In folderCollection
    DeleteOldFiles subFolder.Path, BeforeDate
    Next
    End Function

    Hope this helps.  8)

    PS. Indentation and white space are very helpful in adding to script readability. The interpreter doesn't care but the next person who reads your script will be very thankful.
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    MikeTaylor



      Rookie

      Re: VB Scrript to delete old files and subfolders
      « Reply #2 on: November 23, 2009, 10:29:26 AM »
      Have a great life.
      « Last Edit: November 23, 2009, 07:06:51 PM by MikeTaylor »
      Bill Richardson

      Salmon Trout

      • Guest
      Re: VB Scrript to delete old files and subfolders
      « Reply #3 on: November 23, 2009, 10:35:02 AM »
      MIkeTaylor, please consider carefully before posting. In this and other threads.


      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: VB Scrript to delete old files and subfolders
      « Reply #4 on: November 23, 2009, 10:35:36 AM »
      All you need is xcopy and the /D:12-23-2009   option.  xcopy will do the math.

      I don't think so. First the OP is adding -30 days, so the files can be aged. Second, XCOPY does not delete files, it selects them for a copy operation. Third, we went through this discussion last week and it turned out to be another trip into the rabbit hole.

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

      -- Albert Einstein

      MikeTaylor



        Rookie

        Re: VB Scrript to delete old files and subfolders
        « Reply #5 on: November 23, 2009, 11:02:17 AM »
        Have a great day.
        « Last Edit: November 23, 2009, 07:01:46 PM by MikeTaylor »
        Bill Richardson

        Salmon Trout

        • Guest
        Re: VB Scrript to delete old files and subfolders
        « Reply #6 on: November 23, 2009, 11:03:44 AM »
        I think I recognise the posting style... (And the lack of useful content. Did you get deleted, Billrich, or did you do it yourself?)

        MikeTaylor



          Rookie

          Re: VB Scrript to delete old files and subfolders
          « Reply #7 on: November 23, 2009, 11:15:58 AM »
          Have Nice Day.
          « Last Edit: November 23, 2009, 06:50:12 PM by MikeTaylor »
          Bill Richardson

          Salmon Trout

          • Guest
          Re: VB Scrript to delete old files and subfolders
          « Reply #8 on: November 23, 2009, 11:26:43 AM »
          Who is Bill Rich?

          You.

          Quote
          Who is John Galt?

          The hero of an amazingly dumb book by the crazy Ayn Rand, revered by loony far-right-wing crackpots mainly in the US.


          Sidewinder



            Guru

            Thanked: 139
          • Experience: Familiar
          • OS: Windows 10
          Re: VB Scrript to delete old files and subfolders
          « Reply #9 on: November 23, 2009, 11:27:28 AM »
          Quote
          All you need is xcopy and the /D:12-23-2009   option.  xcopy will do the math.

          I am quite aware how XCOPY works, thank you very much.

          How did you get the value for the /d switch? You must have calculated it, since you can bet your sweet bippie that XCOPY didn't do the calculation. By the way, it's calculated wrong, it is -30 days.

          Also, 12--23-2009 is a date in the future, which makes no logical sense in the context of XCOPY.

          You seem to have a serious problem with reading comprehension. Considering what the OP set out to do, he chose one of the right solutions. You on the other hand gave him a road map to Fantasy Island.

          Have a nice day Bill  8)

          PS. John Galt is a fictional character in Ayn Rand's novel Atlas Shrugged.
          The true sign of intelligence is not knowledge but imagination.

          -- Albert Einstein

          BatchFileBasics



            Hopeful

            Thanked: 18
            Re: VB Scrript to delete old files and subfolders
            « Reply #10 on: November 23, 2009, 11:40:32 AM »
            yuup bill is back. CAN SOMEONE PLEASE BAN HIM?! he is really annoying me and disrupting all topics hes been in
            When the power of love overcomes the love of power the world will know peace - Jimi Hendrix.

            jutpro

              Topic Starter


              Rookie

              Re: VB Scrript to delete old files and subfolders
              « Reply #11 on: November 23, 2009, 11:58:16 AM »
              thank you Sidewinder. I am most grateful for your response.

              I ran the script but I received several errors. The 1st error indicated that the word 'then' was expected at line 21,
              Line 21 has this statement, "If fc.Count = 0".
              I made the correction so now the statment is this, "If fc.Count = 0 then".
              I ran the script again and received another error, this time for line 22. The error message is, "Object required: 'folderName' ".
              Line 22 has this statement, "If foldername.DateLastModified < BeforeDate Then" 

              I am clue less as to how to correct this. :-[   Can you take another look for me.

              Thanks.

              Sidewinder



                Guru

                Thanked: 139
              • Experience: Familiar
              • OS: Windows 10
              Re: VB Scrript to delete old files and subfolders
              « Reply #12 on: November 23, 2009, 12:46:25 PM »
              I think this will fix the remaining error. In the future, try not to use data names like folder, file, and subfolders.  These are properties of VBScript objects and it only confuses things when trying to debug a script.

              Code: [Select]
              Dim fso, startFolder, OlderThanDate

              Set fso = CreateObject("Scripting.FileSystemObject")
              startFolder = "c:\test"
              OlderThanDate = DateAdd("d", -30, Date) ' 30 days

              DeleteOldFiles startFolder, OlderThanDate

              Function DeleteOldFiles(folderName, BeforeDate)
              Dim folder, file, fileCollection, folderCollection, subFolder

              Set folder = fso.GetFolder(folderName)
              Set fileCollection = folder.Files
              For Each file In fileCollection
              If file.DateLastModified < BeforeDate Then
              fso.DeleteFile(file.Path)
              End If
              Next

              Set f = fso.GetFolder(folderName)
              Set fc = f.Files
              If fc.Count = 0 Then
                 If f.DateLastModified < BeforeDate Then
                   fso.DeleteFolder foldername, True
                      End If
              End If

              For Each subFolder In folder.SubFolders
              DeleteOldFiles subFolder.Path, BeforeDate
              Next
              End Function

              Good luck. 8)

              Consider changing fso.deletefile and fso.deletefolder to wscript.echo which will simply list the files and folders to be deleted. When everything looks good you can change back to the original code.
              The true sign of intelligence is not knowledge but imagination.

              -- Albert Einstein

              jutpro

                Topic Starter


                Rookie

                Re: VB Scrript to delete old files and subfolders
                « Reply #13 on: November 23, 2009, 01:24:48 PM »
                Thanks for the tips. You have been a great help. Later I will make the corrections and see what happens then.

                Have a great week :)

                gh0std0g74



                  Apprentice

                  Thanked: 37
                  Re: VB Scrript to delete old files and subfolders
                  « Reply #14 on: November 23, 2009, 06:11:40 PM »
                  I am quite aware how XCOPY works, thank you very much.
                  How did you get the value for the /d switch? You must have calculated it, since you can bet your sweet bippie that XCOPY didn't do the calculation. By the way, it's calculated wrong, it is -30 days.
                  that was exactly argued and made across to him recently (but the post was deleted). I will treat you to coffee if he ever takes your advice about this:)

                  I am only assuming Mike is Bill