How to get value return from a function within a loop?

Multi tool use
How to get value return from a function within a loop?
I am trying to work out how to test the return value of a function call within a loop and breaks the loop if function returns a specific value.
The follow piece of batch script is my attempt, the function return expected value on the first 2 function calls. When the call is made within a loop, I cannot figure out how to test the return value. Please point me to a direction on how to test the function return value within the loop.
SET var1=2
SET var2=0
CALL :FUNC %var1 var2
ECHO var2 is: %var2%
REM ============================
SET var1=6
SET var2=0
CALL :FUNC %var1 var2
ECHO var2 is: %var2%
REM ============================
SETLOCAL EnableDelayedExpansion
SET list=1 0 3 4
FOR %%n IN (%list%) DO (
CALL :FUNC %%n rtn
IF !rtn! == 0 (
GOTO DONE
)
)
ECHO rtn is: %rtn%
: DONE
PAUSE
GOTO :eof
REM %1 is an in parameter
REM %2 is an out parameter
: FUNC
SETLOCAL EnableDelayedExpansion
SET var=%1
EndLocal & SET %2=%var% & GOTO :eof
setlocal
goto :eof
When you write
CALL :FUNC %var1 var2
you actually mean CALL :FUNC %var1% var2
, don't you?– aschipfl
Jul 3 at 7:00
CALL :FUNC %var1 var2
CALL :FUNC %var1% var2
@aschipfl, yes you are right. This is not the production batch script, I wrote this just to demonstrate the real issue I have in my production batch script, and have a typo in there as you've pointed out.
– simon
Jul 3 at 22:59
1 Answer
1
I can't see any problems with your loop, but with your function.
You only need to change the last line to (the quotes are new):EndLocal & SET "%2=%var%" & GOTO :eof
EndLocal & SET "%2=%var%" & GOTO :eof
Without the quotes, you always appended a single space to your return value,
therefore the IF !rtn! == 0 (
was always false
IF !rtn! == 0 (
Thank you for your answer. But I don't understand why it needs the quotes. Is there any reference I can read?
– simon
Jul 3 at 5:29
@simon
SET %2=%var% & GOTO :eof
Note the space between %var%
and the ampersand &
. That extra space is not delimiter but is considered as part of the assigment. Compare it with SET %2=%var%& GOTO :eof
and see the difference. In general with the form set "a=b"
the last quote marks the end of the assignment and the set
command ignores everything after the last quote. There is no official reference for this specific syntax of the the set
command.– sst
Jul 3 at 6:00
SET %2=%var% & GOTO :eof
%var%
&
SET %2=%var%& GOTO :eof
set "a=b"
set
set
@simon
SET a="b"
is very different to SET "a=b"
. In the first case a
contains "b"<space>
(two quotes and a space, total of 4 characters), in the second case a
only contains a single b
. The second form is often referenced as extended set syntax– jeb
Jul 3 at 6:29
SET a="b"
SET "a=b"
a
"b"<space>
a
b
@simon They are two different things. With the form
set a="b" & ...
Not only the space will be stored in the variable but also the the quotes. try set a="b" & echo [%a%]
to see the result.– sst
Jul 3 at 6:30
set a="b" & ...
set a="b" & echo [%a%]
@simon, the
&
is not specifically recognised by set
, it is recognised earlier when parsing the command line...– aschipfl
Jul 3 at 7:03
&
set
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
there are no such things like
setlocal
,goto :eof
or function call... in DOS. Those are cmd features– phuclv
Jul 3 at 5:20