INI文件逐行获取部分 [英] INI file get section line by line

查看:119
本文介绍了INI文件逐行获取部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ini 文件:

 [section1] 
line1
line2
[section2]
line3
line4

我想读取行, code> [section1] 例如。
我只需要将 line1 line2 作为字符串。
现在正在运行:

  SET var = lines.txt 
FOR / Ftokens = * %% a in(%var%)DO(
CALL script.cmd %% a

这是一个批处理文件,但我找不到一个解决方案。
每当我想使用 section2 的内容时,我需要使用 lines2.txt ,但是现在我合并在一起( ini 文件上面)。

解决方案

标志切换动作(在找到起始标题时设置标志,取消设置,当下一个标题开始时):

  @ echo off 
set var = test.ini
setflag =
FOR / Ftokens = *%% a in(%var%)DO(
flag(
echo %% a | find[> null&& setflag =||(
echo call script.cmd with parameter %% a

)else(
if%% a==[section1]set flag = 1


pre>

I have an ini file like this:

[section1]
line1
line2
[section2]
line3
line4

I want to read lines, but only from [section1] for example. I need only the line1 and line2 as string. Now it is running:

SET var=lines.txt
FOR /F "tokens=*" %%a in (%var%) DO (
    CALL script.cmd %%a
)

It is a batch file, but I can't find a solution. Every time when I want to use contents from section2, I need to use lines2.txt, but now I merged together (ini file above).

解决方案

use a flag to toggle action (set the flag when the starting header is found, unset it, when the next header starts):

@echo off
set var=test.ini
set "flag="
FOR /F "tokens=*" %%a in (%var%) DO (
  if defined flag (
     echo %%a|find "[" >null && set "flag=" || (
       echo calling script.cmd with parameter %%a
     )
  ) else (
    if "%%a" == "[section1]" set flag=1
  )
)

这篇关于INI文件逐行获取部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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