GreenFella
Programmer
Hi.
Really hope someone here can help. I have been struggling with a problem whereby I can delete any number of .txt files from a folder once those files are beyond a certain number of days old.
The code that I have tried using so far is this:
Dim I As Integer
Dim oFSO As FileSystemObject
Dim oFile As File
Dim dteDeleteDate As Date
Dim strFileName As String
dteDeleteDate = DateAdd("d", (5 * -1), Date)
Set oFSO = New FileSystemObject
With application.FileSearch
.NewSearch
.LookIn = "c:\backup"
.SearchSubFolders = False
.FileType = msoFileTypeAllFiles
.Execute (msoSortByFileName)
If .FoundFiles.Count > 0 Then
For I = 1 To .FoundFiles.Count
strFileName = application.FileSearch.FoundFiles(I)
oFSO.GetFileName ("'" & strFileName & "'")
oFile.name = strFileName
If oFile.DateCreated < dteDeleteDate Then
oFSO.DeleteFile strFileName
End If
Next I
End If
End With
Set oFSO = Nothing
I am stuck. I can't seem to relate the files which I am finding in the c:\backup folder with an object that I can then use to check the Date Created for comparison.
Any help would be greatly appreciated.
Thanks
Greenfella
Really hope someone here can help. I have been struggling with a problem whereby I can delete any number of .txt files from a folder once those files are beyond a certain number of days old.
The code that I have tried using so far is this:
Dim I As Integer
Dim oFSO As FileSystemObject
Dim oFile As File
Dim dteDeleteDate As Date
Dim strFileName As String
dteDeleteDate = DateAdd("d", (5 * -1), Date)
Set oFSO = New FileSystemObject
With application.FileSearch
.NewSearch
.LookIn = "c:\backup"
.SearchSubFolders = False
.FileType = msoFileTypeAllFiles
.Execute (msoSortByFileName)
If .FoundFiles.Count > 0 Then
For I = 1 To .FoundFiles.Count
strFileName = application.FileSearch.FoundFiles(I)
oFSO.GetFileName ("'" & strFileName & "'")
oFile.name = strFileName
If oFile.DateCreated < dteDeleteDate Then
oFSO.DeleteFile strFileName
End If
Next I
End If
End With
Set oFSO = Nothing
I am stuck. I can't seem to relate the files which I am finding in the c:\backup folder with an object that I can then use to check the Date Created for comparison.
Any help would be greatly appreciated.
Thanks
Greenfella