如何循环使用 Windows 批处理文件?

Windows 批处理文件中 FOR 循环的语法是什么?

703167 次浏览

类型:

for /?

你会得到几页帮助文本。

FOR %%A IN (list) DO command parameters

List 是由空格、逗号或分号分隔的任何元素的列表。

Command 可以是任何内部或外部命令、批处理文件,甚至可以是 OS/2和 NT 中的命令列表

Parameter 包含 command 的命令行参数。 在此示例中,将对列表中的每个元素执行一次命令,如果指定了参数,则使用参数。

一个特殊类型的参数(甚至命令)是% A,它将被列表中的每个元素连续替换。

来自 循环

如果你想做某件事 x 倍,你可以这样做:

例子(x = 200) :

FOR /L %%A IN (1,1,200) DO (
ECHO %%A
)

1,1,200的意思是:

  • 开始 = 1
  • 每步增量 = 1
  • 完 = 200

FOR 将提供关于 FOR 循环的任何信息,包括正确使用的示例。

试试这个代码:

@echo off
color 02
set num1=0
set num2=1
set terminator=5
:loop
set /a num1= %num1% + %num2%
if %num1%==%terminator% goto close
goto open
:close
echo %num1%
pause
exit
:open
echo %num1%
goto loop

num1是要增加的数字,num2是增加到 num1的值,终止符是 num1将结束的值。可以在此语句(if %num1%==%terminator% goto close)中为终止符指定不同的值。这个布尔表达式 goto close 是布尔值为 true 的进程,goto open 是布尔值为 false 的进程。

@echo off
echo.
set /p num1=Enter Prelim:
echo.
set /p num2=Enter Midterm:
echo.
set /p num3=Enter Semi:
echo.
set /p num4=Enter Finals:
echo.
set /a ans=%num1%+%num2%+%num3%+%num4%
set /a avg=%ans%/4
ECHO %avg%
if %avg%>=`95` goto true
:true
echo The two numbers you entered were the same.
echo.
pause
exit

有条件地多次执行一个命令。

  • 语法 FOR 文件

    FOR %%parameter IN (set) DO command
    
  • syntax-FOR-Files-Rooted at Path

    FOR /R [[drive:]path] %%parameter IN (set) DO command
    
  • syntax-FOR-Folders

    FOR /D %%parameter IN (folder_set) DO command
    
  • syntax-FOR-List of numbers

    FOR /L %%parameter IN (start,step,end) DO command
    
  • syntax-FOR-File contents

    FOR /F ["options"] %%parameter IN (filenameset) DO command
    

    或者

    FOR /F ["options"] %%parameter IN ("Text string to process") DO command
    
  • syntax-FOR-Command Results

    FOR /F ["options"] %%parameter IN ('command to process') DO command
    

It

  • Take a set of data
  • Make a FOR Parameter %%G equal to some part of that data
  • Perform a command (optionally using the parameter as part of the command).
  • --> Repeat for each item of data

If you are using the FOR command at the command line rather than in a batch program, use just one percent sign: %G instead of %%G.

FOR Parameters

  • The first parameter has to be defined using a single character, for example the letter G.

  • FOR %%G IN ...

    In each iteration of a FOR loop, the IN ( ....) clause is evaluated and %%G set to a different value

    If this clause results in a single value then %%G is set equal to that value and the command is performed.

    If the clause results in a multiple values then extra parameters are implicitly defined to hold each. These are automatically assigned in alphabetical order %%H %%I %%J ...(implicit parameter definition)

    If the parameter refers to a file, then enhanced variable reference can be used to extract the filename/path/date/size.

    You can of course pick any letter of the alphabet other than %%G. but it is a good choice because it does not conflict with any of the pathname format letters (a, d, f, n, p, s, t, x) and provides the longest run of non-conflicting letters for use as implicit parameters.

来自 FOR /?的帮助文档:

FOR% variable IN (set) DO command [ command-properties ]

指定可替换的单个字母参数。
(set)指定一个或多个文件的集合。可以使用通配符。 指定要为每个文件执行的命令。
命令参数
指定指定命令的参数或开关。

若要在批处理程序中使用 FOR 命令,请改为指定%% variable
变量名是区分大小写的,所以% i 是不同的
从% I。

如果启用了命令扩展,则下面的附加
支持 FOR 指挥的形式:

FOR/D% 变量 IN (set) DO 命令[命令参数]

If set contains wildcards, then specifies to match against directory
names instead of file names.

FOR/R [[ drive: ] path ]% variable IN (set) DO command [ command-properties ]

Walks the directory tree rooted at [drive:]path, executing the FOR
statement in each directory of the tree.  If no directory
specification is specified after /R then the current directory is
assumed.  If set is just a single period (.) character then it
will just enumerate the directory tree.

FOR/L% variable IN (start,step,end) DO command [ command-properties ]

The set is a sequence of numbers from start to end, by step amount.
So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would
generate the sequence (5 4 3 2 1)

这里也留下了一个纯 MS-DOS 的例子,这是我发现的唯一有效的例子:

FOR %A in (1, 2, 3, 4, 5) DO ECHO %A

enter image description here

也可以使用空格代替逗号。