如何在Windows批处理文件中循环?

时间:2021-08-05 02:23:30

What is the syntax for a FOR loop in a Windows batch file?

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

9 个解决方案

#1


73  

FOR %%A IN (list) DO command parameters

list is a list of any elements, separated by either spaces, comma's or semicolons.

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

command can be any internal or external command, batch file or even - in OS/2 and NT - a list of commands

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

parameters contains the command line parameters for command. In this example, command will be executed once for every element in list, using parameters if specified.

参数包含命令行参数。在本例中,将对列表中的每个元素执行一次命令,如果指定了参数的话。

A special type of parameter (or even command) is %%A, which will be substituded by each element from list consecutively.

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

From FOR loops

从循环

#2


154  

If you want to do something x times, you can do this:

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

Example (x = 200):

示例(x = 200):

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

1,1,200 means:

1200的意思是:

  • Start = 1
  • 开始= 1
  • Increment per step = 1
  • 每步增量= 1
  • End = 200
  • 结束= 200

#3


41  

Type:

类型:

for /?

and you will get several pages of help text.

你会得到几页帮助文本。

#4


17  

Conditionally perform a command several times.

有条件地多次执行命令。

  • syntax-FOR-Files

    syntax-FOR-Files

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

    syntax-FOR-Files-Rooted在路径

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

    syntax-FOR-Folders

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

    syntax-FOR-List的数字

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

    syntax-FOR-File内容

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

    or

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

    syntax-FOR-Command结果

    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
  • 使参数%G的a等于该数据的某些部分
  • 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命令,而不是在批处理程序中使用,只使用一个百分号:%G而不是%%G。

FOR Parameters

为参数

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

    第一个参数必须使用单个字符来定义,例如字母G。

  • FOR %%G IN ...

    % % G在…

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

    在每个迭代中一个FOR循环,在评估(....)条款和% % G设置为不同的值

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

    如果该子句导致一个值,那么将%%G设置为该值,并执行命令。

    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)

    如果子句导致多个值,那么将隐式地定义额外的参数来保存每个值。这些自动分配在字母顺序%%H %I %J…(隐式参数定义)

    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.

    你当然可以选择任何字母,而不是%%G。但这是一个很好的选择,因为它不与任何路径名格式的字母(a、d、f、n、p、s、t、x)冲突,并且提供最长的非冲突字母,用作隐式参数。

#5


11  

FOR will give you any information you'll ever need to know about FOR loops, including examples on proper usage.

FOR将提供您需要了解的关于循环的任何信息,包括有关正确用法的示例。

#6


9  

Try this code:

试试这段代码:

@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 is the number to be incremented and num2 is the value added to num1 and terminator is the value where the num1 will end. You can indicate different value for terminator in this statement (if %num1%==%terminator% goto close). This is the boolean expression goto close is the process if the boolean is true and goto open is the process if the boolean is false.

num1是要增加的数字,num2是添加到num1的值,而终止符是num1将要结束的值。在这个语句中,您可以指示终止符的不同值(如果%num1%= %终止符% goto close)。这是布尔表达式goto close是布尔为真的过程,goto open是布尔为假的过程。

#7


3  

@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

#8


1  

From FOR /? help doc:

从/ ?帮助医生:

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

对于(set)中的%变量DO命令[命令参数]

%variable Specifies a single letter replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be used. command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.

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

To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
from %I.

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

If Command Extensions are enabled, the following additional
forms of the FOR command are supported:

如果启用命令扩展,则支持FOR命令的下列其他形式:

FOR /D %variable IN (set) DO command [command-parameters]

对于(set)中的/D %变量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-parameters]

FOR /R[驱动器:]%变量IN (set) DO命令[命令参数]

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-parameters]

对于/L %变量IN (start,step,end)执行命令[命令参数]

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)                                     

#9


0  

for e.x.

e.x。

for /L %n in (210,1,270) do @ffmpeg -i  "C:\Users\x\IMG_0%n.MOV" -vcodec copy -acodec copy "C:\Users\x\out\IMG_0%n.MP4"

it convert file format .mov to .MP4 from "IMG_0210.MOV" to "IMG_0270.MOV"

它将文件格式.mov从“IMG_0210”转换为. mp4。MOV”到“IMG_0270.MOV”

sorry for posting the stupid answer, for I will forget easily without doing it.

对不起,我发了这个愚蠢的答案,因为我不这么做就很容易忘记。

#1


73  

FOR %%A IN (list) DO command parameters

list is a list of any elements, separated by either spaces, comma's or semicolons.

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

command can be any internal or external command, batch file or even - in OS/2 and NT - a list of commands

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

parameters contains the command line parameters for command. In this example, command will be executed once for every element in list, using parameters if specified.

参数包含命令行参数。在本例中,将对列表中的每个元素执行一次命令,如果指定了参数的话。

A special type of parameter (or even command) is %%A, which will be substituded by each element from list consecutively.

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

From FOR loops

从循环

#2


154  

If you want to do something x times, you can do this:

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

Example (x = 200):

示例(x = 200):

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

1,1,200 means:

1200的意思是:

  • Start = 1
  • 开始= 1
  • Increment per step = 1
  • 每步增量= 1
  • End = 200
  • 结束= 200

#3


41  

Type:

类型:

for /?

and you will get several pages of help text.

你会得到几页帮助文本。

#4


17  

Conditionally perform a command several times.

有条件地多次执行命令。

  • syntax-FOR-Files

    syntax-FOR-Files

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

    syntax-FOR-Files-Rooted在路径

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

    syntax-FOR-Folders

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

    syntax-FOR-List的数字

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

    syntax-FOR-File内容

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

    or

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

    syntax-FOR-Command结果

    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
  • 使参数%G的a等于该数据的某些部分
  • 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命令,而不是在批处理程序中使用,只使用一个百分号:%G而不是%%G。

FOR Parameters

为参数

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

    第一个参数必须使用单个字符来定义,例如字母G。

  • FOR %%G IN ...

    % % G在…

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

    在每个迭代中一个FOR循环,在评估(....)条款和% % G设置为不同的值

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

    如果该子句导致一个值,那么将%%G设置为该值,并执行命令。

    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)

    如果子句导致多个值,那么将隐式地定义额外的参数来保存每个值。这些自动分配在字母顺序%%H %I %J…(隐式参数定义)

    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.

    你当然可以选择任何字母,而不是%%G。但这是一个很好的选择,因为它不与任何路径名格式的字母(a、d、f、n、p、s、t、x)冲突,并且提供最长的非冲突字母,用作隐式参数。

#5


11  

FOR will give you any information you'll ever need to know about FOR loops, including examples on proper usage.

FOR将提供您需要了解的关于循环的任何信息,包括有关正确用法的示例。

#6


9  

Try this code:

试试这段代码:

@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 is the number to be incremented and num2 is the value added to num1 and terminator is the value where the num1 will end. You can indicate different value for terminator in this statement (if %num1%==%terminator% goto close). This is the boolean expression goto close is the process if the boolean is true and goto open is the process if the boolean is false.

num1是要增加的数字,num2是添加到num1的值,而终止符是num1将要结束的值。在这个语句中,您可以指示终止符的不同值(如果%num1%= %终止符% goto close)。这是布尔表达式goto close是布尔为真的过程,goto open是布尔为假的过程。

#7


3  

@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

#8


1  

From FOR /? help doc:

从/ ?帮助医生:

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

对于(set)中的%变量DO命令[命令参数]

%variable Specifies a single letter replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be used. command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.

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

To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
from %I.

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

If Command Extensions are enabled, the following additional
forms of the FOR command are supported:

如果启用命令扩展,则支持FOR命令的下列其他形式:

FOR /D %variable IN (set) DO command [command-parameters]

对于(set)中的/D %变量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-parameters]

FOR /R[驱动器:]%变量IN (set) DO命令[命令参数]

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-parameters]

对于/L %变量IN (start,step,end)执行命令[命令参数]

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)                                     

#9


0  

for e.x.

e.x。

for /L %n in (210,1,270) do @ffmpeg -i  "C:\Users\x\IMG_0%n.MOV" -vcodec copy -acodec copy "C:\Users\x\out\IMG_0%n.MP4"

it convert file format .mov to .MP4 from "IMG_0210.MOV" to "IMG_0270.MOV"

它将文件格式.mov从“IMG_0210”转换为. mp4。MOV”到“IMG_0270.MOV”

sorry for posting the stupid answer, for I will forget easily without doing it.

对不起,我发了这个愚蠢的答案,因为我不这么做就很容易忘记。