【问题标题】:Retrieving an argument of a VBScript检索 VBScript 的参数
【发布时间】:2016-10-08 16:22:29
【问题描述】:

如何在不使用 cscript.exe 的情况下从 VBScript 传递和返回参数?

例如,我想从 script1 调用 script2,它返回一个值给 script1,而不涉及 cscript.exe。 我搜索了各种答案,但不知何故涉及cscript.exe的使用。

此脚本获取已安装的声音并设置文件voice.txt 中提供的声音。

Set WshShell = CreateObject("WScript.Shell")
WShShell.CurrentDirectory = "..\Confirmatory Texts"

Set FSO = CreateObject("Scripting.FileSystemObject")

If FSO.FileExists("voice.txt") Then
  Set temp = FSO.OpenTextFile("voice.txt")
  confirm_voice = temp.ReadLine()
  temp.Close

  Set Sapi = CreateObject("SAPI.SpVoice")

  For Each Voice In Sapi.GetVoices
    i = i + 1
  Next

  For loopvar = 0 To i-1
    If loopvar = CInt(confirm_voice) Then
      Set Sapi.Voice = Sapi.GetVoices.Item(loopvar)
    End If
  Next
Else
  WScript.Echo "An Error Occured"
End If

如果我从另一个脚本调用这个脚本,我怎样才能让这个脚本返回一些值给调用它的脚本?

【问题讨论】:

  • edit您的问题并提供两个脚本的minimal reproducible example。正如目前所写,您的问题似乎有点不清楚script2 是什么,script1 是什么,他们如何互相称呼等等......为什么不使用wscript.exe 而不是cscript.exe?顺便说一句,Sapi.GetVoices.Item(loopvar) 是一个 对象 ISpeechObjectToken;你不能把它当作一个字符串来操作……
  • VBScript 帮助文件在这里microsoft.com/en-au/download/details.aspx?id=2764。请参阅脚本组件。另一种方法是使用 32 位 CScript 并加载脚本控件,将第二个脚本加载到脚本控件中并执行它。我在这里的回答表明 - stackoverflow.com/questions/26952155/….
  • 我希望上面的脚本返回一些值给调用它的脚本,我该怎么做?顺便说一句,感谢大家回复

标签: vbscript arguments


【解决方案1】:

VBScript 并不真正为其他 VBScript 文件提供调用或导入机制。最接近的是读取另一个文件的内容并通过ExecuteGlobal 运行它们。

演示:

将以下两个文件放在同一个目录下,运行script1.vbs。它将读取script2.vbs 的内容,并通过ExecuteGlobal 运行代码,使函数Square 在全局范围内可用。一旦该函数在全局范围内可用,您就可以在脚本的其余部分中使用它。

script1.vbs:

Set fso = CreateObject("Scripting.FileSystemObject")

dir    = fso.GetParentFolderName(WScript.ScriptFullName)
script = fso.BuildPath(dir, "script2.vbs")

ExecuteGlobal fso.OpenTextFile(script).ReadAll  '"import" code into global scope

WScript.Echo Square(3)

script2.vbs:

Function Square(i)
  Square = i*i
End Function

【讨论】:

  • 感谢您的回答,我会尝试这种方法。
猜你喜欢
  • 2010-09-18
  • 1970-01-01
  • 1970-01-01
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
相关资源
最近更新 更多