用于设置变量的通用批处理文件 [英] A common batch file to setup variables

查看:65
本文介绍了用于设置变量的通用批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理多个批处理文件,并且希望它们共享一些变量,所以我创建了一个具有所有这些设置SetupEnv的批处理文件:

I am working on multiple batch files and I want them to share some variables, so I created a batch file that has all these setups SetupEnv:

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's 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'm calling that file first:

::setup
call ../SetupEnv

echo %directory% dir
pause;

问题在于,即使文件运行平稳并且我在输出中看到正在设置内容,变量也不会传递给我从中调用它的文件.因此在该示例中,未打印%directory%.

The problem is that even though the file runs smoothly and I can see in the outputs that things are being setup, the variables are not coming across to the file I'm calling it from. So in that example %directory% is not being printed.

编辑 我还尝试使用乔伊的答案:

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

echo %directory% dir
pause

但是那也不起作用,并且%directory%没有打印出来

But that didn't work either and %directory% didn't get printed

推荐答案

call ed批处理文件中设置变量是可行的,只要您在被调用的批处理文件中不使用setlocal(将存在隐式endlocal,当它返回时,变量将丢失):

setting variables in a called batchfile works, as long as you don't use setlocal in the called batchfile (there will be an implicite endlocal, when it returns, so the variables would get 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解决方案,我将其略微更改为:

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

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

这只会执行"以set开头的行(忽略大小写),因此您可以将任何注释保留在该文件中.

This will only "execute" lines, that start with set (ignoring capitalization), so you can keep any comments inside that file.

注意:... do set "%%a"在该行中添加了另一个set(您已经在文件中添加了该行),结果是set "set var=value",您显然不希望这样做.

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

这篇关于用于设置变量的通用批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆