Gunjan
Gunjan

Reputation: 1

Delete files from multiple folders that are 30 days old

I have VB Script which will delete files from folder which is more than 30 days old. My issue is it will delete from only one folder. I need to add multiple paths of folder in one script so that one script can delete files from multiple folders.

Const strPath = "D:\LIMS Testing\"
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Call Search (strPath)

' Comment out below line if you'd like to use this script in windows schedule task
WScript.Echo"Done."

Sub Search(str)
    Dim objFolder, objSubFolder, objFile
    Set objFolder = objFSO.GetFolder(str)
    For Each objFile In objFolder.Files

        ' Use DateLastModified for modified date of a file
        If objFile.DateLastModified < (Now() - 30) Then
            objFile.Delete(True)
        End If

    Next
    For Each objSubFolder In objFolder.SubFolders
        Search(objSubFolder.Path)

        ' Files have been deleted, now see if the folder is empty.
        If (objSubFolder.Files.Count = 0) Then
            objSubFolder.Delete True
        End If

    Next
End Sub

Upvotes: 0

Views: 419

Answers (1)

&#201;tienne Laneville
&#201;tienne Laneville

Reputation: 5031

You can simply call Search for all the folders you want to process:

Search "D:\LIMS Testing\"
Search "D:\some other folder\"
Search "D:\yet another folder\"

If all the folders you want to process are in the same folder already (if they are all under D:\LIMS Testing\ for example), your code will work because the Search subroutine is recursive.

Upvotes: 1

Related Questions