它不会在batch file的for循环中显示增加的variables值

正如你可以看到我在batch file中是新的,我已经在Windows 2003环境中编写了一个脚本,用于打印增加的variables计数器的值,并且我也应用了不在for循环中工作的if条件因为它显示了正确的variables值。

@echo off SetLocal EnableDelayedExpansion cls set /A counter = 1 for C:\Scripts\LogFiles\ %%a in (*.txt) do ( echo %counter% set /A counter += 1 echo %%a if %counter% ==2 (echo test) ) 

延迟扩展需要你使用! 而不是%来展开variables。 所以应该是!counter!

所有forms为%counter%环境variables在命令parsing过程中都被展开(因为这个块包括完整的块),所以你只能看到一个variables循环之前的值,因为循环运行的时候没有变数了; 只有价值。