Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read registry string value from batch file

Tags:

batch-file

I want to read registry string value, from bat file, and then assing the readed value to 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 it doesn't work.

like image 387
user152508 Avatar asked Jul 06 '10 08:07

user152508


People also ask

How do I read a value from the Windows Registry?

Use the GetValue method, specifying the path and name) to read a value from registry key. The following example reads the value Name from HKEY_CURRENT_USER\Software\MyApp and displays it in a message box.

How do I read registry keys?

You can use Get-ChildItem to view registry keys and Set-Location to navigate to a key path. Registry values are attributes of a registry key. In the Registry drive, they are called Item Properties. A registry key can have both children keys and item properties.

What does %% mean in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


1 Answers

If the name of the value (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 only known at run time, you would have to use tokens=* and parse %%A looking for "REG_" so you know where the data starts...

like image 131
Anders Avatar answered Sep 29 '22 21:09

Anders