在我的 .bat 文件中,如何通过用户之前可能经历过的任何其他安装来检查 wget 或 curl 是否在系统中可用。这个检查是否可行,我的文件中是否可以有 if then else
逻辑来做出不同的 react ,就像我们在正常编程中所做的那样。我基本上想使用wget或curl来下载文件。
If (wget is available)
do something
else if (curl is available)
do something else
else
tell the user they are out of luck
最佳答案
如果您知道希望找到 EXE 的路径,则相当简单:
IF EXIST C:\Windows\wget.exe ( *** do something with it ***)
...当然,您可以使用 IF NOT EXIST
来复制它,或者使用 ELSE
语句。
否则,如果您不知道在哪里可以找到该文件,您可以使用类似的内容进行搜索(原始来源已找到 here ):
@echo off
SETLOCAL
(set WF=)
(set TARGET=wget.exe)
:: Look for file in the current directory
for %%a in ("" %PATHEXT:;= %) do (
if not defined WF if exist "%TARGET%%%~a" set WF=%CD%\%TARGET%%%~a)
:: Look for file in the PATH
for %%a in ("" %PATHEXT:;= %) do (
if not defined WF for %%g in ("%TARGET%%%~a") do (
if exist "%%~$PATH:g" set WF=%%~$PATH:g))
:: Results
if defined WF (
*** do something with it here ***
) else (
echo The file: "%~1" was not found
)
您可以将整个 block 包装到一个函数中,并为每个 EXE 调用一次(将 %TARGET%
s 改回 %~1
,给它一个: TITLE,然后调用 :TITLE wget.exe
)...
或者,您可以采取不同的方法,尝试这些命令,看看它们是否失败。由于 ERRORLEVEL 为 0 通常意味着成功,因此您可以执行以下操作:
wget -q <TARGET_URL>
IF NOT ERRORLEVEL 0 (
curl <TARGET_URL>
IF NOT ERRORLEVEL 0 (
ECHO Download failed!
EXIT 1
)
)
:: now continue on with your script...
关于windows - .bat 如何检查curl 或wget 是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3836448/