我想根据属性中的名称值对设置变量。属性文件有 # 用于注释
====示例输入文件=====
#This is a comment
### Another comment
appNames=HelloWorldApp
targetServer=serverABC
DEV.key1=value1
TEST.key2=value2
========= 我有两个问题
1)
C:\temp\dos>for /f "delims=" %i in (test.properties) do @set %i
Environment variable #This is a not defined
Environment variable ### Another not defined
变量已设置,但我希望忽略注释。我想用一行代码来做到这一点,但得到这个。
for /f "delims=" %i in (test.properties) do @echo %i | find "#">nul || @set %i
find: unable to access "#": The system cannot find the file specified.
The process tried to write to a nonexistent pipe.
Environment variable #This is a not defined
find: unable to access "#": The system cannot find the file specified.
The process tried to write to a nonexistent pipe.
2) 我希望将此属性文件作为参数传递给批处理脚本。test.bat 文件的内容
set propFilePathAndName=%1
for /f "delims=" %i in (%propFilePathAndName%) do echo %i
输出
C:\temp\dos>set propFilePathAndName=/temp/dos/test.properties
propFilePathAndNamei was unexpected at this time.
如果我一次一行地运行这个文件的内容,它就可以工作。 我究竟做错了什么? 提前致谢
最佳答案
要忽略以散列开头的行,您可以使用 for 循环的 eol
参数。
for/f "eol=# delims="%i in (test.properties) do @set %i
第二个问题是将批处理文件中的百分比加倍。
set propFilePathAndName=%1
for /f "delims=" %%i in (%propFilePathAndName%) do echo %%i
这是由解析器引起的,命令行和批处理文件有不同的规则。
命令行
for %a in (xyz) do echo %a
批处理文件
for %%a in (xyz) do echo %%a
关于batch-file - 批处理脚本 - 从文件中读取行时忽略哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6962846/