Results 1 to 4 of 4

Thread: trying to run .vbs script from the command line

  1. #1

    trying to run .vbs script from the command line

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

    Code:
    '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:

    Code:
    delOldFiles.vbs c:\someDir 7
    In doing so, I get the "Windows Script Host" dialog that complains of the error:

    Code:
    Object Required: '[string: "7"]'
    and points to the line:

    Code:
    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?

    *************************************************************
    *** c4/c5 incomplete *** Injured in Summer 2003 ***
    *************************************************************

  2. #2
    Senior Member
    Join Date
    Dec 2007
    Location
    Portland, OR
    Posts
    182
    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?

  3. #3
    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.
    Daniel

  4. #4
    Code:
    Try leaving out the keyword "set"
    That did the trick! Thanks.

    *************************************************************
    *** c4/c5 incomplete *** Injured in Summer 2003 ***
    *************************************************************

Similar Threads

  1. Treatment Advocates Plan Run
    By Max in forum Funding, Legislation, & Advocacy
    Replies: 0
    Last Post: 08-23-2007, 09:50 AM
  2. I wanna run away...
    By chick in forum Life
    Replies: 58
    Last Post: 11-29-2005, 08:48 PM
  3. Athletes run, roll to help raise funds for UA's Disability Resource Center
    By bigbob in forum Recreation, Sports, Travel, & Hobbies
    Replies: 0
    Last Post: 10-04-2004, 03:21 PM
  4. Memories of Brian's Run
    By Max in forum Funding, Legislation, & Advocacy
    Replies: 1
    Last Post: 03-29-2004, 09:55 AM
  5. Film tells story of Brian's Run
    By Max in forum Life
    Replies: 0
    Last Post: 06-12-2002, 12:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •