批处理脚本 - 从文件中读取行时忽略哈希

时间:2011-08-05 21:30:12

标签: batch-file

我想在属性中设置名称值对的变量。属性文件具有#for comments

====示例输入文件=====

#This is a comment
### Another comment
appNames=HelloWorldApp
targetServer=serverABC

DEV.key1=value1
TEST.key2=value2

======  我有2个问题

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.    

如果我一次运行一行这个文件的内容,它就可以了。 我究竟做错了什么? 提前致谢

1 个答案:

答案 0 :(得分:6)

要忽略以哈希开头的行,您可以使用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

这是由解析器引起的,命令行和批处理文件有不同的规则。

CMD-线
for %a in (xyz) do echo %a

批处理文件
for %%a in (xyz) do echo %%a