Rob van der Woude's Scripting Pages

Make batch files

automatically prompt to download

third party tools

Many batch files available on this site rely on "third party tools", little programs that are not part of the standard Windows distributions.
Examples of these (though actually not really third party, since they are from the same manufacturer) are DEVCON and the Resource Kit tools.

Tools like these enable batch files to accomplish tasks that otherwise would have been impossible from the command line.
However, when distributing batch files that use these tools, we can only hope that other users also have these tools availble. If not, our carefully constructed batch file will fail.

Messages like 'devcon' is not recognized as an internal or external command, operable program or batch file are not very informative to someone who just downloaded and started GetDevDJ.bat.

Can we somehow prevent these error messages?

Yes, we can.

In some cases, we can even prompt the user to download the missing tools.

Detection

To prevent the error message for missing tools, we need to find a way to check its availability without generating an error message.
In Windows NT 4/2000/XP this is easy:

DEVCON >NUL 2>&1

returns a non-zero errorlevel if DEVCON is not available on the computer.
So to detect the missing tool we could use:

DEVCON >NUL 2>&1
IF ERRORLEVEL 1 ECHO DEVCON wasn't found, please install it and try again

In this case, however, that won't work.
DEVCON itself returns a non-zero errorlevel when invalid (or no) command line arguments are used.
So our batch file would always think DEVCON isn't available.

Fortunately, passing DEVCON the /? command line switch makes sure an errorlevel 0 is returned.
So the following code does work:

DEVCON /? >NUL 2>&1
IF ERRORLEVEL 1 ECHO DEVCON wasn't found, please install it and try again

In this case, if DEVCON is available it will return errorlevel 0, if it is not available the command processor will return a non-zero errorlevel.

So, what we found so far is:

The second requirement may be the hardest: making sure the tool itself returns an errorlevel 0.

In case we cannot be sure of that, or in case we aren't even sure if the command processor will return non-zero errorlevels (e.g. Windows 9x), we still have FIND.

FIND can be used to search for a known substring in the screen output of the tool.
Suppose a tool called TESTTOOL would always return a non-zero errorlevel, then we could still use FIND to detect its availability by checking for a known word or substring (e.g. "copyrights") in TESTTOOL's screen output:

(TESTTOOL /? 2>&1) | FIND /I "copyrights" >NUL
IF ERRORLEVEL 1 ECHO TESTTOOL wasn't found, please install it and try again
Notes: (1) In Windows NT 4 and later, the parentheses make sure the command processor continues with the redirection even if TESTTOOL wasn't found.
  (2) For Windows 9x batch files, you will need to remove the parentheses as well as 2>&1 from the code.
Removing this redirection, however, will make this code fail if TESTTOOL /? shows the word "copyrights" in standard error instead of standard output!

Download prompt

Once we have detected the tool is missing, we can either display our own error message (e.g. jump to the help screen) or prompt the user for action (e.g. download the tool).

I won't go over the jump and error message details, read more on GOTO if you're not yet familiar with it.

To prompt the user for download isn't hard to do in Windows 9x/2000/XP.
In Windows NT 4 it can be quite a challenge without CHOICE.EXE from the Resource Kit (which is a "third party tool" itself).
Read my page on user input for more details.

To perform the actual download without user interaction would itself require "third party tools" or another scripting language (e.g. VBScript).
That is why I choose to just open the correct download web page instead.
It still requires user interaction, but on the other hand it often isn't necessary to modify the download code with every update of the tool.
And it is really simple: just use the START command!

As an example, the code I use to check DEVCON's availability in Windows 2000/XP is shown here:

:: Initialize the variables we need
SET DevconAvailable=
SET Download=

:: Check if DEVCON.EXE is available and if not, prompt for download
DEVCON.EXE /? >NUL 2>&1
IF ERRORLEVEL 1 (
	SET DevconAvailable=No
	ECHO This batch file requires Microsoft's DEVCON utility.
	SET /P Download=Do you want to download it now? [y/N] 
)

:: Start download if confirmed
IF /I "%Download%"=="Y" (
	START "DevCon" "http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q311272"
	ECHO.
	ECHO Install the downloaded files and make sure DEVCON.EXE is in the PATH.
	ECHO Then try again.
)

:: Abort if DEVCON.EXE is not available yet
IF "%DevconAvailable%"=="No" GOTO:EOF

page last modified: 2017-02-11; loaded in 0.0112 seconds