I have come up with a batch script, through some Googling and analysis on batch scripts, for checking if the package I mention in user prompt exists in system and get its version if it does. Here it is in its most possible final version:
@echo off && setlocal EnableDelayedExpansion
set "output_cnt=0"
set /p "pkg=Package? "
for /f "delims=" %%a in ('npm -v 2^>nul') do @set "npmV=%%a"
if defined npmV (echo NPM Version: %npmV%) else (echo NPM isn't installed)
for /f "delims=" %%b in ('node -v 2^>nul') do @set "nodeV=%%b"
if defined nodeV (echo Node Version: %nodeV%) else (echo Node isn't installed)
for /f "delims=" %%c in ('npm list -g %pkg% 2^>nul') do (
set /a output_cnt+=1
set "pkgV[!output_cnt!]=%%c"
)
if defined pkgV (for /l %%n in (1 1 !output_cnt!) do echo !pkg! Version: !pkgV[%%n]!) else (echo %pkg% isn't installed)
setlocal DisableDelayedExpansion && endlocal && pause
Now when I run this script, it does check properly whether Node and NPM are installed and gets their version accurately, but when I pass any package name to the user prompt (e.g. cowsay, and this package is installed, of that I am sure), it always says <package> isn't installed(as in cowsay isn't installed).
As you can see I am trying to capture full output of npm list -g <package> with multiple lines through array, but it clearly screws up.
Can anyone assist in figuring out what's wrong and get what I want?