How to read the value of a registry string from a batch file

I want to read the value of a registry string from a bat file, and then pass the readed value variable. I tried the following:

FOR %%a in ('REG QUERY HKLM\SOFTWARE\MathWorks\MATLAB\7.10 /v MATLABROOT') DO set MATLAB=%%a echo %MATLAB% 

but that will not work.

+4
source share
2 answers

If the value name (baz in this case) does not contain spaces, you can do something like

 FOR /F "skip=4 tokens=2,*" %%A IN ('REG.exe query "HKLM\software\foo\bar" /v "baz"') DO set "MATLABROOT=%%B" 

If the name is dynamic and known only at runtime, you will need to use tokens = * and parse %% A looking for "REG_" so you know where the data starts ...

+6
source

reg displays more than just the value you are interested in. As far as I can tell, the possibility of skip=2 and tokens=3 for option might work.

You may need to sort this out a bit, but if there are more gaps than expected.

+2
source

Source: https://habr.com/ru/post/1314816/


All Articles