user1111726
user1111726

Reputation: 157

How to return string variable from vbs file to batch file

I have a batch file that runs and calls .vbs file called "SelectNBType.vbs" The mentioned .vbs file prompt a small UI output for the user to input a string as shown.

///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
)

And the .VBS as below

////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

I am trying to figure out a way to allow the .vbs file to return the output entered by the user as a string to the batch file so that it can be used later in the batch file in the if conditions as shown.

Thanks

Upvotes: 0

Views: 1580

Answers (2)

AMagpie
AMagpie

Reputation: 206

Use wscript.echo in VBScript which write to StdOut when run with CScript (and a msgbox when run with wscript).

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

See for /?.

Upvotes: 0

Regis Desrosiers
Regis Desrosiers

Reputation: 565

Do you really need to pass the selection as a string? If you have only two options, you can return each of them with either WScript.Quit 1 or WScript.Quit 2 and use that value in the batch file like that:

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.

Upvotes: 1

Related Questions