将属性文件中的值导入批处理文件

时间:2018-01-24 05:02:39

标签: batch-file

@echo off
SETLOCAL
For /F "tokens=1* delims==" %%A IN (build.properties) DO (
    IF "%%A"=="projectPath" set lprojectPath=%%B
    IF "%%A"=="warSourcePath" set lwarSourcePath=%%B
    IF "%%A"=="warDestinationPath" set lwarDestinationPath=%%B
    IF "%%A"=="serverPath" set lserverPath=%%B
)

echo "%lprojectPath%"
echo "%lwarSourcePath%"
echo "%lwarDestinationPath%"
echo "%lserverPath%"

我有一个名为build.properties(key = value)的属性文件。当我运行上面的bat文件时,echo print"" cmd中的空字符串,我无法使用bat文件中的其他变量值。我在从属性文件中检索值时犯了任何错误。

projectPath = D:\DEV_R4.6_bat_test\brsint-web\brisnt-scheduling
warSourcePath = D:\DEV_R4.6_bat_test\brsint-web\brsint-webapp\target\Bristow.war
warDestinationPath = D:\apache-tomcat-7.0_1.2Latest (2)\webapps
serverPath = D:\apache-tomcat-7.0_1.2Latest (2)\bin

这是我的属性文件。

2 个答案:

答案 0 :(得分:1)

在我使用的name=value .ini的类似设置中,

FOR /f %%a IN (db.ini) DO SET %%a

%%aSET命令之后展开为name=value,从而创建变量。例如; SET name=value

如果" l"字符是必不可少的" l"在参数之前:

FOR /f %%a IN (build.properties) DO SET l%%a

答案 1 :(得分:0)

呃 - 你的build.properties中有空格。空格对set命令至关重要。使用:

@echo off
setlocal 
for /f "tokens=1,* delims== " %%a in (build.properties) do set "l%%a=%%b"
REM note the space in delims
set l

您的原始代码使用%projectPath %

等内容定义<SPACE>D:\DEV_R4.6_bat_test\brsint-web\brisnt-scheduling等变量(请注意空格)