Parsing a .ini file

别来无恙 提交于 2019-12-02 13:12:10

问题


My question is about parsing an ini file with the Windows command line.

I got stuck while trying remove a section with all keys from the file. The name of this section is known and saved in a variable.

I tried to save the lines (start, end) for removing the stuff between but it didn't work for me.

Can anyone here help me?

edit:

Here is an example ini file:

[Example]
cycle = value
cycle2 = value
cycle_bu = value

[Example2]
key1 = value
key2 = value
key3 = value
key4 = value

[something3]
key1 = value
key2 = value
key3 = value
key4 = value
key5 = value
key6 = value

回答1:


http://www.robvanderwoude.com/sourcecode.php?src=readini_nt

Could you check if this will do the work?

EDIT: this is not tested:

break >new.ini
set skip_this_section=[something]
set skip_flag=0
for /f  %%I in (myini.ini) do (
   call :print_to_file %%I
)
goto :eif

:print_to_file
setlocal enabledelayedexpansion 
    set line=%1
    set first_char=!line:~0,1!
    if "!first_char!" EQU "[" (
        if "!line!" EQU "!skip_this_section!" (
           set skip_flag=1
        ) else (
           set skip_flag=0
        )
    )
endlocal & set skip_flag=%skip_flag%
if %skip_flag% EQU %0% (
   echo %1 >> new.ini
) 



回答2:


There is an CLI crudini to manipulate ini entries by group and key:

https://github.com/pixelb/crudini/blob/master/EXAMPLES



来源:https://stackoverflow.com/questions/13646498/parsing-a-ini-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!