Windows Batch: reg keyword value for a variable, but does not display an error if the key does not exist

I have the following command command that retrieves a registry key and assigns a value to a variable, but displays an error when the key does not exist

for /f "tokens=2,*" %%a in ('reg query HKLM\Software\MySoftware\1.0\MyExecutable /v "InstallDir" ^| findstr InstallDir') do set InstallPath=%%b

Is there a way around the exception? I tried to use 2> NUL after the reg request or at the end of the command, but at this time I got exception 2>.

help / guidance highly appreciated

+3
source share
2 answers

You have to solve the problem, like with a pipe. ^|
Just open it until2^>NUL

So you get

for /f "tokens=2,*" %%a in ('reg query HKLM\Software\MySoftware\1.0\MyExecutable /v "InstallDir" 2^>NUL ^| findstr InstallDir') do set InstallPath=%%b

, FOR-Loop . ( 2 > NUL ), cmd.exe( 2 > NUL stderr reg)

+5

reg ? ( findstr):

for /f "tokens=2,*" %%a in ('reg query HKLM\Software\MySoftware\1.0\MyExecutable /v "InstallDir" 2>NUL ^| findstr InstallDir') do set InstallPath=%%b
0

All Articles