2

I'm writing a small batch file in windows that needs to run a nodejs app. Before running the app I need to make sure that node is installed by the user, and if not show him a message that node is required.

What I did is this:

@echo OFF

setlocal EnableDelayedExpansion

REM Check if node is installed
for /f "delims=" %%i in ('node -v') do set output=%%i

IF "!output!" EQU "" (
    echo node could not be found
) else (
    node %~dp0app.js
)

If the user has node installed then output will contain the version number. If not installed then it will be empty. This logic works. But if node is not installed (node -v command not found), the batch result also shows the following output:

'node' is not recognized as an internal or external command,
operable program or batch file.
node could not be found

I would like to hide the "not recognized" message from the user and just show "node could not be found".

How can I hide it?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
developer82
  • 13,237
  • 21
  • 88
  • 153
  • 1
    cmd shell returns 9009 for %ERRORLEVEL% when a command doesn't exist. See http://stackoverflow.com/questions/23091906/official-ms-reference-for-cmd-exe-errorlevel-9009. No time to leave full answer. – indiv Jan 21 '16 at 17:28

3 Answers3

5

You can use this errorlevel check to test specifically for 9009, which is the return code for the program not found.

if "%errorlevel%" == "9009"

For your example this would work:

@echo OFF
REM Check if node is installed
node -v 2> Nul
if "%errorlevel%" == "9009" (
    echo node could not be found
) else (
    node %~dp0app.js
)
RLH
  • 1,545
  • 11
  • 12
  • thanks. this seems to do the trick but if node is installed it types the version number from running the command - i want to hide that as well. so it would be just a check if node exist (then run it) if not show message - transparent to the user. – developer82 Jan 22 '16 at 06:35
  • @developer82 Then change the line `node -v 2> Nul` to `node -v >Nul 2>&1` –  Jan 26 '17 at 21:53
5

to suppress the errormessage, redirect it to NUL:

set "output=not installed"
for /f "delims=" %%i in ('node -v 2^>nul') do set output=%%i
echo %output%

another way (inspired by Npocmaka's answer):

where node.exe >nul 2>&1 && echo installed || echo not installed

or to keep closer to your original output:

where node.exe >nul 2>&1 && node %~dp0app.js || echo node could not be found
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • (upvote)I forgot about `where` , though the command is not available on XP and some Win2003 installations. – npocmaka Jan 22 '16 at 13:28
2

The cleanest way is to check if the executable is presented in the path or in the local directory:

set "status=not installed"

if exist "./node.exe" (
    set "status=installed"
)

for %%# in (node.exe) do  if not "%%~f$PATH:#" equ "" set "status=installed"

echo %status%

if "%status%" equ "installed" (
   node %~dp0app.js
)

for the %%~f$PATH check FOR /?


Since windows 7 you have also the where command:

where node || (
 echo node is not installed
)
npocmaka
  • 55,367
  • 18
  • 148
  • 187