Common batch file for setting variables

I am working on several batch files and I want them to share some variables, so I created a batch file with all these SetupEnv settings:

 rem General setup :: To pause or not after running a batch file SET isPause = true :: The directory where your source code is located SET directory = D :: The folders where your primary & secondary source code is located :: I like to have two source code folders, if you don't then just have them pointing to the same folder SET primary_source_code = \Dev\App SET secondary_source_code = \Dev\App2 :::::::::::::::::::::::::::::::::::::::::::: XAMPP ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: rem If you're using XAMPP then set these up :: Your destination folder SET base_destination = C:\xampp\htdocs :: The base url that is pointing to your destination folder (in most cases it localhost) SET base_url = http://10.0.2.65 :::::::::::::::::::::::::::::::::::::::::: Angular ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: rem If you're using angular set these up :: The folder where you built code is copied SET build_file = dist 

And from another batch file, I first call this file:

 ::setup call ../SetupEnv echo %directory% dir pause; 

The problem is that even though the file runs smoothly, and I can see on the outputs that things are being configured, the variables do not get into the file from which I call it. Therefore, in this example, %directory% not printed.

EDIT I also tried using Joey's answer :

 ::setup for /f "delims=" %%x in (../SetupEnv.txt) do (set "%%x") echo %directory% dir pause 

But that didn't work either, and %directory% did not print

0
batch-file
Sep 11 '17 at 2:33 on
source share
1 answer

the settings in call ed batchfile work if you do not use setlocal in the called batch file (there will be implicite endlocal when it returns, so the variables will be lost)

 > type a.bat set var=old echo %var% call b.bat echo %var% > type b.bat set var=new > a.bat > set var=old > echo old old > call b.bat > set var=new > echo new new > 

for an alternative for solution, I would slightly change it to:

 for /f "delims=" %%a in ('type b.bat^|findstr /bic:"set "') do %%a 

This will only β€œexecute” the lines starting with set (ignoring the uppercase letters), so you can store comments inside this file.

Note: ... do set "%%a" adds another set to the line (you already have one file), resulting in set "set var=value" , which you obviously do not need.

0
Sep 11 '17 at 6:02 on
source share



All Articles