windows 如何使用批处理脚本从 .properties 文件中读取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7708681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read from a .properties file using batch script
提问by user1960314
I have a requirement where i'd like to read values from a .properties file
我有一个要求,我想从 .properties 文件中读取值
my properties file test.properties
content
我的属性文件test.properties
内容
file=jaguar8
extension=txt
path=c:\Program Files\AC
From the above file I need to fetch jaguar
or anything after =
从上面的文件中我需要获取jaguar
或之后的任何内容=
Please help me. Thanks
请帮我。谢谢
回答by LPunker
For /F "tokens=1* delims==" %%A IN (test.properties) DO (
IF "%%A"=="file" set file=%%B
)
echo "%file%"
hope this could help
希望这会有所帮助
回答by user1960314
@echo off
FOR /F "tokens=1,2 delims==" %%G IN (test.properties) DO (set %%G=%%H)
echo %file%
echo %extension%
echo %path%
Note that there is no space after %%H. Else this causes a space to be appended, to file paths for example, and will cause file not found errors when the variables from the property files are used as part of a file path.Struggled for hours because of this!
请注意,%%H 后没有空格。否则,这会导致附加一个空格,例如文件路径,并且当属性文件中的变量用作文件路径的一部分时,将导致文件未找到错误。为此挣扎了几个小时!
回答by toschug
A solution with support for comments (# style). See comments in code for explanation.
支持注释(# 样式)的解决方案。请参阅代码中的注释以获取解释。
test.properties:
测试属性:
# some comment with = char, empty line below
#invalid.property=1
some.property=2
some.property=3
# not sure if this is supported by .properties syntax
text=asd=f
properties-read.bat:
属性-read.bat:
@echo off
rem eol stops comments from being parsed
rem otherwise split lines at the = char into two tokens
for /F "eol=# delims== tokens=1,*" %%a in (test.properties) do (
rem proper lines have both a and b set
rem if okay, assign property to some kind of namespace
rem so some.property becomes test.some.property in batch-land
if NOT "%%a"=="" if NOT "%%b"=="" set test.%%a=%%b
)
rem debug namespace test.
set test.
rem do something useful with your vars
rem cleanup namespace test.
rem nul redirection stops error output if no test. var is set
for /F "tokens=1 delims==" %%v in ('set test. 2^>nul') do (
set %%v=
)
output from set test.
(see above):
输出set test.
(见上文):
test.some.property=3
test.text=asd=f
The most important parts are:
最重要的部分是:
- the
for
-loop with theeol
anddelims
option and - the
if
-checks that both variables%%a
and%%b
are set.
for
带有eol
anddelims
选项和的-loop- -
if
检查变量%%a
和%%b
是否已设置。
What you do in the for
-loop with the variable and its value is certainly up to you - assigning to some prefixed variables was just an example. The namespacing approach avoids that any other global variable gets overridden.
For example if you have something like appdata
defined in your .properties file.
你在for
-loop 中对变量和它的值所做的当然取决于你 - 分配给一些前缀变量只是一个例子。命名空间方法避免了任何其他全局变量被覆盖。例如,如果您appdata
在 .properties 文件中定义了类似的内容。
I'm using this to get rid of an extra config.bat and instead using one .properties file for both the java app and some support batch files.
我正在使用它来摆脱额外的 config.bat,而是为 java 应用程序和一些支持批处理文件使用一个 .properties 文件。
Works for me, but certainly not every edge case is covered here, so improvements welcome!
对我有用,但肯定不是每个边缘情况都在这里涵盖,所以欢迎改进!
回答by AabinGunz
Try this
尝试这个
echo off
setlocal
FOR /F "tokens=3,* delims=.=" %%G IN (test.properties) DO ( set %%G=%%H )
rem now use below vars
if "%%G"=="file"
set lfile=%%H
if "%%G"=="path"
set lpath=%%H
if "%%G"=="extension"
set lextention=%%H
echo %path%
endlocal
回答by tukan
I know this is ancient post but I would like to expand on toschug's great answer.
我知道这是一个古老的帖子,但我想扩展 toschug 的精彩答案。
If the path in the .properties file would be defined as %~dp0 or any other variable that needs to be expanded first before using it, I recommend doing it the following way:
In the .properties file:
如果 .properties 文件中的路径被定义为 %~dp0 或任何其他需要在使用前先扩展的变量,我建议按以下方式进行:
在 .properties 文件中:
path=%~dp0
In the batch file you can then use it the following way (the code is to be used between the two for(s) defining one <=> cleanup one):
在批处理文件中,您可以按以下方式使用它(代码将在定义一个 <=> 清理一个的两个 for(s) 之间使用):
if "!test.path!" NEQ "" (
echo Not expanded path: !test.path!
call :expand !test.path! test.path
)
echo test.path expanded: !test.path!
pause
:expand
SET "%~2=%~1"
GOTO :EOF
Don't forget to use (at the start of the batch file):
不要忘记使用(在批处理文件的开头):
SETLOCAL ENABLEDELAYEDEXPANSION
回答by moon
you can try this:
你可以试试这个:
@ECHO OFF
@SETLOCAL
FOR /F "tokens=1* delims==" %%A IN (test.properties) DO (
ECHO %%A = %%B
)
@ENDLOCAL