在“for”循环中命名控制变量

问题描述 投票:0回答:1

功能代码如下:

:: The INDEX control variable is causing the error.
FOR /L %%INDEX IN (%1, %2, %3) DO ECHO %%INDEX

:: Syntax
FOR /L %%parameter IN (start, step, end) DO command

我按如下方式运行此脚本:

test.bat 0 1 5

我收到以下错误:

%INDEX was unexpected at this time.

我注意到,出现此错误是因为

INDEX
循环中控制变量 (
FOR
) 的字符计数。如果我将
INDEX
控制变量更新为单个字符(例如
X
),程序将按预期运行。

FOR
循环中命名控制变量的规则是什么?

windows loops for-loop batch-file cmd
1个回答
0
投票
:: Syntax
FOR /L %%parameter IN (start, step, end) DO command

这里

%%parameter
必须是一个单个字母可替换参数。在打开的
for /?
窗口中运行
cmd


for /?
Runs a specified command for each file in a set of files.

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

  %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.

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.

If Command Extensions are enabled, the following additional
forms of the FOR command are supported:
…
FOR /L %variable IN (start,step,end) DO command [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)
…
© www.soinside.com 2019 - 2024. All rights reserved.