开发者

Batch script: local variable from function1 to function2

开发者 https://www.devze.com 2023-02-06 07:11 出处:网络
Okay guys, let my try to explain my problem: I start with a line from where I start 2 different functions

Okay guys, let my try to explain my problem:

I start with a line from where I start 2 different functions

setlocal EnableDelayedExpansion
for %%i in ("C:\*.*") do (
call :function1 "%%~i"
call :function2 "%%~i"
)
goto :eof

In function1, at a certain point I DO a SET in a local environment:

setlocal EnableDelayedExpansion
...
...
set name1=blabla
endlocal  & SET name=%name1%
echo %name%
goto :eof

The echo does return my variable. Now onto my problem. I quit function one and i go to function 2 (see first code-segment) I can'开发者_Python百科t call the variable form here. I tried in the function2, I tried before function2 is called, but both didn't resolve the issue.

My guess is a have set only a local variable for function1. I search the nets but i read that the line "endlocal & SET name=%name1%" should have solved my issue.

I hope I have explained it well, all help appreciated!


I'm not sure where your problem lies since this works perfectly:

@setlocal enableextensions enabledelayedexpansion
@echo off
set name=ORIGNAME
for %%i in (1 2) do (
    call :function1 %%i
    echo in main %name% !name!
)
endlocal
goto :eof

:function1:
    setlocal enableextensions enabledelayedexpansion
    set name1=%1_blabla
    endlocal  & SET name=%name1%
    echo in function %name%
    goto :eof

outputting:

in function 1_blabla
in main ORIGNAME 1_blabla
in function 2_blabla
in main ORIGNAME 2_blabla

Are you certain that, when you used name in the main areas, you used !name! instead of %name%?

If you used the %name% variant, that would be evaluated when the entire for loop was read, not at the time when you used it (in other words, it would be blank). You can see that in the output of ORIGNAME in the main line.

I'm not certain that's the case since you are using delayed expansion. But, just in case, I thought I'd mention it. I always use delayed expansion and I always used the ! variants of the environment variables since it more closely matches how I expect a shell to work.

In any case, the code I've given works fine so you may want to fiddle with that to see if you can incorporate it into your own.


First a small working sample

@echo off
setlocal EnableDelayedExpansion

call :myAdd returnVar 1 2
echo 1. Percent %returnVar%
echo 1. Exlcam  !returnVar!

(
call :myAdd returnVar 4 5
echo 2. Percent %returnVar%
echo 2. Exlcam  !returnVar!
)
goto :eof

:myAdd
setlocal
set /a result=%2 + %3

(
  endlocal
  set %1=%result%
  goto :eof
)

---- Output ----
1. Percent 3
1. Exlcam  3
2. Percent 3
2. Exlcam  9

The cause for the wrong result in "2. Percent" is a result of the expanding of %var%.
They are expanded at the time of parsing, before executing any line of the parenthesis block, so there is the old value in returnVar.

But that is also the cause why the returning of variables from a function works.
the endlocal removes all local variables, but the block (an ampersand works the same way) is expanded before the "endlocal" is executed.

It's a good idea to test this issues with "echo on".

0

精彩评论

暂无评论...
验证码 换一张
取 消