【发布时间】:2010-11-24 06:09:02
【问题描述】:
Windows 批处理文件中 FOR 循环的语法是什么?
【问题讨论】:
标签: batch-file
Windows 批处理文件中 FOR 循环的语法是什么?
【问题讨论】:
标签: batch-file
FOR %%A IN (list) DO command parameters
list 是任何元素的列表,以空格、逗号或分号分隔。
command 可以是任何内部或外部命令、批处理文件,甚至 - 在 OS/2 和 NT 中 - 命令列表
parameters 包含命令的命令行参数。 在本例中,命令将对列表中的每个元素执行一次,如果指定,则使用参数。
一种特殊类型的参数(甚至命令)是 %%A,它将被列表中的每个元素连续替换。
【讨论】:
FOR /L %x IN (1,1,100) DO ...
for 循环中包含多行,请将括号放在 [ parameters ] 周围
来自FOR /?帮助文档:
FOR %variable IN (set) DO 命令 [command-parameters]
%variable 指定单个字母可替换参数。
(set) 指定一组一个或多个文件。可以使用通配符。
command 指定对每个文件执行的命令。
命令参数
为指定的命令指定参数或开关。
要在批处理程序中使用 FOR 命令,请改为指定 %%variable
% 变量。变量名区分大小写,因此 %i 不同
从%I。
如果启用了命令扩展,则以下附加
支持 FOR 命令的形式:
FOR /D %variable IN (set) DO 命令 [command-parameters]
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]
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-parameters]
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)
【讨论】:
FOR 将为您提供您需要了解的有关 FOR 循环的任何信息,包括有关正确使用的示例。
【讨论】:
如果你想做某件事x次,你可以这样做:
示例(x = 200):
FOR /L %%A IN (1,1,200) DO (
ECHO %%A
)
1,1,200 表示:
【讨论】:
有条件地多次执行一个命令。
语法-FOR-文件
FOR %%parameter IN (set) DO command
syntax-FOR-Files-Rooted at Path
FOR /R [[drive:]path] %%parameter IN (set) DO command
语法-FOR-文件夹
FOR /D %%parameter IN (folder_set) DO command
语法-FOR-数字列表
FOR /L %%parameter IN (start,step,end) DO command
语法-FOR-文件内容
FOR /F ["options"] %%parameter IN (filenameset) DO command
或
FOR /F ["options"] %%parameter IN ("Text string to process") DO command
语法-FOR-命令结果
FOR /F ["options"] %%parameter IN ('command to process') DO command
它
%%G 等于该数据的某些部分如果您在命令行而不是批处理程序中使用 FOR 命令,请仅使用一个百分号:%G 而不是 %%G。
FOR 参数
必须使用单个字符定义第一个参数,例如字母 G。
FOR %%G IN ...
在 FOR 循环的每次迭代中,都会评估 IN ( ....) 子句并将 %%G 设置为不同的值
如果此子句产生单个值,则将 %%G 设置为等于该值并执行命令。
如果子句产生多个值,则隐式定义额外的参数来保存每个值。这些是按字母顺序自动分配的%%H %%I %%J ...(隐式参数定义)
如果参数引用文件,则可以使用增强的变量引用来提取文件名/路径/日期/大小。
您当然可以选择%%G 以外的任何字母。但它是一个不错的选择,因为它不与任何路径名格式字母(a、d、f、n、p、s、t、x)冲突,并提供最长的非冲突字母运行以用作隐式参数.
【讨论】:
%a% 是否给出了常规文件的名称?这种格式很难帮助你。阅读How to Ask后,请随时提出新问题。
So for example in a reference like %%~fG the %%G is the FOR parameter, and the ~f is the Parameter Expansion. 知道了(这里)[ss64.com/nt/syntax-args.html),谢谢你。干杯伙伴。
@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
【讨论】:
试试这个代码:
@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 的值,terminator 是 num1 将结束的值。您可以在此语句中为终止符指定不同的值 (if %num1%==%terminator% goto close)。这是布尔表达式 goto close 是布尔值为真时的进程,goto open 是布尔值为假时的进程。
【讨论】:
类型:
for /?
你会得到几页帮助文本。
【讨论】: