I'm trying to create a window with 3 buttons using vb script and batch.
I want to enable a different custom action depending on what button is clicked.
All 3 buttons result in the same action.
What am I doing wrong?
Code below@ECHO OFF
echo WScript.Quit MsgBox("Do you want to run the program now?", vbYesNoCancel) > #.vbs
cscript //nologo #.vbs
@echo %errorlevel%
del #.vbs
If ERRORLEVEL 0 goto yes
If ERRORLEVEL 1 goto no
If ERRORLEVEL 2 goto cancel:yes
echo msgbox "yes clicked!" ,64, "Information" > %tmp%\yes.vbs
cscript /nologo %tmp%\yes.vbs
del %tmp%\yes.vbs
del #.vbs
Exit:no
echo msgbox "no clicked!" ,64, "Information" > %tmp%\no.vbs
cscript /nologo %tmp%\no.vbs
del %tmp%\no.vbs
del #.vbs
Exit:cancel
echo msgbox "cancel clicked!" ,64, "Information" > %tmp%\cncl.vbs
cscript /nologo %tmp%\cncl.vbs
del %tmp%\cncl.vbs
del #.vbs
pausemessage edited by tim5211
fixed! @ECHO OFF
echo WScript.Quit MsgBox("Do you want to run the program now?", vbYesNoCancel) > #.vbs
cscript //nologo #.vbs
If "%ERRORLEVEL%"=="6" goto yes
If "%ERRORLEVEL%"=="7" goto no
If "%ERRORLEVEL%"=="2" goto cancel
::errorlevel gets reset by the del command 1 line below.
del #.vbs
echo failure...
exit/b
:yes
echo msgbox "yes clicked!" ,64, "Information" > %tmp%\yes.vbs
cscript /nologo %tmp%\yes.vbs
del %tmp%\yes.vbs
del #.vbs
Exit/b
:no
echo msgbox "no clicked!" ,64, "Information" > %tmp%\no.vbs
cscript /nologo %tmp%\no.vbs
del %tmp%\no.vbs
del #.vbs
Exit/b
:cancel
echo msgbox "cancel clicked!" ,64, "Information" > %tmp%\cncl.vbs
cscript /nologo %tmp%\cncl.vbs
del %tmp%\cncl.vbs
del #.vbs
pause
exit/bi5-6600K[delid]@4.8GHz/4.4GHz@1.424v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-30 1T 2800MHz@1.37v
MSI Armor RX 570 4GB@1415Mhz core@1.2v/1920MHzmessage edited by hidde663
try replacing: If ERRORLEVEL 0 goto yes
If ERRORLEVEL 1 goto no
If ERRORLEVEL 2 goto cancelwith:
if %errorlevel%==0 goto yes
if %errorlevel%==1 goto no
if %errorlevel%==2 goto cancelcould be that batch only accepts %1 variables, i can check for you. but try the above first.
i5-6600K[delid]@4.8GHz/4.4GHz@1.424v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-30 1T 2800MHz@1.37v
MSI Armor RX 570 4GB@1415Mhz core@1.2v/1920MHz
Hi hidde663.
Thanks for your reply and input!I tested the batch file again with what you provided, but I'm afraid the result is still the same.
All 3 buttons trigger a YES response
This was all I needed to modify from your code, and then it seemed to work: If %ERRORLEVEL%==6 goto yes
If %ERRORLEVEL%==7 goto no
If %ERRORLEVEL%==2 goto cancelThe syntax of "IF ERRORLEVEL xx" can be confusing, because this rendition means "if errorlevel >= xx then...", NOT "if errorlevel = xx". So you have to be very careful how you arrange your code using this syntax. Hide66 has it right to just use %errorlevel% syntax. It is much less prone to error and misinterpretation.
message edited by nbrane
Hi again. I tried your modified code in my script, but I still keep getting YES for every option.
I can't get it to work
I also changed the variables slightly based on another previous script I made.
Same Result though.
if %ERRORLEVEL% EQU 6 goto yes
if %ERRORLEVEL% EQU 7 goto no
if %ERRORLEVEL% EQU 2 goto cancel
fixed! @ECHO OFF
echo WScript.Quit MsgBox("Do you want to run the program now?", vbYesNoCancel) > #.vbs
cscript //nologo #.vbs
If "%ERRORLEVEL%"=="6" goto yes
If "%ERRORLEVEL%"=="7" goto no
If "%ERRORLEVEL%"=="2" goto cancel
::errorlevel gets reset by the del command 1 line below.
del #.vbs
echo failure...
exit/b
:yes
echo msgbox "yes clicked!" ,64, "Information" > %tmp%\yes.vbs
cscript /nologo %tmp%\yes.vbs
del %tmp%\yes.vbs
del #.vbs
Exit/b
:no
echo msgbox "no clicked!" ,64, "Information" > %tmp%\no.vbs
cscript /nologo %tmp%\no.vbs
del %tmp%\no.vbs
del #.vbs
Exit/b
:cancel
echo msgbox "cancel clicked!" ,64, "Information" > %tmp%\cncl.vbs
cscript /nologo %tmp%\cncl.vbs
del %tmp%\cncl.vbs
del #.vbs
pause
exit/bi5-6600K[delid]@4.8GHz/4.4GHz@1.424v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-30 1T 2800MHz@1.37v
MSI Armor RX 570 4GB@1415Mhz core@1.2v/1920MHzmessage edited by hidde663
I went back and reviewed my code, and sure enough, I had "rem'd" out the 'del #.vbs' there at the top, leaving it to be done in the three branches. I should have posted my whole code, but I forgot I'd made that change. Oh well, looks like things are swimming now.
Thanks for all your help, input & feedback both hidde663 & nbrane! The script is now fully functional & I've started incorporating it into some programs I'm developing.
I'm still in the process of learning advanced Batch commands and learning how vb-script works.
Also looking into developing HTA applications as well in the near future.