Pass errorlevel from one batch file to another?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Clueless in Seattle
Posts: 47
Joined: 01 Jul 2011 13:37

Pass errorlevel from one batch file to another?

#1 Post by Clueless in Seattle » 19 Jun 2016 11:25

It's been a long while since I played around with my batch files, and at my age my memory is failing, so I'm finding that I'm having to relearn most of the stuff that I had learned a couple of decades ago.

I'm running MS-DOS 6.21, under DOSSHELL from MS-DOS 5.0.

I use a text editor (TSE 2.5) with a built in macro language that allows me to run batch files as if they were text editor keyboard commands.

I'm working on revising a pair of these batch files that are called from the TSE text editor. I'm not sure why I broke the batch file down into two parts -- I wrote the code a long time ago -- but that's how it stands now, so the text editor runs the two files in sequence, one after the other.

I can't remember if it's possible to pass an errorlevel from batch file #1 to batch file #2. Does MS-DOS "remember" the errorlevel from a batch file, even after the file has stopped running? And would the subsequent batch file then be able to use that errorlevel from the previous batch file that had just stopped running?

I'd like to add a line at the very beginning of file #2 like this:

Code: Select all

if errorlevel 0 goto end

The objective would be that if file #1 returns an errorlevel of 0, then there would be no need to even run file # 2.

Will in Seattle
a.k.a. "Clueless"

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Pass errorlevel from one batch file to another?

#2 Post by aGerman » 19 Jun 2016 12:02

If you call the 2nd Batch file from within the 1st Batch file the errorlevel from the 2nd Batch file should be available in the 1st.

bat1.bat

Code: Select all

@echo off
call bat2.bat
if errorlevel 1 echo failure
pause


bat2.bat

Code: Select all

@echo off
echo a|find "b"


Batch 1 echos "failure" because FIND returned errorlevel 1.

But ...

Code: Select all

if errorlevel 0 ...

... doesn't make any sense. The meaning of that statement is "if the errorlevel equals 0 or is greater than 0 then ...". You should rather use the "not" keyword.

Code: Select all

if not errorlevel 1 ...

This means "if the errorlevel is not 1 and not greater than 1, then ..."

Regards
aGerman

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Pass errorlevel from one batch file to another?

#3 Post by sambul35 » 19 Jun 2016 12:38

Clueless in Seattle wrote:The objective would be that if file #1 returns an errorlevel of 0, then there would be no need to even run file # 2.

Some clues:

Code: Select all

@echo off

choice /c yn /m "Stop running?" /t 5 /d n
if errorlevel 2 (call batch2.bat arg1 arg2 arg3
) else ( goto :end)

:: some code here

:end
exit /b

Clueless in Seattle
Posts: 47
Joined: 01 Jul 2011 13:37

Re: Pass errorlevel from one batch file to another?

#4 Post by Clueless in Seattle » 19 Jun 2016 13:07

sambul35 wrote:

Code: Select all

:: some code here
:end
exit /b
OK, I'll bite.
I just googled /b and found this:
If you use /b, Cmd.exe sets the ERRORLEVEL to the specified ExitCode. If you exit Cmd.exe, Cmd.exe sets the process exit code with the specified ExitCode.
I'm trying to decipher that technoese and translate it into such everyday English as a cognintively challenged non-techie like me might possibly comprehend.

Could that be saying that if I use /b with the exit command, the errorlevel from my batch file would be passed to MS-DOS and "remembered"?

And if so, could it also be saying that the next batch file run immediately after exiting the first file, would "inherit" that errorlevel?

Will in Seattle
a.k.a. "Clueless"
Running MS-DOS 6.21 under DOSSHELL from MS-DOS 5.0

Clueless in Seattle
Posts: 47
Joined: 01 Jul 2011 13:37

Re: Pass errorlevel from one batch file to another?

#5 Post by Clueless in Seattle » 19 Jun 2016 13:14

aGerman wrote:If you call the 2nd Batch file from within the 1st Batch file the errorlevel from the 2nd Batch file should be available in the 1st.
...

Code: Select all

if errorlevel 0 ...

... doesn't make any sense. The meaning of that statement is "if the errorlevel equals 0 or is greater than 0 then ...". You should rather use the "not" keyword.

Code: Select all

if not errorlevel 1 ...

This means "if the errorlevel is not 1 and not greater than 1, then ..."

Hi aGerman, thanks for the detailed reply!

If I understand you correctly you are telling me (at least) two things:

1. The answer to my question is that "No," DOS will not remember the errorlevel from batch file #1 unless I call batch file #2 from batch file #1. Did I get that right?

2. You also seem to be saying that I've got my if errorlevel lines written all wrong. But I'm not sure I understand how to correct them. Here's what they look like now:

Code: Select all

   if errorlevel 1 echo [%0]  ERRORLEVEL = 1
   if errorlevel 1 echo [%0]  PROCEEDING TO ENCRYPTION
   if errorlevel 1 goto AskToEncrypt

   if errorlevel 0 echo [%0]  ERRORLEVEL = 0
   if errorlevel 0 echo [%0]  FILES ARE SAME:  ABORTING SAVE
   pause
   if errorlevel 0 goto end

My motto is: "If it works, don't fix it." But, if as you implied, there are problems in my code that could cause my batch file to go haywire in the future, I'd like to try to correct them now. Could you suggest how these lines should be written to include "not"?

Will in Seattle
a.k.a. "Clueless"
Running MS-DOS 6.21 under DOSSHELL from MS-DOS 5.0

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Pass errorlevel from one batch file to another?

#6 Post by sambul35 » 19 Jun 2016 13:40

Clueless in Seattle wrote:Could that be saying that if I use /b with the exit command, the errorlevel from my batch file would be passed to MS-DOS and "remembered"?

The above example was given for Cmd.exe running under Windows to show how to use errorlevel to start another batch. EXIT /B just clears Cmd process memory from variables used by the exited batch and returns control to the Cmd window, as if the batch never run. The errorlevel value is not "passed" to the next batch, unless used as an argument.

Are you trying to start batches from a program running under DOS? Somewhat different practices might apply in this scenario, or may not. I used DOS a few times in the past to service HDDs with some 16-bit programs, but hardly run any batches under pure DOS.
Last edited by sambul35 on 19 Jun 2016 13:47, edited 2 times in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Pass errorlevel from one batch file to another?

#7 Post by aGerman » 19 Jun 2016 13:44

1. The answer to my question is that "No," DOS will not remember the errorlevel from batch file #1 unless I call batch file #2 from batch file #1. Did I get that right?

Correct.

2. You also seem to be saying that I've got my if errorlevel lines written all wrong.

Not quite. If you firstly check if errorlevel is GREATER THAN OR EQUALS 1 (if errorlevel 1 ...) - and if so GOTO somewhere - your if errorlevel 0 ... was never seen.
As soon as errorlevel is 0 then if errorlevel 1 ... is FALSE and the remaining code will be executed. But in that case (as well as in any case) if errorlevel 0 ... doesn't make sense. Try

Code: Select all

   if errorlevel 1 echo [%0]  ERRORLEVEL = 1 OR IS GREATER THAN 1
   if errorlevel 1 echo [%0]  PROCEEDING TO ENCRYPTION
   if errorlevel 1 goto AskToEncrypt

   echo [%0]  ERRORLEVEL = 0
   echo [%0]  FILES ARE SAME:  ABORTING SAVE
   pause
   goto end


That's the reason why you're forced to check the highest errorlevel first. Only if you want to distinguish between errorlevel less than X or errorlevel greater than or equals X you could write it the other way around using keyword "not".

Code: Select all

   if not errorlevel 1 echo [%0]  ERRORLEVEL = 0
   if not errorlevel 1 echo [%0]  FILES ARE SAME:  ABORTING SAVE
   if not errorlevel 1 pause
   if not errorlevel 1 goto end

   echo [%0]  ERRORLEVEL = 1 OR IS GREATER THAN 1
   echo [%0]  PROCEEDING TO ENCRYPTION
   goto AskToEncrypt



Regards
aGerman

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Pass errorlevel from one batch file to another?

#8 Post by aGerman » 19 Jun 2016 13:51

Are you trying to start batches from a program running under DOS?

The Operating System is DOS as he wrote several times :wink:

Regards
aGerman

Clueless in Seattle
Posts: 47
Joined: 01 Jul 2011 13:37

Re: Pass errorlevel from one batch file to another?

#9 Post by Clueless in Seattle » 19 Jun 2016 14:41

sambul35 wrote:Are you trying to start batches from a program running under DOS?

Couldn't have said it better myself.

I'm running an old DOS text editor called TSE 2.5 which allows me to access the MS-DOS prompt from within the text editor itself, while the text editor continues to run in the background.

A slight complication is that I use an MS-DOS task switching GUI called DOSSHELL https://en.wikipedia.org/wiki/DOS_Shell, from which I run all my programs.

So a schematic of my software hierarchy might look something like this (starting from the bottom with the installed operating system, and working upward to the DOS prompt which can be accessed from the text editor:

    DOS prompt (accessed from TSE 2.5, where I run the batch file(s)
    TSE 2.5 (running under DOSSHELL)
    DOSSHELL (running under MS-DOS 6.21)
    MS-DOS 6.21 installed on my machine

So we start out with MS-DOS at the very bottom of the pyramid, and end up with a DOS prompt at the very top. I seldom reboot my machine. It's an old laptop that I have configured to go to "sleep" when I turn it off. Then when I turn it on again, it instantly returns me to exactly where I was working when I turned it off the night before, with the cursor still blinking right where I left it, and all the files and programs I was working with still open, up and running just as before. It's so much handier than Windows.

My motto regarding computing after the advent of Windows is: "Used to be fun; but now it's a chore."

Will in Seattle
a.k.a. "Clueless"

P.S. I wish this forum allowed "signatures" so I could automatically attach this info at the end of each post.

Clueless in Seattle
Posts: 47
Joined: 01 Jul 2011 13:37

Re: Pass errorlevel from one batch file to another?

#10 Post by Clueless in Seattle » 19 Jun 2016 14:58

Only if you want to distinguish between errorlevel less than X or errorlevel greater than or equals X you could write it the other way around using keyword "not".
Thanks, aGerman, I appreciate your taking the time to try explain that to me. But I guess I should have warned you that I flunked 9th grade algebra class sixty years ago, and to this day as soon as I see something like "equals X" I get a headache. :? So I'm not very confident I'll be able to understand what you wrote.

Do you think that that little bit of code I posted contains flaws that might come back to bite me someday? Or could I just let it stand as it is, even if it's not very elegant and doesn't measure up to minimal batch file coding standards?

Will in Seattle
a.k.a. "Clueless"

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Pass errorlevel from one batch file to another?

#11 Post by aGerman » 19 Jun 2016 15:20

Don't worry about that :wink: Replace X with any errorlevel value you want if that makes it more understandable for you.

Do you think that that little bit of code I posted contains flaws that might come back to bite me someday?

No, I don't think so as long as you always check if errorlevel 1 ... first.
Only if you want you could replace your lines with either of my two snippets that will do the same things.

What I basically tried to explain is that someting like ...

Code: Select all

if errorlevel 1 ...

... does not mean "if errorlevel equals 1 then ...". Its meaning is "if errorlevel is greater than or equals 1 then ...". Keeping that in mind anything else that I wrote should rather be self-explanatory.

Regards
aGerman

Clueless in Seattle
Posts: 47
Joined: 01 Jul 2011 13:37

SOLVED Re: Pass errorlevel from one batch file to another?

#12 Post by Clueless in Seattle » 19 Jun 2016 16:18

Thanks, aGerman, for being so patient with me. Now I understand what you meant.

In the meantime I've modified the macro in my text editor, and my two batch files, so that batch file 1 calls batch file 2.

I just tried the macro two times, testing for both errorlevels 1 & 0, and both times it ran the batch files correctly. So it looks like this problem is solved.

Many thanks for the help!

Will in Seattle
a.k.a. "Clueless"

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Pass errorlevel from one batch file to another?

#13 Post by aGerman » 19 Jun 2016 16:28

You're welcome! Thanks for your feedback :)

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Pass errorlevel from one batch file to another?

#14 Post by sambul35 » 19 Jun 2016 16:42

aGerman wrote:The Operating System is DOS as he wrote several times :wink:

It doesn't mean much for me, unless someone clarifies what are the differences in running batches under Cmd and DOS. :D
aGerman wrote:

Code: Select all

if errorlevel 1 ...

... does not mean "if errorlevel equals 1 then ...". Its meaning is "if errorlevel is greater than or equals 1 then ...".

Strangely enough, in practice I didn't notice any difference in processing, despite using 6+ errorlevels in some code sections with totally different processing. If the values overlap like that, it should affect the results - correct?
Clueless in Seattle wrote:My motto regarding computing after the advent of Windows is: "Used to be fun; but now it's a chore."

This motto appears quite detach from reality I observe to say the least, not saying "delusional" by any means. :mrgreen: It likely depends on what tasks you generally use a computer for.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Pass errorlevel from one batch file to another?

#15 Post by aGerman » 19 Jun 2016 16:55

It doesn't mean much for me, unless someone clarifies what are the differences in running batches under Cmd and DOS. :D

The command processor in DOS is command.com. Cmd.exe doesn't even exist. Extensions like FOR /F, FOR /L, IF /I, EXIT /B, ... don't exist. The %errorlevel% variable doesn't exist. Many Batch tools that exist in Windows don't exist in DOS and vice versa. The syntax of commands may differ (e.g. CHOICE). And many more ...

in practice I didn't notice any difference in processing

Try to process the returned errorlevel of CHOICE in ascending order with the if errorlevel ... syntax...
But if you keep in mind what I said think about what if errorlevel 0 ... actually means and why I said it is senseless ... That was the reason why I tried to explain the behavior.

Regards
aGerman

Post Reply