【问题标题】:How to read from a .properties file using batch script如何使用批处理脚本从 .properties 文件中读取
【发布时间】:2011-12-04 06:04:41
【问题描述】:

我有一个要求,我想从 .properties 文件中读取值

我的属性文件test.properties 内容

file=jaguar8
extension=txt
path=c:\Program Files\AC

我需要从上面的文件中获取jaguar= 之后的任何内容

请帮助我。谢谢

【问题讨论】:

    标签: windows batch-file


    【解决方案1】:

    你可以试试这个:

    @ECHO OFF
    @SETLOCAL
    FOR /F "tokens=1* delims==" %%A IN (test.properties) DO (
    ECHO %%A   =    %%B 
    )
    @ENDLOCAL
    

    【讨论】:

      【解决方案2】:

      我知道这是一个古老的帖子,但我想扩展 toschug 的出色答案。

      如果 .properties 文件中的路径将被定义为 %~dp0 或任何其他需要在使用前先扩展的变量,我建议按照以下方式进行:

      在 .properties 文件中:

      path=%~dp0
      

      在批处理文件中,您可以通过以下方式使用它(代码将在两个 for(s) 之间使用,定义一个 清理一个):

      if "!test.path!" NEQ "" (
         echo Not expanded path: !test.path!
         call :expand !test.path! test.path
      )
      
      echo test.path expanded: !test.path!
      pause
      
      :expand
      SET "%~2=%~1"
      GOTO :EOF
      

      不要忘记使用(在批处理文件的开头):

      SETLOCAL ENABLEDELAYEDEXPANSION
      

      【讨论】:

        【解决方案3】:
        For /F "tokens=1* delims==" %%A IN (test.properties) DO (
            IF "%%A"=="file" set file=%%B
        )
        
        echo "%file%"
        

        希望这会有所帮助

        【讨论】:

          【解决方案4】:

          支持 cmets (# style) 的解决方案。解释见代码中的 cmets。

          test.properties:

          # some comment with = char, empty line below
          
          #invalid.property=1
          some.property=2
          some.property=3
          # not sure if this is supported by .properties syntax
          text=asd=f
          

          properties-read.bat:

          @echo off
          
          rem eol stops comments from being parsed
          rem otherwise split lines at the = char into two tokens
          for /F "eol=# delims== tokens=1,*" %%a in (test.properties) do (
          
              rem proper lines have both a and b set
              rem if okay, assign property to some kind of namespace
              rem so some.property becomes test.some.property in batch-land
              if NOT "%%a"=="" if NOT "%%b"=="" set test.%%a=%%b
          )
          
          rem debug namespace test.
          set test.
          
          rem do something useful with your vars
          
          rem cleanup namespace test.
          rem nul redirection stops error output if no test. var is set
          for /F "tokens=1 delims==" %%v in ('set test. 2^>nul') do (
              set %%v=
          )
          

          set test. 的输出(见上文):

          test.some.property=3
          test.text=asd=f
          

          最重要的部分是:

          • 带有eoldelims 选项的for 循环和
          • if-检查变量%%a%%b 是否已设置。

          你在for-loop 中对变量和它的值做什么当然取决于你——分配一些前缀变量只是一个例子。命名空间方法避免了任何其他全局变量被覆盖。 例如,如果您在 .properties 文件中定义了类似 appdata 的内容。

          我正在使用它来摆脱额外的 config.bat,而是为 java 应用程序和一些支持批处理文件使用一个 .properties 文件。

          对我有用,但这里肯定不会涵盖所有边缘情况,因此欢迎改进!

          【讨论】:

            【解决方案5】:
            @echo off  
            FOR /F "tokens=1,2 delims==" %%G IN (test.properties) DO (set %%G=%%H)  
            echo %file%  
            echo %extension%  
            echo %path%
            

            请注意,%%H 后面没有空格。否则,这会导致在文件路径中附加一个空格,并且当属性文件中的变量用作文件路径的一部分时,将导致文件未找到错误。为此奋斗了几个小时!

            【讨论】:

            • 它不会考虑 cmets; +1 解决方案。
            【解决方案6】:

            试试这个

            echo off
            setlocal
            FOR /F "tokens=3,* delims=.=" %%G IN (test.properties) DO ( set %%G=%%H )
            
            rem now use below vars
            if "%%G"=="file"
             set lfile=%%H
            if "%%G"=="path"
             set lpath=%%H
            if "%%G"=="extension"
             set lextention=%%H
            echo %path%
            
            endlocal
            

            【讨论】:

              猜你喜欢
              • 2015-11-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-06-15
              • 2011-02-21
              相关资源
              最近更新 更多