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

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

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.

回答1:

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?



回答2:

Inside of VBS you can access parameters with

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

and so on. The number of parameter:

Wscript.Arguments.Count 


回答3:

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



回答4:

Actually the answer to the bonus question is "No". You don't need to clean up. But general programming practise is to always clean up your things, like after your dinner, pls clean up your plate.



回答5:

In teaching new programmers, the correct answer to "should I clean up?" is always yes. If one is in the habit of cleaning up, then one will clean up. Then when it is actually required (lest one make a mess of the heap, etc.), the cleaning up is habit and will happen.

The better question is "Does it cause a problem if I clean up at the end of this script?" or even better... "Assuming I am continuing on with a longish script (or don't have GC available), is this the correct way to clean up these objects?"

my two cents. Paul



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!