Hello.
The following sub deletes files in the root. How can I build this out so that expired files in subfolders are deleted as well?
I have several main folders at the top level and each main folder has 2 subfolders. Those subfolders contain the files.
Public Module deleteFiles
Public Sub deleteExpFiles()
Dim fso, Folder, File1, FileCount
Dim intDel = 2
fso = CreateObject("Scripting.FileSystemObject")
'set this as a case statement to loop through all folders
Folder = fso.GetFolder("C:\Documents and Settings\orbit\My Documents\rotation")
FileCount = Folder.Files
'Create log file and then delete file
Dim objOutputFile = fso.CreateTextFile("C:\Documents and Settings\orbit\My Documents\rotation\ExpiredFiles.log")
With objOutputFile
.WriteLine("=======================================================")
.WriteLine("Expired Files for " & Date.Now)
.WriteLine("=======================================================")
.WriteLine()
End With
'loop
For Each File1 In FileCount
'delete files that are older than 7 days, for testing, do 3
If DateDiff("d", File1.DateLastModified, Now) > intDel Then
objOutputFile.WriteLine("Deleted " & vbCrLf & File1.Path & vbCrLf & "Original file created on " & File1.DateLastModified & vbCrLf & "Removed at " & Date.Now & vbCrLf & vbCrLf)
File1.Delete()
End If
Next
'end writing to log file and loop
End Sub
End Module
The following sub deletes files in the root. How can I build this out so that expired files in subfolders are deleted as well?
I have several main folders at the top level and each main folder has 2 subfolders. Those subfolders contain the files.
Public Module deleteFiles
Public Sub deleteExpFiles()
Dim fso, Folder, File1, FileCount
Dim intDel = 2
fso = CreateObject("Scripting.FileSystemObject")
'set this as a case statement to loop through all folders
Folder = fso.GetFolder("C:\Documents and Settings\orbit\My Documents\rotation")
FileCount = Folder.Files
'Create log file and then delete file
Dim objOutputFile = fso.CreateTextFile("C:\Documents and Settings\orbit\My Documents\rotation\ExpiredFiles.log")
With objOutputFile
.WriteLine("=======================================================")
.WriteLine("Expired Files for " & Date.Now)
.WriteLine("=======================================================")
.WriteLine()
End With
'loop
For Each File1 In FileCount
'delete files that are older than 7 days, for testing, do 3
If DateDiff("d", File1.DateLastModified, Now) > intDel Then
objOutputFile.WriteLine("Deleted " & vbCrLf & File1.Path & vbCrLf & "Original file created on " & File1.DateLastModified & vbCrLf & "Removed at " & Date.Now & vbCrLf & vbCrLf)
File1.Delete()
End If
Next
'end writing to log file and loop
End Sub
End Module