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
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