如果条件在windows bat文件中执行,则设置路径命令

lztngnrs  于 7个月前  发布在  Windows
关注(0)|答案(1)|浏览(70)

我有一个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%”
自动化是完全阻止由于这个问题,感谢任何帮助!

n8ghc7c1

n8ghc7c11#

应该先读取Variables are not behaving as expectedHow 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所述。
完全不需要修改localusersystem环境变量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.exechoco.exetar.exe。它们的存在取决于Windows的版本(curl.exetar.exe)分别 * 巧克力 *(choco.exe)。批处理文件退出,并显示相应的错误消息,并且这三个程序中的一个程序上的退出代码10不存在,其中分别找不到预期的退出代码。

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Check the existence of curl.exe required for downloading Google ChromeDriver.
if exist %SystemRoot%\System32\curl.exe set "CurlExe=%SystemRoot%\System32\curl.exe" & goto CheckChoco
for %%I in (curl.exe) do set "CurlExe=%%~$PATH:I"
if not defined CurlExe echo ERROR: File not found: curl.exe& exit /B 10

:CheckChoco
rem Check the existence of choco.exe required for upgrading Google Chrome.
for %%I in (choco.exe) do set "ChocoExe=%%~$PATH:I"
if not defined ChocoExe echo ERROR: File not found: choco.exe& exit /B 10

rem Check the existence of tar.exe required for extraction of ZIP files.
if not exist %SystemRoot%\System32\tar.exe echo ERROR: File not found: tar.exe& exit /B 10

set "browser=chrome"
echo Browser value: %browser%

if /I not "%browser%" == "chrome" goto OtherBrowser

echo Browser passed is Chrome.
for /F "skip=2 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe QUERY HKCU\Software\Google\Chrome\BLBeacon /v version 2^>nul') do if /I "%%I" == "version" if not "%%K" == "" echo Chrome browser version before upgrade: %%K

echo Upgrading Chrome browser to the latest version...
"%ChocoExe%" upgrade googlechrome -y --ignore-checksums
if errorlevel 1 echo ERROR: Failed to upgrade Google Chrome.& exit /B 1
echo Chrome browser is upgraded successfully.
for /F "skip=2 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe QUERY HKCU\Software\Google\Chrome\BLBeacon /v version 2^>nul') do if /I "%%I" == "version" if not "%%K" == "" echo Chrome browser version after upgrade: %%K

rem Determine the user's preferred downloads folder.
set "DownloadsFolder="
for /F "skip=2 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "{374DE290-123F-4565-9164-39C4925E467B}" 2^>nul') do if /I "%%I" == "{374DE290-123F-4565-9164-39C4925E467B}" if not "%%~K" == "" if "%%J" == "REG_SZ" (set "DownloadsFolder=%%~K") else if "%%J" == "REG_EXPAND_SZ" call set "DownloadsFolder=%%~K"
if not defined DownloadsFolder for /F "skip=2 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "{374DE290-123F-4565-9164-39C4925E467B}" 2^>nul') do if /I "%%I" == "{374DE290-123F-4565-9164-39C4925E467B}" if not "%%~K" == "" if "%%J" == "REG_SZ" (set "DownloadsFolder=%%~K") else if "%%J" == "REG_EXPAND_SZ" call set "DownloadsFolder=%%~K"
if not defined DownloadsFolder set "DownloadsFolder=\"
if "%DownloadsFolder:~-1%" == "\" set "DownloadsFolder=%DownloadsFolder:~0,-1%"
if not defined DownloadsFolder set "DownloadsFolder=%UserProfile%\Downloads"
if not exist "%DownloadsFolder%\" md "%DownloadsFolder%"

rem Determine version of latest stable release of Google ChromeDriver.
set "LatestRelease="
for /F %%I in ('"%CurlExe%" -s https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_STABLE') do set "LatestRelease=%%I"
if not defined LatestRelease echo ERROR: Failed to determine latest release version of Google ChromeDriver.& exit /B 2

set "ChromeDriverFile=%DownloadsFolder%\chromedriver-win32.zip"
echo Downloading 32-bit ChromeDriver to: "%ChromeDriverFile%"
del /A /F "%ChromeDriverFile%" 2>nul
"%CurlExe%" -s -o "%ChromeDriverFile%" "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/%LatestRelease%/win32/chromedriver-win32.zip"
if errorlevel 1 echo ERROR: Failed to download Google ChromeDriver ZIP file.& exit /B 3
if not exist "%ChromeDriverFile%" echo ERROR: Missing file: "%ChromeDriverFile%"& exit /B 4

echo Extracting the ChromeDriver ZIP archive file...
%SystemRoot%\System32\tar.exe -x -f "%ChromeDriverFile%" -C H:\
if errorlevel 1 echo ERROR: Failed to extract file: "%ChromeDriverFile%"& exit /B 5
del "%ChromeDriverFile%"

echo ChromeDriver installation to H:\ is successful.
echo Current ChromeDriver version:
H:\chromedriver.exe -version
goto EndBatch

:OtherBrowser
echo this is else *****

:EndBatch
endlocal

字符串
有一个不区分大小写的字符串比较,用于确定批处理文件是否应该更新 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 commandset /P更好地进行选择提示的原因。
不需要在整个批处理脚本中更改当前工作目录,这就是为什么根本不使用命令CD的原因。使用完全限定的文件和文件夹名称是最好的,因为比使用相对路径和不完整的文件名更安全,更快。
目录%USERPROFILE%\Downloads只是Internet浏览器默认下载文件的Windows默认文件夹。用户只需单击几下鼠标即可轻松将首选Downloads文件夹更改为其他shell文件夹。用于获取首选Downloads的完全限定文件夹名称的代码文件夹的详细信息在我的How to create a directory in the user's desktop directory?回答中有详细描述,只是更改了注册表值名称和环境变量名称,以确定用户的下载目录。
应该在命令提示符窗口中运行一次:

reg QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
reg QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"


所有注册的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'内部的命令行作为附加参数。

相关问题