我可以通过一个参数一个VBScript(cscript的发射VBS文件)? [英] Can I pass an argument to a VBScript (vbs file launched with cscript)?

查看:226
本文介绍了我可以通过一个参数一个VBScript(cscript的发射VBS文件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此脚本保存在test.vbs

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

当我运行该脚本,我想通过workFolder变量的值。

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

我怎样才能做到这一点?我可以做吗?类似的cscript test.vbs workFolder:C:\\ TEMP \\'?也许

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

奖金的问题:是否neccessary清理,通过设置workFolder =无还是做的VBScript自动终止时所传递的变量?也许设置文件=什么和设置FSO =无是不必要也?请让我知道,如果你知道答案,这两个问题。

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.

推荐答案

您可以使用 WScript.Arguments 来访问传递给脚本的参数。

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

调用脚本:

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

在你的脚本:

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

如果你的脚本是在你关闭后,该文件,那么就没有必要将变量设置为没有。资源将在Cscript.exe将进程终止自动清理。一个变量设置为没有通常只需要你明确你的脚本的执行过程中要释放资源。在这种情况下,您将设置包含一个引用一个COM对象没有,脚本终止之前这将释放COM对象变量。这只是一个简短的回答您的奖金问题,你会发现在这些相关的问题的详细信息:

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:

<一个href=\"http://stackoverflow.com/questions/517006/is-there-a-need-to-set-objects-to-nothing-inside-vba-functions\">Is有没有必要设置对象VBA函数里面没有

我何时必须设置一个变量为Nothing 在VB6?

这篇关于我可以通过一个参数一个VBScript(cscript的发射VBS文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆