How do I pass Parameters to a vbscript program?

Posted on 2007-07-20
Last Modified: 2010-08-05
I have a small vbscript program that I inherited that is used to delete archive and log files from a directory based on date.  All the directories and "older than"  numbers are hard coded.  I would like to pass parameters to this file but can't figure out how to do it.  %1 doesn't work.  I ordered two vbscript books but they won't be in for awhile and I'd like to keep going.  Can I set a variable in a calling program and read it?  How do I accomplish this?   I want to pass two things.  A directory name and a number that represents the max number of days to retain.  The code is included below.  

myDir = "F:\oracle\backups\NEDARCD\Recent_Exports\"
WScript.Echo " Current Directory is F:\oracle\backups\NEDARCD\Recent_Exports\"

SET filesystem = CreateObject("Scripting.FileSystemObject")
SET folder = filesystem.GetFolder(myDir)

SET filecollection = folder.Files

FOR Each file IN filecollection
     IF DateDiff("d", CreationDate(myDir &, Now()) > 6 THEN
       WScript.Echo DateDif("d", CreationDate(myDir &,Now()) & " " & & " " & CreationDate(myDir &
       DeleteFiles(myDir &
     END IF

SET filesystem=nothing
SET folder=nothing
SET filecollection=nothing

FUNCTION CreationDate(filespec)
  Dim fso, f
  SET fso = CreateObject("ScriptINg.FileSystemObject")
  SET f = fso.GetFile(filespec)
  CreationDate = f.DateCreated
END Function

FUNCTION DeleteFiles(filespec)
  DIM fso
  SET fso = CreateObject("Scripting.FileSystemObject")
END Function

Question by:DonFreeman
    LVL 58

    Accepted Solution

    Add this

    If (WScript.Arguments.Count > 0) Then
          FilesOlderThan = WScript.Arguments(0)
          FilesOlderThan = 6
    End If

    and then later use FilesOlderThan in place of 6.
    LVL 1

    Author Comment

    So wscript.arguments(0) would be my first argument and that would be my directory name and then wscript.arguments(1) would be my second one right?.   I am quitting for the day but I'll get back on it Monday and when I get it working I'll award your points.  It looks like a solid solution.  Thanks!
    LVL 58

    Expert Comment

    Yes. WScript.Arguments.Count is the count of arguments passed. so you can check that too to make sure 2 arguments were indeed passed.
    LVL 1

    Author Comment

    I am still working on this...

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Effective way to iterate over a python list 8 78
    fix34  challenge 9 73
    word0 challenge 4 38
    wordappend challenge 8 56
    When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now