daveh0
01-19-2008, 09:10 PM
I was in need of a script that would delete files in directory X that were older than Y days old. The script will be called several times (with different values for X and Y) from within a .bat file run by Task Scheduler. I was able to piece this together...
'delOldFiles.vbs
Main
Sub Main
Dim Fso
Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Folder
Set Folder = Fso.GetFolder(wscript.Arguments.Item(0))
Dim DaysOld
Set DaysOld = wscript.Arguments.Item(1)
Dim Files
Set Files = Folder.files
Dim File
Dim FileDateTime
For Each File In Files
FileDateTime = File.DateLastModified
If DateDiff("D", FileDateTime, Date) >= DaysOld THEN
File.Delete
End If
Next
Set Fso = Nothing
End Sub
I am calling the script like so:
delOldFiles.vbs c:\someDir 7
In doing so, I get the "Windows Script Host" dialog that complains of the error:
Object Required: '[string: "7"]'
and points to the line:
Set DaysOld = wscript.Arguments.Item(1)
If I hard code a value in place of DaysOld, it works like a charm... is there some function that needs to be run on that 2nd argument in order for my script to recognize it as an integer?
'delOldFiles.vbs
Main
Sub Main
Dim Fso
Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Folder
Set Folder = Fso.GetFolder(wscript.Arguments.Item(0))
Dim DaysOld
Set DaysOld = wscript.Arguments.Item(1)
Dim Files
Set Files = Folder.files
Dim File
Dim FileDateTime
For Each File In Files
FileDateTime = File.DateLastModified
If DateDiff("D", FileDateTime, Date) >= DaysOld THEN
File.Delete
End If
Next
Set Fso = Nothing
End Sub
I am calling the script like so:
delOldFiles.vbs c:\someDir 7
In doing so, I get the "Windows Script Host" dialog that complains of the error:
Object Required: '[string: "7"]'
and points to the line:
Set DaysOld = wscript.Arguments.Item(1)
If I hard code a value in place of DaysOld, it works like a charm... is there some function that needs to be run on that 2nd argument in order for my script to recognize it as an integer?