It really depends on what you are doing - and you sent the full script.
First you have unbalanced % in %test.dat
Next, it's nice to name the batch files .bat not .dat
Further, if the sole purpose of this getVariables.bat is to set the fom variables of the string file (test.dat), for example
JAVA_HOME=c:\whereverjavahomeis JAVA=c:\whereverjavais
then
@echo off if EXIST "test.dat" ( for /F "tokens=*" %%I in (test.dat) do set %%I )
quite adequate. That it is 4 lines (and all this can be compressed to 1 if you really try ...)
The point is that you only need enabledelayedexpansion and therefore setlocal to display the value of the variables you change WITHIN THE LOOP WHERE YOU'RE CHANGING THE VALUES . You will eventually delete these lines, and enabledelayedexpansion will lose its meaning.
For testing, you can write
@echo off echo before...JAVA=%java% echo before...JAVA_HOME=%java_home% if EXIST "test.dat" ( for /F "tokens=*" %%I in (test.dat) do set %%I ) echo after....JAVA=%java% echo after....JAVA_HOME=%java_home%
or even
@echo off echo before&set java if EXIST "test.dat" ( for /F "tokens=*" %%I in (test.dat) do set %%I ) echo after&set java
In fact, if getVariables.bat only ever been CALL ed, then even the @echo off line is redundant - if you @echo off from the calling party.
Magoo source share