Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
schmintan
- Posts: 3
- Joined: 23 Apr 2012 09:52
#1
Post
by schmintan » 23 Apr 2012 10:00
Below is a simplified version of my problem.
Issue:
In function1, i want to be able to exit the current call of the function if a variable has an incorrect value.
The problem is that even on calling my End function, the program still passes through the entire code once more before exiting.
Code: Select all
@echo off
SET Var1="a"
SET var2="b"
Call:function1 %var1% %var2%
:function1
echo "%~1"
if "%~1"=="a" (
echo cannot continue
call:Exit
)
:End
echo Exiting bat file
Exit /B
Output of running this:
Code: Select all
C:\commands>test.bat
"a"
cannot continue
The system cannot find the batch label specified - Exit
Exiting bat file
""
Exiting bat file
C:\commands>
notice it calls the Exit function twice. it should have called it the first time and exited. can someone clear up what the problem is here please?
Thanks!
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#2
Post
by Squashman » 23 Apr 2012 10:12
It can't call :Exit because that label does not exist. It is actually echoing the first time because it is executing from the first call to function1. It then returns from function1 when it gets down to your EXIT /B statement. Then it tries to execute your function1 again and just drops down to your echo and exit commands again.
-
Fawers
- Posts: 187
- Joined: 08 Apr 2012 17:11
-
Contact:
#3
Post
by Fawers » 23 Apr 2012 10:33
Squashman's info is right.
exit /b works for your call command. I think it's okay to say that the /b switch acts as a "function breaker."
Any exit /b inside a called function will exit the function, and not the whole batch file.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#4
Post
by foxidrive » 23 Apr 2012 10:36
Is this similar to what you want to do?
Code: Select all
@echo off
SET Var1="a"
SET var2="b"
Call:function1 %var1% %var2%
goto :EOF
:function1
echo "%~1"
if "%~1"=="a" (
echo cannot continue
goto :EOF
)
echo function ends
echo Exiting bat file
Exit /B
-
abc0502
- Posts: 1007
- Joined: 26 Oct 2011 22:38
- Location: Egypt
#5
Post
by abc0502 » 23 Apr 2012 10:48
u can use errorlevel like that
Code: Select all
If %errorlevel%==1 ( goto EOF
) Else ( goto continue )
and make a EOF call sign and continue Call sign and pu codes in it
The EOF
and the continue
Code: Select all
put ur code in it in case there was no error