【问题标题】:How to return string variable from vbs file to batch file如何将字符串变量从vbs文件返回到批处理文件
【发布时间】:2023-03-06 05:41:01
【问题描述】:

我有一个批处理文件,它运行并调用名为“SelectNBType.vbs”的 .vbs 文件 提到的 .vbs 文件提示一个小的 UI 输出,供用户输入字符串,如图所示。

///Start of Batch file///
For /F "Tokens=2 Delims=" %%I In ('cscript //nologo SelectNBType.vbs') Do 
Set _NBType=%%I

IF %_NBType%==n (
Echo Normal

)
IF %_NBType%=="c" (
Echo Common
)

还有如下的.VBS

////Start of .VBS file
titletext = "Select Notebook Type"  
prompttext = "Enter NB Type"

NBType = inputbox(prompttext & chr(13) & "   (Type n if NORMAL user NB)" & 
chr(13) & "   (Type n if COMMON/POOL user NB)", titletext)

if NBType = "n" Then
x=MsgBox ("You have selected a Normal Notebook",0+32,"Notification")
WScript.Quit 1

elseif NBType = "c" Then
x=MsgBox ("You have selected a Common/Pool Notebook",0+32,"Notification")

End If

我正在尝试找出一种方法来允许 .vbs 文件将用户输入的输出作为字符串返回到批处理文件,以便以后可以使用它 if条件中的批处理文件如图所示。

谢谢

【问题讨论】:

标签: windows batch-file cmd vbscript echo


【解决方案1】:

您真的需要将选择作为字符串传递吗?如果您只有两个选项,则可以使用 WScript.Quit 1WScript.Quit 2 返回每个选项,并在批处理文件中使用该值,如下所示:

VBS:

titletext = "Select Notebook Type"  
prompttext = "Enter NB Type"

NBType = inputbox(prompttext & chr(13) & "   (Type n if NORMAL user NB)" & chr(13) & "   (Type c if COMMON/POOL user NB)", titletext)

If NBType = "n" Then
   MsgBox "You have selected a Normal Notebook",0+32,"Notification"
   WScript.Quit 1
Elseif NBType = "c" Then
   MsgBox "You have selected a Common/Pool Notebook",0+32,"Notification"
   WScript.Quit 2
End If

英美烟草:

cscript //nologo SelectNBType.vbs
IF %ERRORLEVEL% EQU 1 ECHO You have selected a Normal Notebook.
IF %ERRORLEVEL% EQU 2 ECHO You have selected a Common/Pool Notebook.

【讨论】:

  • 是的,我测试过了。
  • 谢谢,它成功了,你的解决方案可以满足我的需要。但回到问题,有没有办法将字符串从 VBS 传回批处理?
  • 查看其他答案,解决方案就在那里。至少要注意别人给你的东西!
  • @elzzilogico,你提到的答案是在我问完这个问题之后出现的,你需要一颗镇静药!
【解决方案2】:

在使用CScript 运行时写入StdOut 的VBScript 中使用wscript.echo(使用wscript 运行时写入msgbox)。

 For /f "delims=" %%A in (`cscript //nologo c:\file.vbs`) do echo %%A

for /?

【讨论】:

  • 不确定,我正在使用手机,但您的 for 命令引用可能需要在 delims 之前使用 usebackq /跨度>
  • wscript.echo 必须是最后一行,以便 for 获取所需的输出。一个脚本示例就可以了!
猜你喜欢
  • 1970-01-01
  • 2019-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
  • 2010-12-03
  • 2021-02-17
相关资源
最近更新 更多