patriciaxxx
Programmer
I have the following files in a folder. The number portion represents a date as ddmmyy. I need to read them in date order so that I can delete the oldest ones leaving the 2 newest.
TrackBkp120113.mdb
TrackBkp120210.mdb
TrackBkp120212.mdb
TrackBkp120811.mdb
TrackBkp050113.mdb
TrackBkp120910.mdb
TrackBkp120912.mdb
Here is my code in an MS Access module. The problem with my code is it works by reading the number portion as a number and not a Date which means the correct files are not deleted. I have tried using CDate and Format and DateSerial functions to create an expression without success.
TrackBkp120113.mdb
TrackBkp120210.mdb
TrackBkp120212.mdb
TrackBkp120811.mdb
TrackBkp050113.mdb
TrackBkp120910.mdb
TrackBkp120912.mdb
Here is my code in an MS Access module. The problem with my code is it works by reading the number portion as a number and not a Date which means the correct files are not deleted. I have tried using CDate and Format and DateSerial functions to create an expression without success.
Code:
[COLOR=#204A87] strData = CurrentProject.Path & "\BackupData\TrackBkp120910.mdb"
' Get the name of its folder
strDir = Left(strData, InStrRev(strData, "\BackupData"))
' Now find any existing backups - keep only three
strBkp = Dir(strDir & "BackupData\TrackBkp*.mdb")
Do While Len(strBkp) > 0
intBkp = intBkp + 1
If (strBkp < strLowBkp) Or (Len(strLowBkp) = 0) Then
' Save the name of the oldest backup found
strLowBkp = strBkp
End If
' Get the next file
strBkp = Dir
Loop
' If more than two backup files
If intBkp > 2 Then
' Delete the oldest one
Kill strDir & "BackupData\" & strLowBkp
End If
[/color]