Jump to content

Recommended Posts

Posted

For the life of me I cannot seem to get the following "simple" code to work in a batch file.

After reading all about return codes for the MSGBOX buttons, I thought that a simple if errorllevel = "" goto would work.

 

I must stress that I am no coder !!!!

 

If anybody could please tell me where my logic is going wrong I would be most grateful.

 

Batch file is as follows:

 

echo msgbox"Operation will be executed - Do you want to continue ?",vbInformation + vbyesno,"Event log" > "%temp%\popup.vbs"

wscript.exe "%temp%\popup.vbs"

 

if errorlevel 7 goto no

if errorlevel 6 goto yes

 

:no

echo msgbox"Operation cancelled",vbinformation,"Event log" > "%temp%\popup.vbs"

wscript.exe "%temp%\popup.vbs"

goto end

 

:yes

----- Operation command goes in here -----

 

echo msgbox"Operation completed - Press OK to exit",vbinformation,"Event log" > "%temp%\popup.vbs"

wscript.exe "%temp%\popup.vbs"

 

:end

 

Many thanks in advance

Posted

I only made it a batch file for simplicity plus the fact I know nothing about Powershell. The closest I got to programming was RPG400 back in the good old days !!!

I do have a workaround using command MSGBOX.EX, (found online) this, with YESNO parameter in the command line handles the errorlevels as above.

But it would be interesting to know why the return codes from the buttons in MSGBOX don't behave in the same way.

Thank you anyway for your prompt reply.

Posted (edited)

Try this:

 

@echo off
SETLOCAL
ECHO WScript.Quit MsgBox("Operation will be executed - Do you want to continue ?",vbInformation + vbyesno,"Event log") > "%temp%\popup.vbs"
wscript.exe "%temp%\popup.vbs"

ECHO %errorlevel%

if errorlevel 7 goto no
if errorlevel 6 goto yes

:no
ECHO msgbox "Operation cancelled",vbinformation,"Event log" > "%temp%\popup.vbs"
wscript.exe "%temp%\popup.vbs"
GOTO end

:yes

ECHO "----- Operation command goes in here -----"

ECHO msgbox "Operation completed - Press OK to exit",vbinformation,"Event log" > "%temp%\popup.vbs"
wscript.exe "%temp%\popup.vbs"

:end

 

It seems like the key is to call WScript.Quit with an argument to use as the return value. In this case, we're using the return value from MsgBox() (the 6 or 7), and passing that through as the return value of the whole script.

Edited by webman
  • Thanks 2
Posted

Thank you so much for that - works a treat and I believe I understand explanation for the handing of the codes.

You never know, this might rekindle the programmer of days gone by !!!

 

Thanks again and have a great day.

Posted

Just came back to this - composed most of this reply before noticing Webman had already replied so I'm going to post it anyway!

 

As Webman has said the issue was because the temp vbs script you create to display the messagebox wasn't returning an exit code. The batch script cannot access the variables from the temporary vbs file, so the batch script side of things was unable to access the answer to the messagebox displayed to users from the vbs file.

 

This is a slightly different approach: line 1 creates the temp vbs file, adding a line to the file to display the messagebox and stores the users response in a variable called 'a', line 2 adds another line to the vba file that calls WScript.Quit and returns the content of variable 'a' as the exit code. line 3 runs the temporary vbs created in lines 1&2, line 4 then assigns the exit code from the temporary vbs to a variable that can be used by the batch script: errorlevel

 

echo a = msgbox("Operation will be executed - Do you want to continue ?",vbInformation + vbyesno,"Event log") > "%temp%\popup.vbs"
echo WScript.Quit a >> "%temp%\popup.vbs"

wscript.exe "%temp%\popup.vbs"
set "exitcode=%errorlevel%"

if errorlevel 7 goto no
if errorlevel 6 goto yes

:no
echo msgbox"Operation cancelled",vbinformation,"Event log" > "%temp%\popup.vbs"
wscript.exe "%temp%\popup.vbs"
goto end

:yes
----- Operation command goes in here -----
echo msgbox"Operation completed - Press OK to exit",vbinformation,"Event log" > "%temp%\popup.vbs"
wscript.exe "%temp%\popup.vbs"

:end

 

 

  • Thanks 1
Posted

You're very welcome! Enjoy the rest of your day too :)

 

I started messing around with batch files and Windows scripting when I first got into computers around 25 years ago, and now I build websites & software for a living. So you never know :)

Posted

Many thanks for the code and insight ... I have been able to adjust the numerous batch files accordingly ..... apart from one, which would certainly benefit from some sort of new line command to make the message shall we say a little more aesthetically pleasing ?

I have tinkered about with vbcrlf, vbnewline and even /n in the relevant positions within the maessage body but only see the commands as they are written in.

I'm guessing there is another tweak required somewhere.

 

This is definitely not a matter of life or death .... but again an enquiring mind !

 

Code as follows using your fix.

 

echo a = msgbox("Off-site backup operation selected - ensure drive is connected to USB. Operation will take approx 1hr 30mins so please do not switch off or use the computer until verification message/email is received - Do you want to continue ?",vbInformation + vbyesno,"Event log") > "%temp%\popup.vbs"

echo WScript.Quit a >> "%temp%\popup.vbs"

 

wscript.exe "%temp%\popup.vbs"

set "exitcode=%errorlevel%"

 

if errorlevel 7 goto no

if errorlevel 6 goto yes

 

:no

echo msgbox"Operation cancelled",vbinformation,"Event log" > "%temp%\popup.vbs"

wscript.exe "%temp%\popup.vbs"

goto end

 

:yes

----- Operation command goes in here -----

echo msgbox"Operation completed - Press OK to exit",vbinformation,"Event log" > "%temp%\popup.vbs"

wscript.exe "%temp%\popup.vbs"

 

:end

 

Many thanks again

  • Thanks 1
Posted (edited)

Seems you can use vbCrLf, but the issue was possibly the way batch file was echoing to the temporary vbs file - from my testing you need to use the caret character as the batch escape character and ampersand to concatenate the vbCrLf to the text echoed to the temporary vbs file: ^& vbCrLf ^&

 

This works for me, give it a go - I think you'll see the changes made a bit clearer than I've just tried to explain:

echo a = msgbox("Off-site backup operation selected - ensure drive is connected to USB." ^& vbCrLf ^& vbCrLf ^& "Operation will take approx 1hr 30mins" ^& vbCrLf ^& "Please do not switch off or use the computer until verification message/email is received" ^& vbCrLf ^& vbCrLf ^& "Do you want to continue ?",vbInformation + vbyesno,"Event log") > "%temp%\popup.vbs
echo WScript.Quit a >> "%temp%\popup.vbs"

wscript.exe "%temp%\popup.vbs"
set "exitcode=%errorlevel%"

if errorlevel 7 goto no
if errorlevel 6 goto yes

:no
echo msgbox "Operation cancelled",vbinformation,"Event log" > "%temp%\popup.vbs"
wscript.exe "%temp%\popup.vbs"
goto end

:yes
----- Operation command goes in here -----
echo msgbox"Operation completed" ^& vbCrLf ^& vbCrLf ^& "Press OK to exit",vbinformation,"Event log" > "%temp%\popup.vbs"
wscript.exe "%temp%\popup.vbs"

:end

Edited by ThomL
  • Thanks 1
Posted

Once again thank you ... it works fine.

After ages spent trying to get to the bottom of the problem yesterday, nowhere on the net offered this answer !!! The problem is that there is probably too much imfo out there which some of the time is contradictory and largely unintelligable, at least to a non-scripter like myself.

So, I can now complete the batch file tasks and thank both yourself an webman for your time and most importantly concise explanations.

Have a good day.

  • Thanks 2

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...