[RESOLVED] Deleting files older than x [Resolved]
I have the following code:
VB Code:
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
For i = 0 To CInt(NUMFOLDERS)
While Dir(FARR(i) & "\*.BAK") <> ""
filespec = Dir(FARR(i) & "\*.BAK")
If filespec <> "" Then
Set f = fs.GetFile(FARR(i) & "\" & filespec)
mdate = f.DateLastModified
If mdate < DateAdd("d", -14, Format(Now, "mm/dd/yyyy")) Then
txtLog.Text = txtLog.Text & "delete: " & filespec & vbCrLf
End If
End If
Wend
Next
I'm just dumping the output to at textbox at the moment for testing, but the problem is that it never moves to the next file... it keeps running the same file in the while loop. Why doesn't it move to the next one?
Re: Deleting files older than x
Re: Deleting files older than x
"i" is incremented in the for loop. I'm not worried about that. The Dir function is where my problem is.
Re: Deleting files older than x
Yeah i didn't see that for loop till i had already posted
How are you breaking the while loop?
Re: Deleting files older than x
The syntax looks about right. Try Dir$ instead.
Re: Deleting files older than x
If you use Dir() with a wild card, it will return the first matching file every time i.e. always the same file.
You need to call it repeatedly without any parameters to get subsequent files. After it has done the last one it will return null.
Try this:
VB Code:
Dim fs, f
Dim sFileName As String
Set fs = CreateObject("Scripting.FileSystemObject")
For i = 0 To CInt(NUMFOLDERS)
sFileName = Dir$(FARR(i) & "\*.BAK")
While sFileName <> ""
'Do what you need
sFileName = Dir$()
Wend
Next
Re: Deleting files older than x
Ahh... thanks Brian. That's kinda what I was thinking. It doesn't really matter anymore anyways, because what I was trying to do was keep my SQL Server backups deleted (< 2 or 3 weeks) and I figured out how to do it in my server maintenance plan.
Thanks anyways.