The command to query a registry key with a batch command is
REG QUERY ...
Specific usage information is entered in the Command line window
REG QUERY/?
Here we need to query a specific key-value, then use the following command
Reg QUERY [reg PATH]/V [KEY]
As an example,
" HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\110\tools\clientsetup " " Path "
The results appear as follows:
" hkey_local_machine\software\microsoft\microsoft SQL server\\tools\clientsetup" "Path"hkey_local_machine\software\microsoft\microsoft SQL server\ \tools\clientsetup Path REG_SZ C:\Program Files\Microsoft SQL Server\110\tools\binn\
We're going to take the red part out, notice that the reg batch command here is the output of two lines, and we're going to take the second half of the next line. We use for to take this part out. Let's start with the basic structure of for:
for/F "tokens=1,2 delims=:" %%a in (' REG QUERY [Reg_path]' do ( set"value1=%%a " Set " value2=%%b " )
The For loop handles the two lines that are taken out of the REG query, each of which is stored in the variable%%a, but uses tokens and delims,delims to specify the characters that divide the string, which is the ":" to split each line of string, tokens=1,2 Indicates that the first two columns after the split are removed, the column is saved in%%a, and the second column is saved in%%b (if there is a third column saved in%%c) ... Keep Looking at the example above.
echo off for/F"tokens=1,2 delims=:"%%ainch('reg QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL server\110\tools\clientsetup"/V "Path"') Do( Set "value1=%%a"Echo%value1%Set "value2=%%b"Echo%value2%)
The final variable value
value1 = Path REG_SZ C
value2 = \program Files\Microsoft SQL Server\110\tools\binn\
Connect the value1 first character (that is, the drive letter C) and the value2 to the middle by:
Set " value2=%value1:~-1%:%value2% "
To read a path value for a specific registry key with a batch process