开发者

Why won't this variable set in the for loop while using ENABLEDELAYEDEXPANSION?

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set seed=-1
echo seed init val:%seed%

for %%f in (*.txt) DO (
    checksum %%f %seed%
    echo error level: !ERRORLEVEL!
    set seed = !ERRORLEVEL!
    echo new seed val:!seed!
)

Output:

C:\>returnval
seed init val:-1
Seed in main : FFFFFFFF
The 32-bit checksum for result.txt is 44DD58EE
error level: 1155356910
new seed val:-1 //still -1 ?? should be 1155356910
Seed in main : FFFFFFFF
The 32-bit checksum for test.txt is E245740F
error level: -498764785
new seed val:-1

The line that sets seed = ERRORLEVEL does not work.

to echo the ERRORLEVEL I did need to use the "!" otherwise it would not expand and display properly.

As you can see when we get to new seed value: it is still -1

How do I set seed to the last ERRORVALUE?

(Side note, the for loop is just looking up two text files I have and feeding it to an exe that returns a checksum for the file., that seems to works fine, as the ERRORLEVEL is the result of the exe.)

Answer: needed to use /a on the set to get the seed to take the numeric value of ERRORLEVEl and then use the "!" for the checksum parameter seed 开发者_高级运维as well.

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set seed=-1
echo seed init val:%seed%

for %%f in (*.txt) DO (
    checksum %%f !seed!
    echo error level: !ERRORLEVEL!
    set /a seed = !ERRORLEVEL!
    echo new seed val:!seed!
)


You already know that you need to use delayed expansion for this to work properly, so it should be set seed = !ERRORLEVEL!. And echo seed value: !seed! when printing it out. The rule of thumb: if it's in a loop, then you have to delay the expansion.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜