以下是大多数编程语言中可用的经典" for"语句。

for(variable declaration;expression;Increment) {
   statement #1
   statement #2
   
}

批处理脚本语言没有类似于上述语法的直接" for"语句,但仍然可以使用if语句和标签来实现经典的" for"循环语句。

让无涯教程看看批处理脚本中经典for循环的常规语法实现。

Set counter
:label

If (expression) exit loop
Do_something
Increment counter
Go back to :label

以下是一个示例,说明如何执行经典的" for"循环语句。

@echo off 
SET /A i = 1 
:loop 

IF %i%==5 GOTO END 
echo The value of i is %i% 
SET /a i=%i%+1 
GOTO :LOOP 
:END

上面的命令产生以下输出。

The value of i is 1 
The value of i is 2 
The value of i is 3 
The value of i is 4

参考链接

https://www.learnfk.com/batch-script/batch-script-classic-loop-implementationn.html