0

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?

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • You can also tell me about other ways to check if choice.exe exists – Sean Armecin Dec 16 '20 at 16:12
  • Your method will fail, because the code after `||` is always going to be executed since `choice` always sets the `ErrorLevel`; you could use `where choice > nul 2>&1 && (echo found) || (echo not found)` to find out whether or not the command exists… – aschipfl Dec 16 '20 at 16:34
  • I tested my method and what you say is true. But if I were to use yours I would now have to check if where.exe exists since that is the application that has the where command. – Sean Armecin Dec 16 '20 at 16:46
  • Sean that is an excellent observation, especially because `where.exe` wasn't built-in prior to Windows Vista, whereas `choice.exe` was, _(although for some reason it was excluded in Windows 2000 and XP)_. Please note however, that Windows 2003 has choice.exe and all versions from Windows Vista, which was released over fourteen years ago, so at some point you should decide not to support somebody using an Operating System so outdated, and very likely not secure. – Compo Dec 16 '20 at 17:35

2 Answers2

0

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
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • Thank you! I didn't even know that %SystemRoot% existed – Sean Armecin Dec 16 '20 at 16:47
  • 1
    Please note that with this method, you should within the rest of your script, also use `%SystemRoot%\System32\choice.exe` each time you use the utility. Doing that would prevent potential issues with easily modified variables, as `choice.exe` would still rely upon `%Path%`, and even worse, `choice` would rely on both `%Path%` and `%PATHEXT%` – Compo Dec 16 '20 at 17:27
  • Are you saying that instead of using the command "choice" I should replace it with "%SystemRoot%\System32\choice.exe"? – Sean Armecin Dec 16 '20 at 17:37
  • Yes @SeanArmecin, and in my opinion everybody should always use that methodology all of the time, _instead of assuming no malicious or accidental modifications have taken place_. You could of course define a variable yourself to include the path, then just use that variable throughout your code, e.g. `Set "Choice=%SystemRoot%\System32\choice.exe"`, then of course, `If Not Exist "%Choice%" GoTo nochoiceapp`, and in future commands `%Choice% /C …` or `"%Choice%" /C …` – Compo Dec 16 '20 at 17:46
0

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.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • you can make it language independent by using `/c yn`. – Stephan Dec 16 '20 at 19:13
  • Thanks for the additional information @Stephan, I was aware of that as an additional option, and should, I suppose, have mentioned it. The reason I didn't was simply to keep the code short enough to have been worth posting, given that an answer had already been accepted. – Compo Dec 16 '20 at 19:22