变量表达式%Var%在批处理文件中的for循环中不起作用

时间:2018-07-03 15:53:21

标签: batch-file for-loop cmd variable-expansion

FOR /F "tokens=* USEBACKQ" %F IN (java -jar %MAVEN_HOME%\jenkins-cli.jar -s http://someip.com build /dev/ -s  --username **** --password ***) DO (SET var=%F)

此处%MAVEN_HOME%的值未替换为实际值,但是当我在没有for循环的情况下运行它时,它工作正常。

有人可以解决这个问题,并用实际值替换MAVEN_HOME吗?

1 个答案:

答案 0 :(得分:1)

您需要像这样将命令放入` `

FOR /F "tokens=* USEBACKQ" %F IN (`java -jar %MAVEN_HOME%\jenkins-cli.jar -s http://someip.com build /dev/ -s  --username **** --password ***`) DO (SET var=%F)

因为语法是

C:\>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.

...

FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

    or, if usebackq option present:

FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ('string') DO command [command-parameters]
FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]

如您所见,您有USEBACKQ,因此该命令必须被反引号包围,并且还说如果要批量使用for命令,则必须将百分比加倍文件