I am trying to read the first line from a file and I am setting it as environment variable. Below is the variable I use
@echo off
if EXIST "test.dat" (
set JAVA_HOME_PATH=
set JAVA_PATH=
set /p JAVA_HOME_PATH=<test.dat
echo %JAVA_HOME_PATH%
set JAVA_PATH=%JAVA_HOME_PATH%\bin\java
echo %JAVA_PATH%
)
Assuming the test.dat contains the path to JDK and if it is c:\JDK1.6
on running it for the first time I get
ECHO is off.
ECHO is off.
on running again I get
c:\JDK1.6
\bin\java
and on running again I get
c:\JDK1.6
c:\JDK1.6\bin\java
I dint change the test.dat file. But why is this happening ? Only when I run for third time all the variables getting set ? Looks weird. Am I doing anything wrong in this ? Please help me out.
Your issue is one of Delayed Expansion of variables.
To fix it, simply change your script to include
SETLOCAL ENABLEDELAYEDEXPANSION
, and use!!
instead of%%
as so:Batch always replaces any %var% in any statement with its CURRENT value and THEN runs the statement. Your IF statement runs from the IF keyword to the closing-parenthesis.
On the first run, batch substitutes (nothing) for
Java_home_path
and forjava_path
so theECHO %java_home_path%
is interpreted as 'echo` and batch reports its ECHO status.HOWEVER,
java_home_path
is set toc:\JDK1.6
fromtest.dat
butJAVA_PATH
is set to(nothing)\bin\java
On the second run, these existing values are duly reported,
java_home_path
is set fromtest.dat
andJAVA_PATH
is set toc:\JDK1.6\bin\java
On the third run, you get the names you expect reported.
Cure: (1)
cure: (2)