Can I pass an argument to a VBScript (vbs file launched with cscript)?
Asked Answered
P

5

90

I have this script saved in "test.vbs":

Set FSO = CreateObject("Scripting.FileSystemObject")
Set File = FSO.OpenTextFile(workFolder &"\test.txt", 2, True)
File.Write "testing"
File.Close
Set File = Nothing
Set FSO = Nothing
Set workFolder = Nothing

When I run the script I want to pass the value of the "workFolder" variable.

How can I do this? Can I do it? Something like "cscript test.vbs workFolder:'C:\temp\'" perhaps?

Bonus question: Is it neccessary to clean up the passed variable with "Set workFolder = Nothing" or does VBSCript do that automatically when it terminates? Maybe "Set File = Nothing" and "Set FSO = Nothing" is unneccessary also? Please let me know if you know the answer to both these questions.

Pheidippides answered 10/5, 2010 at 21:52 Comment(0)
T
147

You can use WScript.Arguments to access the arguments passed to your script.

Calling the script:

cscript.exe test.vbs "C:\temp\"

Inside your script:

Set File = FSO.OpenTextFile(WScript.Arguments(0) &"\test.txt", 2, True)

Don't forget to check if there actually has been an argument passed to your script. You can do so by checking the Count property:

if WScript.Arguments.Count = 0 then
    WScript.Echo "Missing parameters"
end if

If your script is over after you close the file then there is no need to set the variables to Nothing. The resources will be cleaned up automatically when the cscript.exe process terminates. Setting a variable to Nothing usually is only necessary if you explicitly want to free resources during the execution of your script. In that case, you would set variables which contain a reference to a COM object to Nothing, which would release the COM object before your script terminates. This is just a short answer to your bonus question, you will find more information in these related questions:

Is there a need to set Objects to Nothing inside VBA Functions

When must I set a variable to “Nothing” in VB6?

Tendance answered 10/5, 2010 at 21:56 Comment(2)
Bingo, that's it! Very clear, thanks a lot. (The bonus question is still open in case anyone wants to answer it in one of these comments.)Pheidippides
@Peter: I added a short answer to your bonus question.Tendance
T
26

Inside of VBS you can access parameters with

Wscript.Arguments(0)
Wscript.Arguments(1)

and so on. The number of parameter:

Wscript.Arguments.Count
Tolerate answered 10/5, 2010 at 21:58 Comment(1)
Thank you! (The bonus question is still open in case anyone wants to answer it in one of these comments.)Pheidippides
H
6

Each argument passed via command line can be accessed with: Wscript.Arguments.Item(0) Where the zero is the argument number: ie, 0, 1, 2, 3 etc.

So in your code you could have:

strFolder = Wscript.Arguments.Item(0) 

Set FSO = CreateObject("Scripting.FileSystemObject")
Set File = FSO.OpenTextFile(strFolder, 2, True)
File.Write "testing"
File.Close
Set File = Nothing
Set FSO = Nothing
Set workFolder = Nothing

Using wscript.arguments.count, you can error trap in case someone doesn't enter the proper value, etc.

MS Technet examples

Helicon answered 10/5, 2010 at 21:56 Comment(3)
Aha, thanks man. Interesting that you dont have to create an instance of Wcript. (The bonus question is still open in case anyone wants to answer it in one of these comments.)Pheidippides
1) workFolder is not defined as an object in either the above answer, or the original question, so Set workFolder = Nothing should raise an error. 2) As the Argument is not suggested as needing to be reused more than once, perhaps skip assigning it to the strFolder variable. 3) Would suggest using x instead of 0 in Wscript.Arguments.Item(0)Tuneful
Actually it would not raise an error unless option explicit was set, and in that case nothing would work since nothing has been defined. That is true about the variable however, it isn't needed, then again neither are the FSO or the FILE objects and a simple with could be used. A ton of different approaches possible... :)Helicon
U
5

You can also use named arguments which are optional and can be given in any order.

Set namedArguments = WScript.Arguments.Named

Here's a little helper function:

Function GetNamedArgument(ByVal argumentName, ByVal defaultValue)
  If WScript.Arguments.Named.Exists(argumentName) Then
    GetNamedArgument = WScript.Arguments.Named.Item(argumentName) 
  Else  
    GetNamedArgument = defaultValue
  End If
End Function

Example VBS:

'[test.vbs]
testArg = GetNamedArgument("testArg", "-unknown-")
wscript.Echo now &": "& testArg

Example Usage:

test.vbs /testArg:123
Urfa answered 6/2, 2020 at 23:1 Comment(0)
S
0

To answer your bonus question, the general answer is no, you don't need to set variables to "Nothing" in short .VBS scripts like yours, that get called by Wscript or Cscript.

The reason you might do this in the middle of a longer script is to release memory back to the operating system that VB would otherwise have been holding. These days when 8GB of RAM is typical and 16GB+ relatively common, this is unlikely to produce any measurable impact, even on a huge script that has several megabytes in a single variable. At this point it's kind of a hold-over from the days where you might have been working in 1MB or 2MB of RAM.

You're correct, the moment your .VBS script completes, all of your variables get destroyed and the memory is reclaimed anyway. Setting variables to "Nothing" simply speeds up that process, and allows you to do it in the middle of a script.

Saturant answered 13/11, 2019 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.