windows - .bat 如何检查curl 或wget 是否存在

标签 windows curl batch-file wget

在我的 .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/

相关文章:

使用分块传输编码的 curl

php - 是否有任何通用的 CRUD 客户端应用程序?

batch-file - 在bat文件中给定时间后结束命令

windows - 如何让旧的 VB6 应用程序在 Windows Vista 和 Windows 7 中运行?

c# - xaml 中带有集合的嵌套对象似乎会泄漏到相邻的集合中

python - 在 Windows 上使用 Python 3.4 64 位构建 VRPN 服务器

windows - 从 32 位 C dll 创建 64 位 dll

php - Paypal 自适应支付php

powershell - 如何从命令提示符或批处理文件将具有长名称的组添加到本地组?

java - 通过命令提示符启动和停止tomcat