locked
Periodic folder and sub folder clean up RRS feed

  • Question

  • Hi,

    I have a root FTP folder with sub-folders in which we would like to have a script running regularly.

    I'm in need of some advice for completing the below .vbs script to simply do the following:

    Delete files older than X amount of days inside the root folder, and files inside the sub-folders of the root folder, without deleting the folders themselves.

    My script below, deletes all the folders inside the root folder fine, however I'm not advanced enough in scripting to get the last piece to my puzzle to work. Any help would be much appreciated;

    Working script to clear files in root folder:

    Const strPath = "C:\Test" Dim objFSO Set objFSO = CreateObject("Scripting.FileSystemObject")

    Call Search (strPath)

    Sub Search(str)

        Dim objFolder, objSubFolder, objFile

        Set objFolder = objFSO.GetFolder(str)

        For Each objFile In objFolder.Files

     

            ' Use DateLastModified for accessed date of a file

            If objFile.DateLastAccessed < (Now() - 90) Then

                objFile.Delete(True)

            End If

    Next

    End Sub

     

    Non working attempt to clear files in subfolders also:

    Const strPath = "C:\Test"

    Dim objFSO

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set colSubfolders = objFolder.Subfolders

    Set colFiles = objFolder.Files

     

    Call Search (strPath)

    Sub Search(str)

        Dim objFolder, objSubFolder, objFile

        Set objFolder = objFSO.GetFolder(str)

        For Each objFile In objFolder.Files

     

            ' Use DateLastModified for accessed date of a file

            If objFile.DateLastAccessed < (Now() - 90) Then

                objFile.Delete(True)

            End If

    Next

        For Each objSubFolder In objFolder.SubFolders

            Set colFiles = objSubfolder.Files 

       For Each objFile in colFiles 

           If objFile.DateLastAccessed < (Now() - 90) Then

                objFile.Delete(True)

            End If

        Next

    End Sub

    Thanks!


    Sunday, May 18, 2014 6:28 AM

Answers

  • I assume you are looking for a recursive script, e.g. like this one:

    Const strPath = "C:\Test"
    Const iAge = 30
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Search (strPath)

    Sub Search(sPath)    
        Set objFolder = objFSO.GetFolder(sPath)    
        For Each objFile In objFolder.Files
            If objFile.DateLastAccessed < (Now() - iAge) Then WScript.Echo objfile.Path
    '        If objFile.DateLastAccessed < (Now() - iAge) Then objFile.Delete(True)
        Next    
        
        For Each objSubfolder In objFolder.SubFolders
            Search objSubfolder.Path
        Next
    End Sub

    While testing, leave the "delete" instruction commented out. When posting again please make sure that your script is readable. In its current form it is not.

    Sunday, May 18, 2014 7:36 AM

All replies

  • I assume you are looking for a recursive script, e.g. like this one:

    Const strPath = "C:\Test"
    Const iAge = 30
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Search (strPath)

    Sub Search(sPath)    
        Set objFolder = objFSO.GetFolder(sPath)    
        For Each objFile In objFolder.Files
            If objFile.DateLastAccessed < (Now() - iAge) Then WScript.Echo objfile.Path
    '        If objFile.DateLastAccessed < (Now() - iAge) Then objFile.Delete(True)
        Next    
        
        For Each objSubfolder In objFolder.SubFolders
            Search objSubfolder.Path
        Next
    End Sub

    While testing, leave the "delete" instruction commented out. When posting again please make sure that your script is readable. In its current form it is not.

    Sunday, May 18, 2014 7:36 AM
  • Dear Frederik,

    Thank you I will test your script out now and verify the results here shortly.

    Apologies for the unreadable code I posted, I have now edited the post, and hopefully it is clearer.

    Thanks,

    Sunday, May 18, 2014 8:49 AM
  • Hi Frederick,

    I ran the script and it works in deleting the files in root and sub folder, however it seems to require human input for each deletion? A Windows Scripting Host popup appears with the file path, and after pressing OK, the file gets deleted. 

    Sunday, May 18, 2014 9:17 AM
  • Hi Frederick,

    I ran the script and it works in deleting the files in root and sub folder, however it seems to require human input for each deletion? A Windows Scripting Host popup appears with the file path, and after pressing OK, the file gets deleted. 


    You need to remove the debugging code consisting of the line that contains the instruction "wscript.echo". I also recommend that you do not just "use" the script but spend a little time in trying to understand how it works.
    Sunday, May 18, 2014 10:00 AM
  • Thanks Frederik,

    I get the script, its similar to the original one I was using, just wasnt sure about the debugging part. 

    Works like a charm now, thank you very much, I appreciate it!

    Sunday, May 18, 2014 12:56 PM