PDA

View Full Version : Solved: fso.GetFile not working!



Ischyros
01-09-2009, 09:11 AM
I have a sub routine that counts the files in a folder and if the # is greater than 15 it deletes the earliest files (by datecreated) until the # is <= 15. The code worked great for about a month, and not I get a runtime erro '53', "File Not Found". The problem is that the file is there, I checked! Any ideas, suggestions, etc. The error occurs on this line "Set f = fso.GetFile(FileName)"

Sub Folder_Maintenance()
Dim fso As Object, ObjFiles As Object, _
Folder As Object, f As Object, indFile, _
Dir As String
Dim FileName As String, MinDC As Double
Dir = "R:\Capital Projects\Status Reports\Project Resource Management Form\Log Files"
Set fso = CreateObject("Scripting.FileSystemObject")
Set Folder = fso.getfolder(Dir)
Set ObjFiles = Folder.Files
While ObjFiles.Count > 15

For Each indFile In ObjFiles
MinDC = indFile.datecreated
FileName = indFile.Name
Exit For
Next

For Each indFile In ObjFiles
If indFile.datecreated < MinDC Then
MinDC = indFile.datecreated
FileName = indFile.Name
'MsgBox (MinDC)
'MsgBox (FileName)
Else
End If
Next
Dim a
Set f = fso.GetFile(FileName)
f.Delete

Wend
End Sub

:think:

Bob Phillips
01-09-2009, 09:31 AM
Sub Folder_Maintenance()
Dim fso As Object, ObjFiles As Object, _
Folder As Object, f As Object, indFile, _
Dir As String
Dim FileName As String, MinDC As Double
Dir = "R:\Capital Projects\Status Reports\Project Resource Management Form\Log Files"
Set fso = CreateObject("Scripting.FileSystemObject")
Set Folder = fso.getfolder(Dir)
Set ObjFiles = Folder.Files
While ObjFiles.Count > 15

For Each indFile In ObjFiles
MinDC = indFile.datecreated
FileName = indFile.Path
Exit For
Next

For Each indFile In ObjFiles
If indFile.datecreated < MinDC Then
MinDC = indFile.datecreated
FileName = indFile.Path
'MsgBox (MinDC)
'MsgBox (FileName)
Else
End If
Next
Dim a
Set f = fso.GetFile(FileName)
f.Delete

Wend
End Sub

Ischyros
01-09-2009, 10:57 AM
Thanks so much....I didn't even think of .Path vs .Name. Works great!

Bob Phillips
01-09-2009, 11:12 AM
It probably worked before because you were already in that directory.