为什么在使用ENABLEDELAYEDEXPANSION时这个变量不会在for循环中设置? [英] 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(* .txt)中的%% f(
校验和%% f%种子%
回显错误级别:!ERRORLEVEL!
set seed =!ERRORLEVEL!
echo new seed val:!seed!
)
输出:
C:\> returnval
种子初始值val:-1
种子在主:FFFFFFFF
result.txt的32位校验和是44DD58EE
错误级别:1155356910
新种子值:-1 //仍然-1?应该是1155356910
种子在主:FFFFFFFF
test.txt的32位校验和是E245740F
错误级别:-498764785
新种子值:-1
设置seed = ERRORLEVEL的行不起作用。
回显ERRORLEVEL使用!否则将无法正常展开和显示。
正如您可以看到当我们获得新的种子值:它仍然是-1
将种子设置为最后一个
ERRORVALUE
?
注意,for循环只是查找我有两个文本文件,并将其送到一个exe返回的文件的校验和,似乎工作正常,因为ERRORLEVEL是exe的结果。)
答案需要在集合上使用/ a,以获取种子获取ERRORLEVEl的数值,然后使用!对于校验和参数种子。
@echo off
pre>
SETLOCAL ENABLEDELAYEDEXPANSION
seed = -1
echo seed init val:%seed%
for(* .txt)DO(
checksum %% f!seed!
回声错误级别:!ERRORLEVEL!
set / a seed =!ERRORLEVEL!
echo new seed val:!seed!
)
解决方案您已经知道,您需要使用延迟扩展才能正常工作,因此应该
set seed =!ERRORLEVEL!
。和echo seed值:!seed!
时打印出来。经验法则:如果它在一个循环中,那么你必须延迟扩展。@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 lastERRORVALUE
?(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!
. Andecho seed value: !seed!
when printing it out. The rule of thumb: if it's in a loop, then you have to delay the expansion.这篇关于为什么在使用ENABLEDELAYEDEXPANSION时这个变量不会在for循环中设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!