1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Remove a section from an INI file

Discussion in 'Support' started by thedave, Nov 13, 2008.

  1. thedave

    Joined:
    Nov 13, 2008
    Messages:
    253
    Likes Received:
    2
    Howdy!

    I'm looking for a way to remove an entire section from an INI file including the header, not just the individual keys and settings contained within, can anyone tell me if that's possible using internal commands or do I need to parse the INI file myself?
     
  2. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    thedave wrote:
    |
    | I'm looking for a way to remove an entire section from an INI file
    | including the header, not just the individual keys and settings
    | contained within, can anyone tell me if that's possible using
    | internal commands or do I need to parse the INI file myself?

    Sorry, you must do it yourself. The special functions for accessing INI file
    content, @INIREAD and @INIWRITE are very limited. What you need is an
    @INIDELETE function, which could be built as a plug-in, but which nobody has
    created.
    --
    Steve
     
  3. MickeyF

    Joined:
    Jun 3, 2008
    Messages:
    76
    Likes Received:
    1
    In case anyone decides to try to write a plug-in, it should be REALLY easy. The standard windows API call, WritePrivateProfileString, can be used to delete the section in one call, providing NULL for the lpKeyName.

    See http://msdn.microsoft.com/en-us/library/ms725501(VS.85).aspx for details.

    Note that a very simple enhancement to the @IniWrite[] function would be to follow the same functionality as WritePrivateProfileString follows, namely, that

    %@iniwrite[.\MyIni.ini,SectionToDelete,,]

    would delete the section.
     
  4. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,190
    Likes Received:
    11
    This batch file should do the trick:

    if %1.==. .or. %2.==. (echo usage: %0 ini_name section_name & quit)
    setlocal
    set found_section=0
    copy %1 %1.bak
    del /q %1
    set h=%@fileopen[%1,w]
    :: turn off special character handling like pipes and redirection
    setdos /x-456
    :: iterate through every line in the original INI file
    do l in @%1.bak
    iff %found_section then
    :: section names always begin with a [.
    :: That's how we recognize the end of
    :: the section (by the start of the next).
    iff "%@left[1,%l]" == "[" then
    set found_section=0
    else
    ::skip over the lines in this section
    iterate
    endiff
    endiff
    :: If we find the section name then turn on
    :: the flag that tells us to not copy those lines
    iff %l == [%2] then
    set found_section=1
    iterate
    endiff
    :: otherwise, copy all the lines before and
    :: after the section
    set r=%@filewrite[%h,%l]
    enddo
    set r=%@fileclose[%h]
    endlocal

    -Scott




    "JP Software Forums" <neil@jpsoft.com>
    11/13/2008 12:33 PM
    Please respond to



    To
    Samintz@ra.rockwell.com
    cc

    Subject
    RE: [Support-t-638] Remove a section from an INI file






    thedave wrote:
    |
    | I'm looking for a way to remove an entire section from an INI file
    | including the header, not just the individual keys and settings
    | contained within, can anyone tell me if that's possible using
    | internal commands or do I need to parse the INI file myself?

    Sorry, you must do it yourself. The special functions for accessing INI
    file
    content, @INIREAD and @INIWRITE are very limited. What you need is an
    @INIDELETE function, which could be built as a plug-in, but which nobody
    has
    created.
    --
    Steve
     
  5. logic

    Joined:
    May 30, 2008
    Messages:
    42
    Likes Received:
    0
    From: JP Software Forums [mailto:neil@jpsoft.com]
    Sent: Thursday, November 13, 2008 11:53 AM
    Subject: RE: [Support-t-638] Re: Remove a section from an INI file

    A plugin is not necessary for a simple API call :-)

    set
    result=%@WINAPI[kernel32,WritePrivateProfileStringW,"SectionName",NULL,NULL,
    "filename.ini"]

    Done and done, modulo wrapping :-)

    Jonathan Gilbert
     
  6. MickeyF

    Joined:
    Jun 3, 2008
    Messages:
    76
    Likes Received:
    1
    Nice! Of course, since I'm only at 4nt 6.01, I didn't know/remember about the @WINAPI function. I'll still claim credit for the idea, even if you get credit for the implementation (as simple as it was). ;)
     
  7. thedave

    Joined:
    Nov 13, 2008
    Messages:
    253
    Likes Received:
    2
    Hey, that's cheating! Works great, thanks.
     

Share This Page