Reputation: 484
These commands are inside a bat file which has delayed expansions enabled. I'm trying to check for a non existent command so that if it fails my programs doesn't hang up and I have a flag which can help me.
The flag here is path_check
:
set /a path_check=1
echo !PATH! | ( findatr /i "C:\\Windows\\System32;" 2>NUL || echo "here")
this prints "here" with success:
set /a path_check=1
echo !PATH! | ( findatr /i "C:\\Windows\\System32;" 2>NUL || set /a path_check=1
)
echo !path_check!
this prints 1 instead of 0.
I can't attribute anything to this inconsistent behavior. In case the command exists but fails otherwise the program is responding fine.
EDIT
Based on the responses, I want to highlight that I'm using a non existent command and NOT a command that returns an error but exists otherwise. findatr
is not a typo, it's intentionally that because I want to check for a non existent command.
Upvotes: 0
Views: 73
Reputation: 82307
Your code works, but not at you expect!
The variable path_check
is set to 1, but as the pipe creates a new cmd-context, the variable is only valid/accessible there.
And after the pipe is ready the sub cmd-context will be removed and also the variable.
But perhaps you should change your solution to first check if your program is in the path, before you try to start it.
set "path_check=0"
for /f "delims=" %%I in ("findatr.exe") do (
if "%~$PATH:I" == "" set "path_check=1"
)
echo %path_check%
Upvotes: 0
Reputation: 41234
newcommand >nul 2>nul & if errorlevel 9009 echo command doesn't exist (on the path)
Upvotes: 1
Reputation: 37569
example (prints 0
if found, otherwise 1
):
set /a path_check=0 echo %PATH% | findstr /i "C:\\Windows\\System32;" 2>NUL || set /a path_check=1 echo %path_check%
Upvotes: 1