-4

I don't know how to make a .bat file that can use vbyes or vbno.

I tried this:

@echo off
color 0a
cls
echo Hi %USERNAME%
pause >nul

echo a = msgbox("Hello",4+16,"Hi bruh")>hi.vbs
start hi.vbs
if a = vbYes then goto hello2
else
goto hello1
pause >nul

:hello
echo Hello1
pause >nul
exit

:hello2
echo Hello2
pause >nul
exit

But it didn't work. What am I doing wrong?

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 1
    Given a *properly written* .vbs file, you can get it to return a value: [How do I return an exit code from a VBScript console application](http://stackoverflow.com/q/187040/1115360). You can then check that value in the batch file with `errorlevel`: [using errorlevel in a batch file to know if a program exited normally](http://stackoverflow.com/a/17076252/1115360). If that is what you are trying to do. – Andrew Morton Apr 11 '17 at 17:04

2 Answers2

1

You can't use VBScript constants or variables in batch files. You need to have the VBScript return the status code of the MsgBox as its exit code and then evaluate the %errorlevel% variable in your batch script:

>hi.vbs echo a = MsgBox("Hello",4+16,"Hi bruh")
>>hi.vbs echo WScript.Quit a

cscript //nologo hi.vbs
set "exitcode=%errorlevel%"

if %exitcode% equ 6 then goto hello2
if %exitcode% equ 7 then goto hello1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

You can not directly use VBScript constants nor code in batch files; you need to explicitly define the values of the desired constants and create a .VBS file separated from the .BAT one. However, there is another programming language similar to VBScript in several aspects, called JScript, that can be used in the same Batch .BAT file via a simple trick; you just need to enclose the Batch code between these lines:

@set @x=0 /*


rem End of Batch section */

... and place the JScript code at end of the file. For example:

@set @x=0 /*

@echo off
color 0a
cls
echo Hi %USERNAME%
pause >nul

rem Define some useful constants
set /A vbYes=6, vbNo=7

rem Execute *this same* Batch file as a JScript one
cscript //nologo //E:JScript "%~F0"
if %errorlevel% == %vbYes% (
   echo Hello2 = Yes
) else (
   echo Hello1 = No
)
pause >nul
exit

rem End of Batch section */

// Start of JScript section

// Usage of Popup method: .Popup(strText,[nSecondsToWait],[strTitle],[nType])

var WshShell = WScript.CreateObject("WScript.Shell");
WScript.Quit(WshShell.Popup("Hello",0,"Hi bruh",4+16));

Many facilities available in VBScript are very similar that those in JScript and just differ in details. For example, the VBScript msgbox is called Popup method in JScript, so the conversion of short code sections from one language to the other one should have no problems...

Aacini
  • 65,180
  • 12
  • 72
  • 108