I need a script to delete multiple folders called from a text file. S.Mani provided similar code to delete files older than x days from multiple folders called from a text file. I have tried to modify it but no success. I am hoping that someone with more scripting knowledge than I can help me with this.
I have a folder like C:\folders that contains thousands of folders. The folders to delete are in a text file with just the folder name such as "folder1" "folder2" etc with each item on a new line.
I like the delete older than function that S.Mani uses but that is not necessary for this task.
Here is S.Mani's code....
'*************************
**********
**********
**********
**********
**********
**********
**********
**********
******
Dim fso, startFolder, OlderThanDate
Dim strFilename, strPath
Set fso = CreateObject("Scripting.Fi
leSystemOb
ject")
strFilename = "C:\Path.txt"
OlderThanDate = DateAdd("d", -7, Date) ' 30 days (adjust as necessary)
set objFSO = CreateObject("Scripting.Fi
leSystemOb
ject")
if objFSO.FileExists(strFilen
ame) Then
set objFile = objFSO.GetFile(strFilename
)
Dim TextStream
Set TextStream = objFile.OpenAsTextStream(1
,0)
Dim Line,i
Do While Not TextStream.AtEndOfStream
strPath = trim(TextStream.readline)
DeleteOldFiles strPath, OlderThanDate
Loop
Set TextStream = nothing
Else
Msgbox "Input File not found"
End If
Msgbox "Done..!"
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
'*************************
**********
**********
**********
**********
**********
**********
**********
**********
****