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

时间:2010-10-01 03:57:22

标签: windows curl batch-file wget

在我的.bat文件中,如何通过用户可能已经完成的任何其他安装来检查系统中是否有wget或curl。这个检查是否可行,我可以在文件中使用if then else逻辑来做出不同的反应,就像在正常编程中一样。我基本上想用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

2 个答案:

答案 0 :(得分:3)

如果你知道你期望找到EXE的路径,那就很容易了:

IF EXIST C:\Windows\wget.exe ( *** do something with it ***)

...当然,您可以使用blurb 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
  ) 

你可以将整个块包装成一个函数并为每个EXE调用一次(将%TARGET%更改回%~1,给它一个:TITLE,然后call :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...

答案 1 :(得分:1)

Powershell v3 CTP1附带了一个像wget / curl这样的命令。它叫做Invoke-Web-Request。要了解详情,请访问以下帖子:http://rambletech.wordpress.com/2011/09/21/windows-powershell-v3-includes-command-like-wgetcurl/