How .bat can check if curl or wget exist
In my .bat file, how can I check if wget or curl are available in the system through whatever other previous installations the user may have went through. Is this check possible, and can I have if then else
logic in my file to react differently, like we do in normal programming. I basically want to use wget or curl to download a file.
If (wget is available)
do something
else if (curl is available)
开发者_Python百科do something else
else
tell the user they are out of luck
If you know the path where you'd expect to find the EXE, it's fairly easy:
IF EXIST C:\Windows\wget.exe ( *** do something with it ***)
...of course you could do IF NOT EXIST
with a blurb to copy it, or use an ELSE
statement.
Otherwise, if you don't know where you might find the file, you can search for it with something like this (original source found 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
)
You could wrap that whole block into a function and call it once for each EXE (change the %TARGET%
s back into %~1
, give it a :TITLE, then call :TITLE wget.exe
)...
Alternately, you could take a different approach and just try the commands and see if they fail. Since ERRORLEVEL of 0 usually means success, you could do something like this:
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...
Powershell v3 CTP1 comes with a command like wget/curl. It's called Invoke-Web-Request. To learn more, you can visit this post: http://rambletech.wordpress.com/2011/09/21/windows-powershell-v3-includes-command-like-wgetcurl/
精彩评论