简体   繁体   中英

Batch script make setlocal variable accessed by other batch files

I am writing a batch file to read a path from file and set it to environment variable. This batch file will be used (called) by many other batch files to get the variable. While writing the batch file I faced a problem will variable expansion so I used SETLOCAL ENABLEDELAYEDEXPANSION to overcome this issue. But doing so the other batch files which is using it are not able to get the variables set.

Below is the batch script,

getVariables.bat

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
if EXIST "test.dat" (
    for /F "tokens=*" %%I in (test.dat) do set %%I
    echo setting JAVA_HOME to :: !JAVA_HOME!
    echo setting JAVA to !JAVA!
)

In my another batch file I am using the above batach file to get the variables set

   another.bat

   call getVariables.dat
   echo "%JAVA%"

But echo is printing "", where it is not set. If this is because of setlocal , how can I overcome this ? I also need setlocal for delaying the expansion and to occur at execution time. Any solution for this issue ? Please help me out.

To set multiple global variables to the value of local variables, use the following trick:

endlocal & (
  set "globalvar1=%localvar1%"
  set "globalvar2=%localvar2%"
  set "globalvar3=%localvar3%"
)

The variables in the ( ) block are expanded before endlocal is executed.

这个endlocal & set str=value将创造奇迹。

It really depends on quite what you're doing - and whether you've posted the full script.

First, you have unbalanced % in %test.dat

Next, it's a good idea to name batch files .bat not .dat

Next, if the only purpose of this getVariables.bat is to set variables fom a file of lines (test.dat) like

JAVA_HOME=c:\whereverjavahomeis
JAVA=c:\whereverjavais

then

@echo off
if EXIST "test.dat" (
    for /F "tokens=*" %%I in (test.dat) do set %%I
)

is perfectly adequate. That's it - 4 lines (and it can all be condensed to 1 if you really try...)

The point is that you only need the enabledelayedexpansion and hence the setlocal in order to display the value of the variables you're changing WITHIN THE LOOP WHERE YOU'RE CHANGING THE VALUES . You'll eventually remove those lines, and the enabledelayedexpansion loses its raison d'etre .

For testing, you could 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 is only ever CALL ed then even the @echo off line is redundant - assuming you've executed @echo off from the calling batch.

go into cmd and search setx. it lets you make system variables and all other kinds of things like even finding the x and y coordinates of a variable in a file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM