当嵌套在if语句中时,启动cmd在Windows批处理文件中无法正确执行

时间:2018-09-10 11:45:47

标签: windows batch-file

无论出于何种原因,此命令都有效:

SET QUERY="heat wave"
START /d "C:\Program Files (x86)\Google\Chrome\Application" chrome.exe https://www.google.com/search?q=%QUERY: =+%

但这不是:

SET SWITCH=1
IF %SWITCH%==1 (
SET QUERY="heat wave"
START /d "C:\Program Files (x86)\Google\Chrome\Application" chrome.exe https://www.google.com/search?q=%QUERY: =+%
)

1 个答案:

答案 0 :(得分:1)

这里的关键是去enabledelayedexpansion

setlocal enabledelayedexpansion
SET SWITCH=1
IF %SWITCH%==1 (
SET QUERY="heat wave"
START /d "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://www.google.com/search?q=!QUERY: =+!
)

有关delayedexpansion的更多信息,请从cmdline执行set /?

如果没有delayedexpansion,也可以通过将最后一个%加倍并调用start来实现。

SET SWITCH=1
IF %SWITCH%==1 (
SET QUERY="heat wave"
call START /d "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://www.google.com/search?q=%%QUERY: =+%%
)