如何将字符串变量从vbs文件返回到批处理文件

时间:2018-03-14 01:44:59

标签: windows batch-file cmd vbscript echo

我有一个运行的批处理文件并调用名为“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条件中的批处理文件如图所示。

由于

2 个答案:

答案 0 :(得分:1)

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

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

BAT:

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.

答案 1 :(得分:0)

在VBScript中使用wscript.echo,当与CScript一起运行时写入StdOut(与msgbox一起运行时为wscript

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

请参阅for /?