我可以将参数传递给 VBScript(使用 cscript 启动的 vbs 文件)吗?
我将此脚本保存在“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
当我运行脚本时,我想传递“workFolder”变量的值。
我该怎么做?我可以做吗?也许类似于“cscript test.vbs workFolder:'C:\temp\'”?
额外问题:是否有必要使用“Set workFolder = Nothing”清理传递的变量,还是 VBSCript 在终止时自动执行此操作?也许“Set File = Nothing”和“Set FSO = Nothing”也是不必要的?如果您知道这两个问题的答案,请告诉我。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
WScript.Arguments
访问传递给脚本的参数。调用脚本:
在脚本内部:
不要忘记检查是否确实有参数传递给脚本。您可以通过检查
Count
属性来完成此操作:如果关闭文件后脚本已结束,则无需将变量设置为
Nothing
。当cscript.exe进程终止时,资源将被自动清理。通常仅当您明确希望在脚本执行期间释放资源时才需要将变量设置为Nothing
。在这种情况下,您可以将包含对 COM 对象的引用的变量设置为Nothing
,这将在脚本终止之前释放 COM 对象。这只是对您的奖金问题的简短回答,您将在这些相关问题中找到更多信息: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:在 VBS 内部,您可以访问参数
等等。参数个数:
Inside of VBS you can access parameters with
and so on. The number of parameter:
通过命令行传递的每个参数都可以通过以下方式访问: Wscript.Arguments.Item(0) 其中零是参数编号:即 0、1、2、3 等。
因此,在您的代码中您可以:
使用 wscript.arguments .count,如果有人没有输入正确的值,您可以捕获错误,等等。
MS Technet 示例
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:
Using wscript.arguments.count, you can error trap in case someone doesn't enter the proper value, etc.
MS Technet examples
您还可以使用命名参数,它们是可选的并且可以按任何顺序给出。
这是一个小辅助函数:
VBS 示例:
用法示例:
You can also use named arguments which are optional and can be given in any order.
Here's a little helper function:
Example VBS:
Example Usage:
要回答您的额外问题,一般答案是否定的,您不需要在像您这样的由 Wscript 或 Cscript 调用的简短 .VBS 脚本中将变量设置为“Nothing”。
您可能在较长的脚本中间执行此操作的原因是为了将 VB 本来会占用的内存释放回操作系统。如今,8GB RAM 是典型的,16GB+ 相对常见,这不太可能产生任何可衡量的影响,即使是在单个变量中有几兆字节的巨大脚本上。在这一点上,它有点像您使用 1MB 或 2MB RAM 工作的时代的遗留物。
你是对的,当你的 .VBS 脚本完成时,所有变量都会被销毁,并且内存无论如何都会被回收。将变量设置为“Nothing”只会加速该过程,并允许您在脚本中间执行此操作。
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.