我有一个Windows bat 文件和面临的问题,设置路径命令存在,如果条件是得到执行。
set browser=chrome
echo Browser value: %browser%
if "%browser%"=="chrome" (
echo "Browser passed is Chrome"
echo Chrome browser version before upgrade
reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version
echo Upgrade chrome browser to the latest version
choco upgrade googlechrome -y --ignore-checksums
echo Check the exit code of googlechrome upgrade command
if %ERRORLEVEL% neq 0 (
echo "Failed to upgrade googlechrome."
exit /b 1
)
echo "Chrome browser is upgraded successfully. Chrome browser version after upgrade,"
reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version
:: Install chromedriver using curl
echo "Install chromedriver from official google website"
C:
cd Users\myuser\Downloads
echo Set the installation directory
set "INSTALL_DIR=C:\Users\myuser\Downloads"
set DESTINATION_DIR=H:
echo Download latest chromeDriver for 32-bit Windows
set browserVersion=curl https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_STABLE
curl -o "%INSTALL_DIR%\chromedriver-win32.zip" https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/%browserVersion%/win32/chromedriver-win32.zip
:: Check the exit code of the chromedriver download command
if %ERRORLEVEL% neq 0 (
echo "Failed to install chromedriver."
exit /b 1
)
echo "Chromedriver installation is successful."
unzip chromedriver-win32.zip -d %DESTINATION_DIR%
echo Add the directory to the system's PATH
set PATH=%DESTINATION_DIR%\chromedriver-win32;%PATH%
echo %PATH%
H:
echo "Current chromedriver version"
chromedriver -version
) else (
echo this is else *****
)
字符串
在实际执行之前,设置路径命令正在执行。
输出为:
C:\Users\myuser>set browser=chrome
C:\Users\myuser>echo Browser value: chrome
Browser value: chrome
\<abc>\<def> was unexpected at this time.
C:\Users\myuser> set PATH=\chromedriver-win32;C:\Program Files\Docker\Docker\Resources\bin;<.... rest of the path>
C:\Users\myuser>
型
甚至尝试使用setx如下,并面临同样的问题,setx /M PATH“%DESTINATION_CHROMEDRIVER-WIN32;%PATH%”
自动化是完全阻止由于这个问题,感谢任何帮助!
1条答案
按热度按时间n8ghc7c11#
应该先读取Variables are not behaving as expected和How does the Windows Command Interpreter (CMD.EXE) parse scripts?整个命令块在条件
if "%browser%"=="chrome"
的行中以(
开始,在命令行) else (
中以)
结束,以及小的else
命令块由 *Windows命令处理器 * 完全解析通过在之前使用语法%VariableName%
扩展所有变量引用,根本就不计算IF条件。简单的解决方案是使用带有命令后藤的IF条件来控制命令执行顺序,以避免如此大的命令块。与支持完全不同的块的更现代的脚本解释器相比,*Windows命令处理器 * 专为这种一个命令行接一个命令执行行为而设计。
批处理文件代码使用两种技术来避免命令块:
1.使用后藤命令的IF条件,如
if /I not "%browser%" == "chrome" goto OtherBrowser
。1.命令操作符,如
&
,如single line with multiple commands using Windows batch file所述。完全不需要修改local,user或system环境变量
PATH
。最终安装的chromedriver.exe
可以简单地引用其众所周知的完全限定文件名。请查看What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?它详细解释了环境变量PATH
如何由Windows管理并由 * 使用Windows命令处理器 *cmd.exe
。下面的批处理文件被完全重写。它首先检查是否存在所需的可执行文件,如
curl.exe
,choco.exe
和tar.exe
。它们的存在取决于Windows的版本(curl.exe
和tar.exe
)分别 * 巧克力 *(choco.exe
)。批处理文件退出,并显示相应的错误消息,并且这三个程序中的一个程序上的退出代码10不存在,其中分别找不到预期的退出代码。字符串
有一个不区分大小写的字符串比较,用于确定批处理文件是否应该更新 Google Chrome 并安装最新稳定版本的 Google ChromeDriver,或者在标签
OtherBrowser
下面继续批处理文件。有关如何通过CMD的内部命令IF进行字符串比较的详细信息,请参阅symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files。如果在真实的批处理文件中有一个提示用户更新哪个浏览器的选择菜单,则应该使用命令CHOICE。阅读以下答案:How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?它详细解释了如何使用
%SystemRoot%\System32\choice.exe
以及使用Windows command比set /P
更好地进行选择提示的原因。不需要在整个批处理脚本中更改当前工作目录,这就是为什么根本不使用命令CD的原因。使用完全限定的文件和文件夹名称是最好的,因为比使用相对路径和不完整的文件名更安全,更快。
目录
%USERPROFILE%\Downloads
只是Internet浏览器默认下载文件的Windows默认文件夹。用户只需单击几下鼠标即可轻松将首选Downloads文件夹更改为其他shell文件夹。用于获取首选Downloads的完全限定文件夹名称的代码文件夹的详细信息在我的How to create a directory in the user's desktop directory?回答中有详细描述,只是更改了注册表值名称和环境变量名称,以确定用户的下载目录。应该在命令提示符窗口中运行一次:
型
所有注册的shell文件夹的输出应该有助于理解获取完全限定的Downloads文件夹名称的代码。
在命令提示符窗口中执行
if /?
或help if
会导致输出命令IF的用法帮助。在第一个输出帮助页面上解释了用于评估分配给动态的命令或可执行文件的退出代码的语法。(不是 * 环境 *)变量ERRORLEVEL
。关于Difference between Dynamic Environment Variables and Normal Environment Variables in CMD的答案演示了使用推荐语法IF ERRORLEVEL 1
或使用条件命令运算符||
与最差语法if %ERRORLEVEL% neq 0
相比的优势。要了解所使用的命令及其工作方式,请打开command prompt窗口,在那里执行以下命令,并仔细阅读每个命令的帮助页面。
choco --help
cmd /?
curl --help
del /?
echo /?
个endlocal /?
个exit /?
个for /?
goto /?
if /?
md /?
reg /?
个reg query /?
rem /?
set /?
setlocal /?
tar --help
有关
2>nul
的解释,请阅读有关Using command redirection operators的Microsoft文档。在执行命令FOR之前,Windows命令解释器处理此命令行时,必须使用FOR命令行上的插入字符^
对重定向运算符>
进行转义,以将其解释为原义字符它使用在后台启动的单独命令进程执行嵌入式命令行,并将%ComSpec% /c
和'
内部的命令行作为附加参数。