PDA

View Full Version : trying to run .vbs script from the command line


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?

Bondo
01-19-2008, 11:02 PM
First:
If it was C, then argument 0 is the program that was called, 1 is the first argument, and 2 is the second. May or may not be this issue, sounds as though it isn't if you hardcode the 7.

Off the top of my head - isn't that the "val" function? that takes a string and returns the numeric value?

dan_nc
01-20-2008, 01:52 AM
It has been an extremely long time since did anything with VBScript, but off the top of my head, you're getting that error because wscript.Arguments.Item(1) is a string and not an object. Try leaving out the keyword "set"

i.e.

Instead of

Set DaysOld = wscript.Arguments.Item(1)

Use

DaysOld = wscript.Arguments.Item(1)

you can use CInt() to convert the string to Int.

daveh0
01-20-2008, 11:33 AM
Try leaving out the keyword "set"

That did the trick! Thanks.