Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete a Text File if it is beyond a certain number of days old 1

Status
Not open for further replies.

GreenFella

Programmer
Oct 23, 2001
41
CA
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
 
Here's a thought. There are other ways to get the date and time of the file created. example

Dim MyStamp
' Assume TESTFILE was last modified on February 12, 1993 at 4:35:47 PM.
' Assume English/U.S. locale settings.
MyStamp = FileDateTime("TESTFILE") ' Returns "2/12/93 4:35:47 PM".

Now all you have to do for a specific deletion date is to use the mid function to shorten the string "mystamp" and then compare it to your delete date. If they match then you can delete the file. it might be worth a try.

Durible Outer Casing to Prevent Fall-Apart
 
one more thing, use the StrComp command to do a text comparison of the two strings. example
Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD": MyStr2 = "abcd" ' Define variables.
MyComp = StrComp(MyStr1, MyStr2, 1) ' Returns 0.

when MyComp = 0 then the two strings, ie the two dates are the same!


Durible Outer Casing to Prevent Fall-Apart
 
Thanks legos.

In the words of Mr. Burns... eeeeeeeeexcellent.

I knew there had to be a way that I was missing but do you think I could find it? Nope.

Thanks again.

Greenfella
 
sorry to take up so much room, but i realized i missed one last thing. You wanted to delete all of the files that are older than the specific date. The reason your function isn't working is because you are either comparing a string to a date or 2 strings to eachother. Heres a few suggestions that might help out on that problem.
1. use the datevalue function to turn a string into a date.
example
Dim MyDate
MyDate = DateValue("February 12, 1969") ' Return a date.
This also works with the 2/12/69 and 2/12/1969 methods of dates. i don't know if the date1<date2 thing will work once they are both dates but it is worth a shot.
2. You can modify the strings and break them into day month and year seperately. Then you can first compare the years, then months, and then day and find out if the file was made before your cut off date.
3. The method you currently have seems almost correct, i'd try using the DateValue function first and then doing the date1<date2 thing you already have. if that won't work the only option i can see is the method mentioned above.

Durible Outer Casing to Prevent Fall-Apart
 
You can try something like this:
Set oFSO = New FileSystemObject
Set oFolder = oFSO.GetFolder("c:\backup")
For Each oFile In oFolder.Files
If oFile.DateCreated < dteDeleteDate Then
oFile.Delete
End If
Next

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top