循环文件的间隔

Loop over an interval of files

我想定义一个 for-loop(windows 命令行)迭代目录中文件的(编号)间隔,比如 1..100,然后是 101..200,然后201..300 等[编辑:不管文件名]。请参阅以下伪代码:

for %WORKDIR% %%f(1..100) in (*.htm) do (
   REM DoSomething with %%f
)

for %WORKDIR% %%f(101..200) in (*.htm) do (
   REM DoSomething with %%f
)

...etc

问: 有没有办法从命令行定义 "numbered intervals" 个文件?

//罗尔夫

您可以将每个函数放在一个单独的文件中:

:1to100
setlocal enabledelayedexpansion

set /a "file=1"

rem Skip 0 (skip=0 is omitted because it's illegal) and process 100.
for /f %%f in ('dir %workdir%\*.htm /b') do (
    if !file! gtr 100 (exit /b) else (set /a "file+=1")
    echo Do something with %%f.
)

endlocal
goto :eof

:100to200
setlocal enabledelayedexpansion

set /a "file=1"

rem Skip 100 and process 100.
for /f "skip=100" %%f in ('dir %workdir%\*.htm /b') do (
    if !file! gtr 100 (exit /b) else (set /a "file+=1")
    echo Do something with %%f.
)

endlocal
goto :eof