How to break from a .cmd?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 631
Joined: 28 Jun 2010 03:46

How to break from a .cmd?

#1 Post by miskox » 16 Oct 2024 23:00

I have 1.cmd:

Code: Select all

@call 2.cmd
@echo HERE-1.cmd
and I have 2.cmd

Code: Select all

@echo off
for %%f in (a.a b.b c.c) do call :DOIT "%%f"

echo END-2.cmd
goto :EOF

:DOIT
set errlev=1
if not "%errlev%"=="0" echo ERROR %1.&exit /b

echo TOO FAR
goto :EOF
Output:

Code: Select all

c:\>
ERROR "a.a".
ERROR "b.b".
ERROR "c.c".
END-2.cmd
HERE-1.cmd
c:\>
I want to exit 2.cmd when first error (a.a) occurs. If I just use EXIT 1.cmd quits - I don't want that.

Please help.

Thanks.

Saso

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: How to break from a .cmd?

#2 Post by jeb » 17 Oct 2024 02:05

You can use a small function to exit only the current batch

Code: Select all

:exit-current-batch
for /L %%# in (0 1 40) DO (
    (goto) 2> NUL
    call set "_tmp=%%0"
    call set "_tmp=%%_tmp:~0,1%%"
    call set "_tmp=%%_tmp::=%%"
    if defined _tmp (
        REM Current Batch entry found
        exit /b
    )
)
exit /b
Just use CALL :exit-current-batch or GOTO :exit-current-batch, and it will return to the caller batch file.

miskox
Posts: 631
Joined: 28 Jun 2010 03:46

Re: How to break from a .cmd?

#3 Post by miskox » 17 Oct 2024 02:38

Thanks.

Saso

Post Reply