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.
Best Answer
You can use
WScript.Arguments
to access the arguments passed to your script.Calling the script:
Inside your script:
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 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 toNothing
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 toNothing
, 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: