I'm doing a batch script that has to check if there are some programs installed on the computer. For that, I execute programName --version
and I store the output in a variable. The problem is when I try to compare with a regular expression (only to know if this program exists in the machine). I'm trying this code, but does't work
>output.tmp node --version
<output.tmp (set /p hasNode= )
if "%hasNode%" == "[vV][0-9.]*" (echo Has node) else (echo You have to install node)
If I change the regular expression for the output of this command works properly, so I suppose that I'm doing a bad use of the regular expression (I've checked it and it's fine for the command's output)
Thanks four your help guys
Batch/cmd does not support regexes directly. You have to use findstr
for that, for example:
echo %node% | findstr /r "[vV][0-9.]*" >nul 2>&1 && (echo contains) || (echo does not contain)
or
echo %node% | findstr /r "[vV][0-9.]*" >nul 2>&1
if errorlevel 1 (echo does not contain) else (echo contains)
This trick delegates comparison to findstr
and than uses return code (errorlevel) from it.
(please note that regexes findstr
supports are also quite limited and has some quirks, more info http://ss64.com/nt/findstr.html)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With