Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Setting errorlevel

35 views
Skip to first unread message

frank

unread,
Dec 18, 2003, 10:34:15 AM12/18/03
to
How can I set the errorlevel from within a batch file? (preferably without
using something external like gawk, although I did try echo.|gawk '{exit 1}'
and it gave me an error about the ' characters)


Matthias Tacke

unread,
Dec 18, 2003, 1:05:53 PM12/18/03
to
"frank" wrote:

Call a second batch which in turn you explicitly "exit /B errorlevel"

See what is not in the windows help:
exit /?
--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm

Ted Davis

unread,
Dec 18, 2003, 4:32:54 PM12/18/03
to

That's unix syntax
gawk "BEGIN{exit(1)}"
is a Windows version.

There are a couple of utilities in one of the GNU Windows utilities
collections (Sh-Utils) at
<http://sourceforge.net/project/showfiles.php?group_id=23617> that
contain true and false utilities returning 0 and 1 respectively.
Since you appear to be Unix aware, you might be interested in many of
the packages on that page.

T.E.D. (tda...@gearbox.maem.umr.edu)
SPAM filter: Messages to this address *must* contain "T.E.D."
somewhere in the body or they will be automatically rejected.

Herbert Kleebauer

unread,
Dec 18, 2003, 7:01:03 PM12/18/03
to
Matthias Tacke wrote:

> Call a second batch which in turn you explicitly "exit /B errorlevel"

No need for an external batch:

:::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
call :seterr
echo errorlevel: %errorlevel%
goto :eof

:seterr
exit /b 10
:::::::::::::::::::::::::::::::::::::::::::::::::::::::


NOTE: set behave different in a .bat and .cmd file:

:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: save this code as a.bat and b.cmd and execute both

@echo off

set a=
call :seterr
echo errorlevel: %errorlevel%
set a=
echo errorlevel: %errorlevel%

set a=
call :seterr
echo errorlevel: %errorlevel%
set a=irgendwas
echo errorlevel: %errorlevel%

set a=igendwas
call :seterr
echo errorlevel: %errorlevel%
set a=
echo errorlevel: %errorlevel%

goto :eof

:seterr
exit /b 10
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Timo Salmi

unread,
Dec 19, 2003, 1:11:04 AM12/19/03
to
Matthias Tacke <Matt...@Tacke.de> wrote:
> "frank" wrote:
> >How can I set the errorlevel from within a batch file? (preferably without
> Call a second batch which in turn you explicitly "exit /B errorlevel"

@echo off & setlocal enableextensions
:: Requires G(nu)AWK
gawk 'BEGIN{exit 2}'
for %%e in (0 1 2 3 4 5 6 7 8 9) do (
if %errorlevel% EQU %%e echo The errorlevel is %%e)
goto :EOF

@echo off & setlocal enableextensions
echo.@exit /B ^2>tmp$$$.cmd
call tmp$$$
for %%e in (0 1 2 3 4 5 6 7 8 9) do (
if %errorlevel% EQU %%e echo The errorlevel is %%e)
for %%f in (tmp$$$.cmd) do if exist %%f del %%f
goto :EOF

BTW, note the ^2 to avoid the script from mistaking if for a handle.

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

Matthias Tacke

unread,
Dec 19, 2003, 8:58:23 AM12/19/03
to
Herbert Kleebauer wrote:

>Matthias Tacke wrote:
>
>> Call a second batch which in turn you explicitly "exit /B errorlevel"
>
>No need for an external batch:
>

Yes, I should have known, that the (internal) call also opens another
cmd. I focused to much on the "ending the batch" of exit.

Timo Salmi

unread,
Dec 19, 2003, 1:27:47 PM12/19/03
to
Herbert Kleebauer <kl...@unibwm.de> wrote:
> Matthias Tacke wrote:
> > Call a second batch which in turn you explicitly "exit /B errorlevel"
> No need for an external batch:

That is an inventive solution that you presented. However, sometimes
an external batch solution is much simpler, at least for the reader
to follow. Furthermore, an extennal batch most often can be easioly
created by the original batch and then deleted also by the original
batch. Given in an earlier posting of mine on this subject.

(snip)

> :::::::::::::::::::::::::::::::::::::::::::::::::::::::
> :: save this code as a.bat and b.cmd and execute both

That's also two batches.

Please understand that I am not minimizing your fine, inventive
solution. I am just pointing out that often the seemingly less
orthodox solution is more convenient and more obvious in its logic.

Al Dunbar

unread,
Dec 19, 2003, 8:49:48 PM12/19/03
to

"Herbert Kleebauer" <kl...@unibwm.de> wrote in message
news:3FE23FBF...@unibwm.de...

> Matthias Tacke wrote:
>
> > Call a second batch which in turn you explicitly "exit /B errorlevel"
>
> No need for an external batch:
>
> :::::::::::::::::::::::::::::::::::::::::::::::::::::::
> @echo off
> call :seterr
> echo errorlevel: %errorlevel%
> goto :eof
>
> :seterr
> exit /b 10
> :::::::::::::::::::::::::::::::::::::::::::::::::::::::

Could be made more general as in this example:

> :::::::::::::::::::::::::::::::::::::::::::::::::::::::
> @echo off
> call :seterr 10


> echo errorlevel: %errorlevel%
> goto :eof
>
> :seterr

> exit /b %1
> :::::::::::::::::::::::::::::::::::::::::::::::::::::::

/Al

Al Dunbar

unread,
Dec 19, 2003, 8:51:14 PM12/19/03
to

"Timo Salmi" <t...@UWasa.Fi> wrote in message
news:brvfv3$i...@poiju.uwasa.fi...

> Herbert Kleebauer <kl...@unibwm.de> wrote:
> > Matthias Tacke wrote:
> > > Call a second batch which in turn you explicitly "exit /B errorlevel"
> > No need for an external batch:
>
> That is an inventive solution that you presented. However, sometimes
> an external batch solution is much simpler, at least for the reader
> to follow. Furthermore, an extennal batch most often can be easioly
> created by the original batch and then deleted also by the original
> batch. Given in an earlier posting of mine on this subject.
>
> (snip)
>
> > :::::::::::::::::::::::::::::::::::::::::::::::::::::::
> > :: save this code as a.bat and b.cmd and execute both
>
> That's also two batches.

Yes, but the linecount, the linecount.

> Please understand that I am not minimizing your fine, inventive
> solution. I am just pointing out that often the seemingly less
> orthodox solution is more convenient and more obvious in its logic.

Yes, this is always a possibility. I just think that, in this case, the
simpler solution is, well, simpler.

/Al


Herbert Kleebauer

unread,
Dec 21, 2003, 12:38:42 PM12/21/03
to
Matthias Tacke wrote:
>
> Herbert Kleebauer wrote:
>
> >Matthias Tacke wrote:
> >
> >> Call a second batch which in turn you explicitly "exit /B errorlevel"
> >
> >No need for an external batch:
> >
> Yes, I should have known, that the (internal) call also opens another
> cmd. I focused to much on the "ending the batch" of exit.

And as we have learned here a few weeks ago, no call is necessary
at all:

echo.>nul|exit /b 16
echo %errorlevel%

Timo Salmi

unread,
Dec 21, 2003, 12:58:08 PM12/21/03
to
Herbert Kleebauer <kl...@unibwm.de> wrote:
> And as we have learned here a few weeks ago, no call is necessary
> at all:
> echo.>nul|exit /b 16
> echo %errorlevel%

Now _that_ is an excellent, concise solution. Certainly worth a
Google pointer link.

Matthias Tacke

unread,
Dec 21, 2003, 1:09:42 PM12/21/03
to
Herbert Kleebauer wrote:

Clever combined. I like it. Pretty short.

The redirection is redundant and may be omitted.
So this gives the same result:

echo.|exit /b 16

Uno

unread,
Dec 21, 2003, 1:25:51 PM12/21/03
to

"Herbert Kleebauer" <kl...@unibwm.de> wrote in message news:3FE5DAA2...@unibwm.de...

Why not:

cmd/cexit/b16
echo %errorlevel%


0 new messages