one thing i can make sure is the enviroment variable i use is exactly
the value i expect before leave its function batch file scope, but in
the caller batch file, it's value hasn't been changed. i even have
tried use delayedexpansion technique.
::test1.bat
ECHO OFF
set build=True
setlocal enabledelayedexpansion
::read configfile
::build several project and output to log file
endlocal
for /f %%i in ('findstr /s /r /i /c:"[1-9][0-9]* Error\(s\)" D:
\StateMachine\src\IDE\log\*.log') do @goto error
for /f %%i in ('findstr /s /r /c:"[1-9][0-9]* failed" D:\StateMachine
\src\IDE\log\*.log') do @goto error
goto eof
:error
@set build=False
echo %build% in test1.bat
:eof
::test2.bat
ECHO OFF
call test1.bat
ECHO %test1% in test2.bat
when i run test2.bat, i get following output:
False in test1.bat
True in test2.bat
why this happened?
thinktwice - 20 Jun 2008 04:49 GMT
> ::test2.bat
> ECHO OFF
> call test1.bat
> ECHO %test1% in test2.bat <<< should be %build%
sorry, an typo in previous example, and this example file works, i
should find out why my real batch file doesn't work
foxidrive - 20 Jun 2008 06:25 GMT
>::test1.bat
>
[quoted text clipped - 5 lines]
> ::build several project and output to log file
>endlocal
What is endlocal doing there?
>for /f %%i in ('findstr /s /r /i /c:"[1-9][0-9]* Error\(s\)" D:
>\StateMachine\src\IDE\log\*.log') do @goto error
>for /f %%i in ('findstr /s /r /c:"[1-9][0-9]* failed" D:\StateMachine
>\src\IDE\log\*.log') do @goto error
Both these commands goto the :error routine
>goto eof
>
[quoted text clipped - 7 lines]
>call test1.bat
>ECHO %test1% in test2.bat
%text1% is not set in the code above.
>when i run test2.bat, i get following output:
>False in test1.bat
>True in test2.bat
>
>why this happened?
Al Dunbar - 20 Jun 2008 06:36 GMT
> ::test1.bat
>
[quoted text clipped - 28 lines]
>
> why this happened?
Not 100% sure, but I think the complexity of your coding standards may be
making it more difficult to find your problems. Here are a few suggestions
for your consideration:
1) never define a label called ":eof", as ":eof" is automatically defined as
immediately following the last line in the file. If you define it
explicitly, you might be tempted to add some code to be executed on exit
like this:
if some-error-condition goto:eof
etc.
etc.
goto:eof
:eof
echo/the result is...
pause
The statements following the ":eof" label will never be executed.
2) what do you think happens when this code runs:
set test=a
if "%test%" EQU "a" (
echo/equal
) else (
echo/not equal
)
You might say that it will display the word "equal", but it will not,
because there is a trailing blank in the set statement. Do this instead:
(set test=a)
if "%test%" EQU "a" (
echo/equal
) else (
echo/not equal
)
the variable test will now contain what seemed to be the most likely and
expected value.
3) when a function returns a boolean result (i.e. true or false), there is
little benefit in prettying up the code by capitalizing the first letter,
but there is a disadvantage: unnecessary complexity. Consider this:
(set test=True)
if "%test%" EQU "true" (
echo/TRUE
) else (
echo/FALSE
)
if "%test%" EQU "false" (
echo/FALSE
) else (
echo/TRUE
)
The output will be the seemingly illogical: FALSE then TRUE. Better to use
true/false values that are less ambiguous, such as:
(set test=true)
call:showresult
(set test=)
call:showresult
goto:eof
:showresult
if defined test (
echo/test is true
) else (
echo/test is false
)
/Al
> by the way, i often use enviroment variable to pass batch file result
> in the way like:
[quoted text clipped - 4 lines]
> should i use delayedexpansion technique also? it works in some cases
> and failed in other.
delayed expansion *always* *works* - the way it was intended to. When your
efforts tend to show otherwise, I'd suggest you consider examining how you
are using it and/or what, precisely, you think it is supposed to do.
/Al