简体   繁体   中英

Shared configuration for multiple batch scripts

Is it possible to have a batch file with some common variables and then include this script in other scripts so that the variables are visible there?

What is a common approach to this?

Try this (in the first script):

set>variables.txt

.. and in the second script:

for /f "delims=" %%i in (variables.txt) do set %%i

Edit: To do this for a set of variables (first file):

::define some variables
set "_myvar1=value1"
set "_myvar2=value2"
set "_myvar3=value3"
::save them to a text file
set _myvar>variables.txt

This saves all variables heading _myvar to the text file. The code for the second script doesn't change.

Simply, within the scripts that need to see the "shared" values

CALL yourbatchthatsetsthecommonvariables.bat

BUT changes to those variables will be isolated to the specific CMD instance - they won't be communicated between instances.

AND the values returned for any instance by this COMMON.BAT would be those relevant to the current instance.

So: Suppose you have COMMON.BAT

set mytime=%time%
set myval=abc123

Then instance 1... or instance 2

...
call COMMON.BAT
...

would set myval to abc123 but mytime to the time that each called COMMON.BAT

Absolutely, and this is pretty common too.

Let's say your variables in config.bat :

set PATH=c:\opt\tools\jdk1.6;%PATH%
set CLASSPATH=c:\opt\projects\myproject\jars;%CLASSPATH%

Then you can have for example start-dev.bat :

call common.bat
javaw -cp %CLASSPATH% -Denv=dev

And start-prod.bat :

call common.bat
javaw -cp %CLASSPATH% -Denv=prod

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