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

Variable expression %Var% not working in for loop in batch file

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 替换为实际值?

你需要像这样把命令放在` `

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 命令你必须将百分比加倍一个批处理文件