Parsing a .ini file

2019-03-01 19:35发布

问题:

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
)