从Windows批处理文件中读取环境变量(cmd.exe) [英] Read environment variables from file in Windows Batch (cmd.exe)
问题描述
我正在尝试从批处理文件中读取变量,以供以后在批处理脚本中使用,这是Java启动程序。我理想地希望在所有平台(Unix,Windows)上具有相同的设置文件格式,并且也是一个有效的Java Properties文件。也就是说,它应该是这样的:
I'm trying to read variables from a batch file for later use in the batch script, which is a Java launcher. I'd ideally like to have the same format for the settings file on all platforms (Unix, Windows), and also be a valid Java Properties file. That is, it should look like this:
setting1=Value1
setting2=Value2
...
是否可以像读取Unix shell脚本一样读取这些值?应该看起来像这样:
Is it possible to read such values like you would in a Unix shell script? The could should look something like this:
READ settingsfile.xy
java -Dsetting1=%setting1% ...
我知道这可能用 SET setting1 = Value1
,但我真的希望所有平台上的设置具有相同的文件格式。
I know that this is probably possible with SET setting1=Value1
, but I'd really rather have the same file format for the settings on all platforms.
要澄清:我需要在命令行中执行此操作/ batch环境,因为我还需要设置不能在JVM中改变的参数,例如-Xmx或-classpath。
To clarify: I need to do this in the command line/batch environment as I also need to set parameters that cannot be altered from within the JVM, like -Xmx or -classpath.
推荐答案
您可以在批处理文件中执行以下操作:
You can do this in a batch file as follows:
setlocal
FOR /F "tokens=*" %%i in ('type Settings.txt') do SET %%i
java -Dsetting1=%setting1% ...
endlocal
这读取包含SETTING1 = VALUE1等字符串的文本文件,并调用SET将其设置为环境变量。
This reads a text file containing strings like "SETTING1=VALUE1" and calls SET to set them as environment variables.
setlocal / endlocal用于将环境变量的范围限制为执行批处理文件。
setlocal/endlocal are used to limit the scope of the environment variables to the execution of your batch file.
CMD命令处理器实际上相当强大,而是拜占庭语法。
The CMD Command Processor is actually quite powerful, though with a rather byzantine syntax.
这篇关于从Windows批处理文件中读取环境变量(cmd.exe)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!