我正在对大量的文件进行一些处理,我希望将所做的工作限制在找到的前9个文件上。我已经在批处理文件中尝试过这一点,但它不起作用。它对所有文件进行处理,而不是在第9个文件处停止。我做错了什么?
setlocal
set fileCount=0
for %%I in (*.doc) do (
rem do stuff here ...
set /a fileCount+=1
if "%fileCount%"=="9" exit
)发布于 2014-08-01 05:35:14
问题是%fileCount%是在解析时而不是在执行期间进行扩展的,因此它没有考虑到在执行过程中对fileCount的更改。因此,您的for-loop相当于以下内容:
for %%I in (*.doc) do (
rem do stuff here ...
set /a fileCount+=1
if "0"=="9" exit
)要纠正它,您需要启用并使用延迟扩展。要做到这一点,您可以使用setlocal EnableDelayedExpansion而不仅仅是setlocal,使用!fileCount!而不是%fileCount%。所以:
setlocal EnableDelayedExpansion
set fileCount=0
for %%I in (*.doc) do (
rem do stuff here ...
set /a fileCount+=1
if "!fileCount!"=="9" exit
)有关更多信息,请参见http://ss64.com/nt/delayedexpansion.html。
https://stackoverflow.com/questions/25073357
复制相似问题