using path from registry in a batch file

1

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?

yash

Posted 2019-09-11T12:36:44.913

Reputation: 13

Answers

2

[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.

SetLocal
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.

Akina

Posted 2019-09-11T12:36:44.913

Reputation: 2 991

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 – 2019-09-11T13:31:41.550

Good job! You have my up vote. – Tonny – 2019-09-11T14:29:36.530

Thanks a lot Akina for very quick response. – yash – 2019-09-12T05:14:06.293

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 – 2019-09-12T05:22:03.427

@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 – 2019-09-12T05:33:27.837

@Akina . while using for command above my path in batch file and when executing the build, i am getting error b is unexpected at this time – yash – 2019-09-12T05:55:25.953

@yash If you use the above code in batch file then the percent sign must be doubled (set JavaHomePath=%%b). – Akina – 2019-09-12T05:58:35.383

@Akina for /f "tokens=3*" %%a in ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Web Start\1.0.1" /v Home') do set JavaHomePath=%%b

if exist %JavaHomePath% goto SETVMPATH goto SETENVFORBUILD i am using this but still getting java is not recognized. please see the path in the question description – yash – 2019-09-12T07:01:57.777

@yash I have tested everything one more time. The answer edited - look and test. Enable echo (for whole batch or for some lines) and check that the variable value is correctly assigned and inserted into the commands. – Akina – 2019-09-12T07:40:55.043

@Akina yes the code is correct. however a little bug. The path in my batch file is PROGRA~2 (which is program filesx86 ), And the path we are referring is of Program files . i can't find that path in registry. is it possible to edit this variable javahomepath slightly to add x86 so that it looks C:\Program Files (x86)\Java\jre8 ? – yash – 2019-09-12T10:10:34.200

It is strange that the path in registry do not match the path in the filesystem... of course you can parse the variable using the same command FOR /F using \ as a delimiter and re-join it back adding this substring... but I'd recommend to check the correctness the registry data and, maybe, restore valid key value. – Akina – 2019-09-12T11:08:42.333