我可以将参数传递给 VBScript(使用 cscript 启动的 vbs 文件)吗?

发布于 2024-08-31 20:53:25 字数 517 浏览 13 评论 0原文

我将此脚本保存在“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

巾帼英雄 2024-09-07 20:53:25

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

调用脚本:

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

在脚本内部:

Set File = FSO.OpenTextFile(WScript.Arguments(0) &"\test.txt", 2, True)

不要忘记检查是否确实有参数传递给脚本。您可以通过检查 Count 属性来完成此操作:

if WScript.Arguments.Count = 0 then
    WScript.Echo "Missing parameters"
end if

如果关闭文件后脚本已结束,则无需将变量设置为 Nothing。当cscript.exe进程终止时,资源将被自动清理。通常仅当您明确希望在脚本执行期间释放资源时才需要将变量设置为 Nothing。在这种情况下,您可以将包含对 COM 对象的引用的变量设置为 Nothing,这将在脚本终止之前释放 COM 对象。这只是对您的奖金问题的简短回答,您将在这些相关问题中找到更多信息:

是否需要在 VBA 函数内将对象设置为 Nothing

何时必须将变量设置为“Nothing” ” 在 VB6 中?

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?

南笙 2024-09-07 20:53:25

在 VBS 内部,您可以访问参数

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

等等。参数个数:

Wscript.Arguments.Count

Inside of VBS you can access parameters with

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

and so on. The number of parameter:

Wscript.Arguments.Count
瑕疵 2024-09-07 20:53:25

通过命令行传递的每个参数都可以通过以下方式访问: Wscript.Arguments.Item(0) 其中零是参数编号:即 0、1、2、3 等。

因此,在您的代码中您可以:

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

使用 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:

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

冷…雨湿花 2024-09-07 20:53:25

您还可以使用命名参数,它们是可选的并且可以按任何顺序给出。

Set namedArguments = WScript.Arguments.Named

这是一个小辅助函数:

Function GetNamedArgument(ByVal argumentName, ByVal defaultValue)
  If WScript.Arguments.Named.Exists(argumentName) Then
    GetNamedArgument = WScript.Arguments.Named.Item(argumentName) 
  Else  
    GetNamedArgument = defaultValue
  End If
End Function

VBS 示例:

'[test.vbs]
testArg = GetNamedArgument("testArg", "-unknown-")
wscript.Echo now &": "& testArg

用法示例:

test.vbs /testArg:123

You can also use named arguments which are optional and can be given in any order.

Set namedArguments = WScript.Arguments.Named

Here's a little helper function:

Function GetNamedArgument(ByVal argumentName, ByVal defaultValue)
  If WScript.Arguments.Named.Exists(argumentName) Then
    GetNamedArgument = WScript.Arguments.Named.Item(argumentName) 
  Else  
    GetNamedArgument = defaultValue
  End If
End Function

Example VBS:

'[test.vbs]
testArg = GetNamedArgument("testArg", "-unknown-")
wscript.Echo now &": "& testArg

Example Usage:

test.vbs /testArg:123
惟欲睡 2024-09-07 20:53:25

要回答您的额外问题,一般答案是否定的,您不需要在像您这样的由 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文