In batch script how to loop through registry sub keys

1

From windows registry I am able to get the list of java version that are installed on my system using below code. But how to loop through the list and store Java home in an array?

@ECHO OFF
REG QUERY "HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Development Kit" /S
PAUSE

Output as follows:

output
-----------
D:\ITM\configuration 
experience\apm_datacollectors_win_8.1.4.0\j2se_datacollecto
r_win_8.1.4.0\j2sedc\.gdc\7.3.0.5.0\bin>config.bat

HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Development Kit
CurrentVersion    REG_SZ    1.8

HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Development Kit\1.7
JavaHome    REG_SZ    C:\Program Files\jdk1.7.0_79
MicroVersion    REG_SZ    0

HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Development Kit\1.7.0_79
JavaHome    REG_SZ    C:\Program Files\jdk1.7.0_79
MicroVersion    REG_SZ    0

HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Development Kit\1.8
JavaHome    REG_SZ    C:\Program Files\Java\jdk1.8.0_121
MicroVersion    REG_SZ    0

HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Development Kit\1.8.0_121
JavaHome    REG_SZ    C:\Program Files\Java\jdk1.8.0_121
MicroVersion    REG_SZ    0

HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Development Kit\1.8.0_121\MSI
INSTALLDIR    REG_SZ    C:\Program Files\Java\jdk1.8.0_121\
NOSTARTMENU    REG_SZ    0

Press any key to continue . . .

Amit

Posted 2017-05-23T14:52:42.467

Reputation: 11

What, exactly, are you trying to accomplish - that is, why do you think you need an array with the various JavaHome values? Batch does not support arrays; you may need to consider other tools. We can better advise you if you can clarify what your needs are. – Jeff Zeitlin – 2017-05-23T15:04:02.000

@JeffZeitlin Arrays are not native to batch files but can easily be implemented - an example Windows CMD script to count files and get filenames

– DavidPostill – 2017-05-23T15:58:32.623

Answers

1

There is no such thing as an array in MS-DOS, but you can loop through the results, and get the values for JavaHome like this:

for /f "tokens=3* usebackq" %%a in (`reg query "HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Development Kit" /S ^| find "JavaHome"`) do (
    echo %%a %%b
)

Berend

Posted 2017-05-23T14:52:42.467

Reputation: 1 824

Arrays are not native to batch files but can easily be implemented - an example Windows CMD script to count files and get filenames

– DavidPostill – 2017-05-23T15:58:49.273

@Berend: Thanks, the for loop that you shared above works fine and returns the result. In that for loop %%a is the variable which stores the value after iteration? Is my understanding correct and then what is %%b? – Amit – 2017-05-24T09:42:16.380

If the path contains a space, two variables are set, the second one (%%b) will contain everything after the first space. type help for on a command prompt to get an explanation of the tokens=3* option. – Berend – 2017-05-24T10:09:21.557

0

First of all thanks for the answer and making me (a java developer) aware that arrays are not supported in batch script :-)

What I want to accomplish? --> I am working on a monitoring agent, its currently supplied to the customers in form of a zip file. Customer unzips it and runs a config.bat file to configure the agent on its machine. This config.bat asks user to input JAVA_HOME (directory where java is installed in customers machine), as the agent is java based. My task is to enhance this customer experience by automatically searching for any java installed in his machine and do not to ask customer to actually input JAVA_HOME.

Now, one use case is that there is only a version of Java installed in customers machine. Then config.bat script should read that entry of java from windows registry. But what if there are multiple instances of java installed in customers machine? In such a case I will have to show to the customer that he has multiple instances of java in his machine and he can choose one out of those for the agent installation.

@Berend: Thanks, the for loop that you shared above works fine and returns the result as below. In that for loop %%a is the variable which stores the value after iteration? Is my understanding correct and then what is %%b?

D:\ITM\configuration 
experience\apm_datacollectors_win_8.1.4.0\j2se_datacollecto
r_win_8.1.4.0\j2sedc\.gdc\7.3.0.5.0\bin>config.bat
C:\Program Files\jdk1.7.0_79
C:\Program Files\jdk1.7.0_79
C:\Program Files\Java\jdk1.8.0_121
C:\Program Files\Java\jdk1.8.0_121

Amit

Posted 2017-05-23T14:52:42.467

Reputation: 11