I am pretty new to registry and batch files editing. I want to replace path C:\PROGRA~2\Java\jre8\bin inside my batch file to a more generic path so that each time I don't have to modify the path and it automatically takes from the registry.

key path = HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
inside key i have 
name = JavaHome
data = C:\Program Files\Java\jre8

Can someone help how to do it, or tell any reference documents to read?


[test.bat] file body

Batch file code which queries registry, gets parameter value into environment variable, then use this value for to get directory listing. Tested.

for /f "tokens=2*" %%a in ('HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment" /v JavaHomePath') do set JavaHomePath=%%b
dir "%JavaHomePath%"

How does it works?

We use REG command-line utility for to obtain registry key value. According command is

REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment" /v JavaHome 

Its output then will be parsed by FOR /F command. The command output looks like

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
    JavaHome    REG_SZ    C:\Program Files\Java\jre8

When not specified then <space/tab> is used as a default delimiter. So 1st token is Keyname, 2nd is KeyType and 3th (and next, if value contains spaces) is KeyValue.

We specify tokens=2*. This (digit 2) means that we want to assign 3rd token to specified variable (%%a) and (asterisk after the digit) all the tail to the auto-created variable with the name of the next (by alphabet) letter (i.e. %%b). So KeyValue, which is the tail after 2nd token and delimiter, will be placed into %%b variable.

And then we execute set command and assing this value to environment variable.

The REG QUERY produces 2 lines. FOR command porcesses each of them. So JavaHomePath environment variable is set/overrided each time the next line is processed, and the result of the last line parsing is stored in the variable after all lines processed and FOR execution finished. You can update the code and pipe REG QUERY output to FIND to get only one line.

PS. Yesterday I had used "tokens=3*" in the answer. Today I test it on another workstation with another OS language - "tokens=3*" works wrong and "tokens=2*" works correct. It is strange for me, I don't understand why (I have been too inattentive? maybe...), but it is a fact... Now the code above works correctly from a batch file.

So test your final code carefully.

  • This works just fine, but some explanation what it does would be more helpful than a quick one-liner. FOR /F with TOKENS is far from obvious for a batch file beginner.
    – Tonny
    Sep 11 '19 at 13:31
  • Good job! You have my up vote.
    – Tonny
    Sep 11 '19 at 14:29
  • Thanks a lot Akina for very quick response.
    – yash
    Sep 12 '19 at 5:14
  • But i had some doubt. so should i use JavaHomePath (since JavaHomePath=%b) inplace of C:\PROGRA~2\Java\jre8\bin" in inside my batch file ?
    – yash
    Sep 12 '19 at 5:22
  • @yash Yes. Do not forget to wrap variable name with percent signs %JavaHomePath%, this will tell CMD that variable value must be used instead of a literal. Additionally - add SetLocal in front of yur batch or JavaHomePath= as a last line of it for to remove the variable which is not necessary outside the batch.
    – Akina
    Sep 12 '19 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.