How to execute the choice command but not do anything

230 Views Asked by At

My batch script uses the choice command. And in my script I would want to check if choice.exe exists because it is the application that has the choice command.

And I know exactly how to do that. By executing the choice command and making it so that if it fails, it exits the script

choice || goto:nochoiceapp

:nochoiceapp
echo Cannot continue with script
echo Your system doesn't have choice.exe
pause

But I want to do this at the start of my script. Not just when the choice command is needed.

And I want this to only check if choice.exe exists, not actually executing "choice". It would lead to a choice prompt

How do I make it so that it executes the choice command but does nothing?

2

There are 2 best solutions below

4
SomethingDark On BEST ANSWER

Choice is a regular system executable that's located in System32. You can simply check that the file exists.

if not exist "%SystemRoot%\System32\choice.exe" goto :nochoiceapp
2
Compo On

Here's an alternative, which allows for the Microsoft utility choice.exe to be located in the current directory, somewhere in %PATH%, or defined within the registry as directly executable.

choice.exe /D N /T 0 2> NUL 1>&2 & If Not ErrorLevel 2 GoTo nochoiceapp

The idea is that it automatically enters N to the default [Y,N]? prompt, which means that if the ErrorLevel is not 2, the executable did not exist in any of the locations, or it did, but was not the correct utility.

Please note for languages which do not use N as the default 'No' option, you should change the N accordingly.