读取INI文件并将其批量设置为全局变量 [英] Reading an INI file and setting them as global variables in batch

查看:182
本文介绍了读取INI文件并将其批量设置为全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,该文件已经做了很多事情,并且一直在尝试对其进行扩展以从ini文件中获取一些数据.

I have a batch file that does a bunch f things already and been trying to expand it to get some data from an ini file.

例如,ini文件看起来像这样

The ini file for example looks like this

[Settings1]
Text=Text
Text1=Text
Text2=Text

[Settings2]
Text=Text
Text1=Text
Text2=Text

我想出了一种方法来获取下一批所需的部分

I have figured out a way to get the section I require with the following batch

@echo off

setlocal EnableDelayedExpansion
set "file=settings.ini"
set "section=[Settings1]"

set flag=0
for /f "usebackq delims=" %%# in ("%file%") do (
    set line=%%#
    ::trim
    for /f "tokens=* delims= " %%a in ("!line!") do set "line=%%a"
    set f=!line:~0,1!
    if "!f!" neq ";" (
        if !flag! equ 1 (
            ::for /f "tokens=1* delims==" %%a in ("!line!") do (
            for /f "tokens=1* delims==" %%a in ("%%#") do (
                set "!section!.%%a=%%b"

            )
        )

        if "!f!" equ "[" (
            if "!line!" equ "%section%" (
                set flag=1
            ) else (
                set flag=0
            )
        )       
    )
)

set %section%

然后输出以下内容

Settings1.Text=Text
Settings1.Text1=Text
Settings1.Text2=Text

我想做但不能弄清楚该怎么做的是接受这些输出中的每一个并分配值,因此将等号后的文本"仅分配给其自己的变量,然后可以在以后调用在我的脚本中

What I want to be able to do but cannot work out how to do is to take in each of these outputs and assign the value so just the 'Text' after the equals sign to its own variable that can then be recalled later in my script

推荐答案

看看下面的示例.我使用了您现有的代码,即使可以改进它.您可以看到我如何利用for /l循环为您提供可以做什么的想法.您可以调整形状以适合您的期望结果.您也可以针对不同的部分考虑相同的解决方案.

Have a look at the below example. I used your existing code, even though it can be improved. You can see how I utilized the for /l loop to give you an idea of what can be done. You can shape this to fit your desired result. You can consider the same solution for different sections as well.

@echo off
setlocal enabledelayedexpansion
set "file=settings.ini"
set "section=[Settings1]"
set num=0
set flag=0
for /f "usebackq delims=" %%# in ("%file%") do (
    set line=%%#
    ::trim
    for /f "tokens=* delims= " %%a in ("!line!") do set "line=%%a"
    set f=!line:~0,1!
    if "!f!" neq ";" (
        if !flag! equ 1 (
            ::for /f "tokens=1* delims==" %%a in ("!line!") do (
            for /f "tokens=1* delims==" %%a in ("%%#") do (
                set "!section!.%%a=%%b"
                set /a num+=1

            )
        )

        if "!f!" equ "[" (
            if "!line!" equ "%section%" (
                set flag=1
            ) else (
                set flag=0
            )
        )       
    )
)

for /l %%a in (0,1,%num%) do (
  if %%a equ 0 (
     if defined [Settings1].Text echo(%[Settings1].Text%
   ) else (
     if defined [Settings1].Text%%a echo(![Settings1].Text%%a!
  )
)

这篇关于读取INI文件并将其批量设置为全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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