Page 1 of 1
[Solved] preventing the call return
Posted: 16 Jul 2011 12:23
by allal
is there a way to prevent returning to calling command without using exit
like
Code: Select all
call script.cmd
rem we don't want to return here
echo mission failed&goto :eof
::------------------------------------------------
rem we are in script.cmd
goto label
:label
if not exist file.txt (
rem throw a terminating error to prevent returning or use goto end
throw no such file &rem throw is not supported
goto end
)
:end
rem must end here without using exit
rem please cmd forget the call
im calling a script so using the goto command will not work
Re: preventing the call return
Posted: 16 Jul 2011 12:43
by aGerman
This code returns to the calling command, but
exit /b 1 returns ErrorLevel 1, thats why
goto :eof is executed.
Code: Select all
@echo off &setlocal
call :label||goto :eof
echo file exists
pause
goto :eof
:label
if not exist file.txt (
echo no such file &pause>nul
exit /b 1
)
goto :eof
Regards
aGerman
PS:
Because you edited your post after that ...
It s the same thing. Return an ErrorLevel from script.cmd using exit /b.
Re: preventing the call return
Posted: 16 Jul 2011 13:02
by allal
aGerman wrote:This code returns to the calling command, but
exit /b 1 returns ErrorLevel 1, thats why
goto :eof is executed.
Code: Select all
@echo off &setlocal
call :label||goto :eof
echo file exists
pause
goto :eof
:label
if not exist file.txt (
echo no such file &pause>nul
exit /b 1
)
goto :eof
Regards
aGerman
it is something like
Code: Select all
@echo off &setlocal
call :label
if errorlevel 1 echo we returned mission failed&goto :eof
echo file exists
pause
goto :eof
it returned to call statement as this will return it to the calling batch as well
please is there a way like the throw command in other language that will force
a terminating error instantly to prevent any further processing without exiting
Re: preventing the call return
Posted: 16 Jul 2011 13:13
by aGerman
Hmm, why can't you use EXIT then? It terminates the cmd process. What else should happen?
Regards
aGerman
Re: preventing the call return
Posted: 16 Jul 2011 14:49
by allal
aGerman wrote:Hmm, why can't you use EXIT then? It terminates the cmd process. What else should happen?
i don't want to exit as the error will not be shown to the user further more when the error is printed it must be printed in red color
Re: preventing the call return
Posted: 16 Jul 2011 15:10
by Ed Dyreen
'
An error is
always shown on console
the error will not be shown to the user
Code: Select all
echo.this is ok
>con echo.this can't be supressed
>&2 echo.this goes to channel 2
>2&1 echo.this goes to channel 2 and channel 1
( >&2 echo.channel 2 is suppressed ) 2>nul
it must be printed in red color
Code: Select all
if not errorlevel 1 goto :skip
rem throw a terminating error to prevent returning
echo.no such file &pause
rem a happy crash to prevent returning
for %%! in ( %%~ )
:skip
Re: [Solved] preventing the call return
Posted: 17 Jul 2011 04:12
by allal
Ed Dyreen wrote:'
An error is
always shown on console
but this will continue after the error returning to the call statement
Code: Select all
rem a happy crash to prevent returning
for %%! in ( %%~ )
this code really disallow return to call statement but an accidental press will
exit the console,furthermore it is like an emulation of the exit statement.
i have discovered the solution and it was very damn simple.
sometimes when something is very easy only beginners can solve it
Code: Select all
@echo off
if not exist c:\file.txt call :ThrowException No Such A File :: c:\file.txt
:ThrowException
cmd /T:0C /K echo.%*
Re: [Solved] preventing the call return
Posted: 17 Jul 2011 05:25
by Ed Dyreen
'
Glad you solved it, guess we both had problems figuring what you were after
furthermore it is like an emulation of the exit statement.
Maybe, but technically a crash is a crash, and an exit is an exit
Code: Select all
call :a
echo.terminate process
exit %errorlevel%
:a
call :b
echo.terminate session
exit /b
:b
echo.crash process
for %%! in ( %%~ )
Re: [Solved] preventing the call return
Posted: 17 Jul 2011 09:21
by allal
Ed Dyreen wrote:'
Maybe, but technically a crash is a crash, and an exit is an exit
i tested you code in the prompt and it didn't exit the prompt
and when i tested it in the batch file it prevents from returning to the call statement and it exit the batch file
i tried to cause a crash with '????' is not recognized as an internal command...
and it failed
what a strange code !!!
Re: [Solved] preventing the call return
Posted: 17 Jul 2011 12:14
by dbenham
allal wrote:i have discovered the solution and it was very damn simple.
sometimes when something is very easy only beginners can solve it
Code: Select all
@echo off
if not exist c:\file.txt call :ThrowException No Such A File :: c:\file.txt
:ThrowException
cmd /T:0C /K echo.%*
Nothing has been solved
Type EXIT into the console after the pseudo "exception" and the script continues execution.
caller.cmd
Code: Select all
@echo off
call script.cmd
echo MISSION FAILED!
exit /b
script.cmd
Code: Select all
@echo off
if not exist c:\file.txt call :ThrowException No Such A File :: c:\file.txt
echo Next line in script if no error
::Normally exit script before subroutines are defined
exit /b
:ThrowException
cmd /T:0C /K echo.%*
Demonstration of failure - Interactive session with prompt set to > without showing the current path:
>caller.cmd
No Such A File :: c:\file.txt
>exit
Next line in script if no error
MISSION FAILED!
>
Regarding Ed's code:
allal wrote:what a strange code !!!
Yes - Ed has "solved" your problem by intentionally introducing a fatal syntax error. The code can be made even simpler:
The ugly syntax error message can be eliminated by redirecting stderr to nul. But there is still a potential problem - any SETLOCAL will still be in effect after the script terminates
caller2.cmd
Code: Select all
@echo off
call script2
echo MISSION FAILED!
exit /b
script2.cmd
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set test=SETLOCAL is still in effect
if not exist c:\file.txt call :ThrowException No Such A File :: c:\file.txt
echo Next line in script if no error
exit /b
:ThrowException
echo.%*
2>nul call :FatalSyntaxError
:FatalSyntaxError
for
Interactive Session:
>set test
Environment variable test not defined
>echo !test!
!test!
>caller2
No Such A File :: c:\file.txt
>echo !test!
SETLOCAL is still in effect
>
You can type EXIT at this point and the batch script(s) will NOT resume (the console will terminate). But you can see that SETLOCAL ENABLEDELAYEDEXPANSION is still in effect. You need to be careful in your subsequent processing
I talk about persistence of SETLOCAL after script termination
in this thread.Dave Benham
Re: [Solved] preventing the call return
Posted: 17 Jul 2011 12:54
by allal
ok you really saved me benham thanks a lot
so i will stick with the fatal syntax error