Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 246
Peter Avatar asked May 10 '10 21:05

Peter


People also ask

How do you pass an argument in VBScript?

Passing an argument that starts with “//” to a VBscript, will by default be taken as options for the windows script host itself (cscript or wscript). To avoid this pass a double slash "//" to end the argument parsing of cscript/wscript. Alternatively use a named argument as below.

How do you pass an argument by reference to a function in VBScript?

In VBScript, there are two ways values can be passed: ByVal and ByRef . Using ByVal , we can pass arguments as values whereas with the use of ByRef , we can pass arguments are references.

What does .vbs file do?

A VBS file is a Virtual Basic script written in the VBScript scripting language. It contains code that can be executed within Windows or Internet Explorer, via the Windows-based script host (Wscript.exe), to perform certain admin and processing functions.


2 Answers

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?

like image 112
Dirk Vollmar Avatar answered Sep 24 '22 02:09

Dirk Vollmar


Inside of VBS you can access parameters with

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

and so on. The number of parameter:

Wscript.Arguments.Count 
like image 37
Oleg Avatar answered Sep 25 '22 02:09

Oleg