Pass a parameter into a VB Script

morinia
morinia used Ask the Experts™
on
How do I pass a parameter into a VB Script.  I have the following  VB Script and would like to pass the folder name as a parapeter?

Set objExcel = CreateObject("Excel.Application")
strSheetName = "Sheet1"
Set objShell = CreateObject ("Shell.Application")
Set objFolder = objShell.Namespace ("C:\Documents and Settings\George A. Blocker\My Documents\Question_9")
Todays_Date = FormatDateTime(Now(),1)
For Each strFileName in objFolder.Items
    MsgBox strFileName
    objExcel.Workbooks.open(objFolder & "\" & strFileName )
    objExcel.Cells(1, 1) = Todays_Date
    With objExcel.ActiveWorkbook
        .Worksheets("Sheet1").Name = "Today"
        .SaveAs objFolder & "\" & strFileName & "_today"
        .Close
    End With
  Next





Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Just add something like this near the top:

If WScript.Arguments.Count > 0 Then
 FolderName = WScript.Arguments.Item(0)
Else
 Wscript.Echo "Please specify folder name."
 Wscript.Quit
End If

Then reference the FolderName varible in your code.  More info here:

http://technet.microsoft.com/en-us/library/ee156618.aspx

~bp
moriniaAdvanced Analytics Analyst

Author

Commented:
billprew:

What this be the command in the batch file be to execute the vb script with the parameters.

Question_9a.vbs("C:\Documents and Settings\George A. Blocker\My Documents\Question_9")
Bill PrewTest your restores, not your backups...
Top Expert 2016
Commented:
Try

Question_9a.vbs "C:\Documents and Settings\George A. Blocker\My Documents\Question_9"

or

cscript Question_9a.vbs "C:\Documents and Settings\George A. Blocker\My Documents\Question_9"

Also, you may want to display the parm in your code as a double check, I think the quotes end up part of the parm if I recall.  But if you need to, you can do a Replace() to remove them.

~bp
Top Expert 2010
Commented:
Hello morinia,

Expanding a bit on billprew's example, last year I had a project in which a VBScript was used to manage a process,
and that VBScript took three parameters, one required and two optional.  It's launched this way (some names
obfuscated) from the command prompt:

c:\MyScript.vbs /AutoReportGroupName:"Name of Report" /StartDate:"1 Jan 2010" /EndDate:"31 Jan 2010"

If the AutoReportGroupName parameter is not provided, the script aborts and writes a message to its log file.
If either the StartDate or EndDate parameter is missing, the code has its own rules to calculate what it should
be.  (If both are present then the script uses the parameter values.  If either is omitted, the script calculates
both.)

Here is the part of the code that validates the parameters:




' Prepare to check command line arguments

Set NamedArguments = WScript.Arguments.Named

' Validate command line arguments

If Not NamedArguments.Exists("AutoReportGroupName") Then
    ts.WriteLine "Error: AutoReportGroupName argument not provided"
    Escape = True
' The code below will check to see if either the StartDate or EndDate arguments are missing.  If either
' is missing, calculate the start and end dates for the report.  If the current system date is on or after
' the 3rd Wed of the current month, then use that date as the start.  If not, use the 3rd Wed of the previous
' month.  We do this because DTS is scheduled to run the 3rd Sunday, and we want to make sure we do not "run out"
' of data loaded in the application
' Note that the application does not mind getting the report dates in mid week, as it always "snaps" the reports to full weeks
ElseIf Not NamedArguments.Exists("StartDate") Or Not NamedArguments.Exists("EndDate") Then
    If Date >= NthWeekday(3, 4, Month(Date), Year(Date)) Then
        StartDate = NthWeekday(3, 4, Month(Date), Year(Date))
        EndDate = NthWeekday(3, 5, Month(Date), Year(Date))
    Else
        StartDate = NthWeekday(3, 4, Month(Date) - 1, Year(Date))
        EndDate = NthWeekday(3, 5, Month(Date) - 1, Year(Date))
    End If
    StartDate = Right("0" & Month(StartDate), 2) & "/" & Right("0" & Day(StartDate), 2) & "/" & Right(Year(StartDate), 2)
    EndDate = Right("0" & Month(EndDate), 2) & "/" & Right("0" & Day(EndDate), 2) & "/" & Right(Year(EndDate), 2)
    AutoReportGroupName = NamedArguments("AutoReportGroupName")
ElseIf Not IsDate(NamedArguments("StartDate")) Then
    ts.WriteLine "Error: StartDate argument (" & NamedArguments("StartDate") & ") not a valid date value"
    Escape = True
ElseIf Not IsDate(NamedArguments("EndDate")) Then
    ts.WriteLine "Error: EndDate argument (" & NamedArguments("EndDate") & ") not a valid date value"
    Escape = True
Else
    StartDate = CDate(NamedArguments("StartDate"))
    EndDate = CDate(NamedArguments("EndDate"))
    If EndDate < StartDate Then
        ts.WriteLine "Error: EndDate argument (" & NamedArguments("EndDate") & ") earlier than StartDate (" & _
            NamedArguments("EndDate") & ")"
        Escape = True
    Else
        StartDate = Right("0" & Month(StartDate), 2) & "/" & Right("0" & Day(StartDate), 2) & "/" & Right(Year(StartDate), 2)
        EndDate = Right("0" & Month(EndDate), 2) & "/" & Right("0" & Day(EndDate), 2) & "/" & Right(Year(EndDate), 2)
        AutoReportGroupName = NamedArguments("AutoReportGroupName")
    End If
End If



(NthWeekday is a UDF defined further down in the code.)

Regards,

Patrick

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial