批处理文件:如何仅读取INI文件中的节 [英] Batch File: How to read only sections in a INI file

查看:116
本文介绍了批处理文件:如何仅读取INI文件中的节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是批处理脚本的新手,我正在尝试根据调用批处理文件时传递的参数编写一个简单的脚本,以便从INI文件读取.

I am very much a novice at Batch Scripting and i'm trying to write a simple script to READ from an INI file based on the Parameters that is passed when the batch file is called.

这是INI文件外观的一个示例:

This is an example for what the INI file would look like:

[SETTING1]
Value1=Key1
Value2=Key2
Value3=Key3

[SETTING2]
Value1=Key1
Value2=Key2
Value3=Key3

[SETTING3]
Value1=Key1
Value2=Key2
Value3=Key3

仅阅读被调用的部分时,我遇到了问题.它将从与值"和键"匹配的任何部分读取,而且我不知道如何限制它,仅读取具有设置的部分.

I am running into a problem when it comes to reading ONLY the section that is called. It will read from any section that matches the "Value" and "Key" and i don't know how to limit it to only read the section with the settings.

使用以下参数调用文件:run.bat run.ini setting2.我下面的代码是到目前为止的内容,感觉好像我已经正式碰壁了.有人能帮忙吗?任何帮助将不胜感激.

The file is being called with this Parameter: run.bat run.ini setting2. My code below is what I have so far and I feel as if i have officially hit a wall. Can anyone help with this? Any help would greatly be appreciated.

    @ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET INIFile="%~f1"

for /f "usebackq delims=" %%a in (!INIFile!) do (
        if %%a==[%2] (
        SET yesMatch=%%a
            for /f "tokens=1,2 delims==" %%a in (!yesMatch!) do (
                if %%a==Value1 set Key1=%%b
                if %%a==Value2 set Key2=%%b
                if %%a==Value3 set Key3=%%b
)
        ECHO !yesMatch!
        ECHO !Key1!
        ECHO !Key2!
        ECHO !Key3!
        pause
)
)

pause
exit /b

推荐答案

@ECHO OFF
SETLOCAL
SET INIFile="%~f1"
SET "FLAG="

for /f "usebackq tokens=1,*eol=|delims==" %%a in (%INIFile%) do (
 IF "%%b"=="" (
  REM No "=" so section
  IF /i "%%a"=="[%2]" (SET flag=Y) ELSE (SET "flag=")
 ) ELSE IF defined flag (
  REM data line - only if FLAG is defined
  REM set values defined
  SET "%%a=%%b"
  REM pick particular values
  if /i "%%a"=="Value1" set "Key1=%%b"
  if /i "%%a"=="Value2" set "Key2=%%b"
  if /i "%%a"=="Value3" set "Key3=%%b"
 )

)
SET key
SET val

GOTO :EOF

这是获取价值观的一种方式.

Here's a way to get your values.

文件中的数据为[section]name=value,因此将delims设置为=会将仅节"分配给%%a或将 name 分配给%%a%%b.

The data in the file is either [section] or name=value so settling delims to = will assign either section-only to %%a or name to %%a and value to %%b.

仅在遇到其相应部分后才设置(定义)该标志,并在下一部分将其清除.仅定义了该任务.

The flag is only set (defined) after its appropriate section is encountered, and cleared on the next section. Only of it is defined will the assignment take place.

简单的set %%a=%%b的优点在于,它可以设置该部分中定义的任何值-如果添加了新值,则无需更改代码.原始版本的优点是可以选择特定的值并仅设置这些值.您付钱,您可以选择.

The advantage of the simple set %%a=%%b is that it results in setting whatever values are defined in the section - no changes to the code need to take place if new values are added. Your original version has the advantage of picking particular values and setting only those. You pays your money, you takes your choice.

请注意,if语句上的/i开关使比较不区分大小写.

Note that the /i switch on an if statement makes the comparison case-insensitive.

请注意,set "value=string"的使用还可以确保分配的值中不包括行尾的空格.

Nota also the use of set "value=string" which ensures that trailing spaces on a line are not included in the value assigned.

edit:默认情况下,end of line字符为;,因此for/f会忽略以;开头的任何行.结果是;-commented-out部分的值将覆盖为上一部分设置的值.

edit : By default, the end of line character is ; so any line starting ; is ignored by for/f. The consequence is that the values for the ;-commented-out section would override the values set for the previous section.

eol设置为|应该可以解决该问题. eol设置为什么都没有关系.这只是一个字符,可能不会出现在INI文件的任何位置(否则该行将被截断.)

Setting eol to | should cure the problem. It really doesn't matter what eol is set to; it's exactly one character which may not appear anywhere in the INI-file (else that line would appear truncated.)

如有必要,可以将eol设置为control-Z,但是选择未使用的字符会更容易...

It is possible, if necessary, to set eol to control-Z but selecting an unused character is easier...

因此,单行更改-for /f选项中包含了eol参数.

Consequently, a one-line change - the eol parameter is included in the for /f options.

这篇关于批处理文件:如何仅读取INI文件中的节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆