批处理脚本 - 从文件中读取行时忽略哈希 [英] Batch script - Ignore hash when reading lines from a file
问题描述
我想设置从名称值对变量的属性。属性文件有#征求意见
I would like to set variables from name value pairs in a properties. The property file has # for comments
====示例输入文件=====
====sample input file=====
#This is a comment
### Another comment
appNames=HelloWorldApp
targetServer=serverABC
DEV.key1=value1
TEST.key2=value2
=========
我有2个问题
========= I have 2 issues
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
变量的设置,但我想被忽略的意见。我想用code一行做到这一点,但得到这个代替。
Variables are set, but I would like comments to be ignored. I would like to do this with a single line of code, but get this instead.
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的文件的批量script.Contents
2) I would like this properties file to be passed as an argument to a batch script.Contents of the test.bat file
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.
如果我同时运行该文件中的一行的内容,它的工作原理。
我究竟做错了什么?
在此先感谢
If I run the contents of this file one line at a time , it works. What am I doing wrong? Thanks in advance
推荐答案
要忽略开始用哈希行,你可以使用 EOL
参数的循环。< BR> FOR / FEOL =#delims =%i的(test.properties)做@set%I
To ignore lines starting with a hash, you could use the eol
parameter of the for loop.
for /f "eol=# delims=" %i in (test.properties) do @set %i
第二个问题是翻番批处理文件内的百分比。
The second issue is to double the percent inside of a batch file.
set propFilePathAndName=%1
for /f "delims=" %%i in (%propFilePathAndName%) do echo %%i
这是由分析器引起的,也有命令行和批处理文件不同的规则。
This is caused by the parser, there are different rules for the command line and batch files.
CMD行结果在(XYZ)也呼应%%一个一个
批处理文件结果为%%一中(XYZ)也呼应%% A
这篇关于批处理脚本 - 从文件中读取行时忽略哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!