循环一段文件

时间:2015-04-03 14:40:09

标签: cmd

我想定义一个for循环(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

问:有没有办法从命令行定义文件的“编号间隔”?

// Rolf

1 个答案:

答案 0 :(得分:1)

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

: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