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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to sort colFiles by date motified? 1

Status
Not open for further replies.

Richardkl

Technical User
Dec 8, 2001
40
0
0
US
How to can I sort the colFiles by date motified so that the oldest is the first one in the list?

I have created a vb script that deletes files that are older than 30 days and leaves at least 30 files in the folder. But I found a flaw in the logic, the colFiles sorts by default to the "name" field and that causes a problem that the first file in the list can be the newest file (these are backup files that auto name). Even though it is greater than 30 days old, the others are older, it will be the one deleted with the present script. I feel that I don't need to even check how old the file is if I can get the files in "colfiles" sorted to the oldest file to be the first in the list, I can just delete files until there are only 30 left, ANY ideas?

NumberOfDaysOld = 30
NumberOfFilesToKeep = 30
strPath = "d:\qb"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)
Set colFiles = objFolder.Files
For Each objFile In colFiles
filecount = objFolder.Files.Count
If objFile.DateLastModified > (Date - NumberOfDaysOld) And filecount > NumberOfFilesToKeep Then
objFile.Delete
End If
Next

End Sub
 
And the problem is ?
Anyway I'd not create the recordset inside the recursive function ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top