2011-08-05 13 views
2

プロパティで名前の値の組から変数を設定したいと思います。プロパティファイルには、バッチスクリプト - ファイルから行を読み込むときにハッシュを無視する

====サンプル入力ファイル=====

#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 

変数が設定されているが、私はコメントは無視されたいです。コードの1行でこれをやりたいのですが、代わりにこれを取得してください。私は、このプロパティはTEST.BATファイルのバッチscript.Contentsに引数として渡されるファイルたい

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)

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行ずつ実行します。 私は何が間違っていますか? ありがとうございます

答えて

6

ハッシュで始まる行を無視するには、forループのeolパラメータを使用できます。
for /f "eol=# delims=" %i in (test.properties) do @set %i

2番目の問題は、バッチファイル内のパーセントを倍増することです。

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

+0

とてもうまくいきました。ありがとうございました! – chintu

関連する問題