www.fireflysoftware.com

Removing a Section from an .INI File


If you've ever needed to remove a section from an .INI file, you know that it isn't a trivial matter.  In this example, we'll show how to do this using TEXTools.  The example will demonstrate also how to handle multiple lines as a group.  This technique is very useful when using TEXTools.

First, suppose you have an .INI file from which you want to remove the TCPIP section:

[FILES]
Order=ba
a=c:\file a.txt
b=c:\file b.txt

[TCPIP]
ID=9
Path=C:\NET
Vers=2.0

[SPOOLER]
Header=2C
State=03

The "algorithm" for doing this is to join each .INI section onto a separate line, delete the targeted line and then restore the remaining lines back to their original structure.  Before joining the lines onto a single line, first, we must add marker strings to each line to help identify the beginning and end of each line:

InsStr 1 '<bol>'
AppendStr '<eol>'

Inserting "<bol>" makes it easy to distinguish the name of the section we want to delete from other text in the file.  Appending "<eol>" allows us to restore each line's current structure after we've deleted the targeted section.  

Note: The application of the above two filters demonstrates how to symbolically represent a blank line in a text because, if you insert the string, "<bol>" at the head of each line and then append "<eol>" at the end of each line, then blank lines are all of those that contain the string, "<bol><eol>".  This is useful if you must search or process text with reference to blank lines.  Here, this is mostly just a side-effect of our algorithm although we will still take advantage of this to remove these blank lines later.

Back to our problem at-hand.   Running the text through the above two filters gives us this:

<bol>[FILES]<eol>
<bol>Order=ba<eol>
<bol>a=c:\file a.txt<eol>
<bol>b=c:\file b.txt<eol>
<bol><eol>
<bol>[TCPIP]<eol>
<bol>ID=9<eol>
<bol>Path=C:\NET<eol>
<bol>Vers=2.0<eol>
<bol><eol>
<bol>[SPOOLER]<eol>
<bol>Header=2C<eol>
<bol>State=03<eol>

Next, the lines are joined onto a single line using the JoinLines filter:

JoinLines

The result of this is that all the text ends up on a single line.  This one line is then "broken" at each "<bol>[" using the ReplStr filter:

ReplStr '<bol>[' '#0d#0a<bol>['

This outputs a carriage return / linefeed ahead of each "<bol>[" and in the process, it adds a blank line ahead of the first line.  The result is a blank line followed by 3 lines, (which might be shown here wrapped):


<bol>[FILES]<eol><bol>Order=ba<eol><bol>a=c:\file a.txt<eol><bol>b=c:\file b.txt<eol><bol><eol>
<bol>[TCPIP]<eol><bol>ID=9<eol><bol>Path=C:\NET<eol><bol>Vers=2.0<eol><bol><eol>
<bol>[SPOOLER]<eol><bol>Header=2C<eol><bol>State=03<eol>

Although the preceding blank line doesn't really hurt anything, it's removed using DelBlankLines:

DelBlankLines

At this point, each .INI section is now on a line all by itself and prefixed by the "<bol>" marker, (again, lines might be wrapped):

<bol>[FILES]<eol><bol>Order=ba<eol><bol>a=c:\file a.txt<eol><bol>b=c:\file b.txt<eol><bol><eol>
<bol>[TCPIP]<eol><bol>ID=9<eol><bol>Path=C:\NET<eol><bol>Vers=2.0<eol><bol><eol>
<bol>[SPOOLER]<eol><bol>Header=2C<eol><bol>State=03<eol>

Removing the TCPIP section is now simply a matter of deleting the one line that represents it:

ExclLines '<bol>[TCPIP'

This, of course, results in the following:

<bol>[FILES]<eol><bol>Order=ba<eol><bol>a=c:\file a.txt<eol><bol>b=c:\file b.txt<eol><bol><eol>
<bol>[SPOOLER]<eol><bol>Header=2C<eol><bol>State=03<eol>

This eliminates the unwanted section.  We must now restore the remaining lines back to their original form.  To do this we first remove any symbolically represented blank lines (discussed earlier) and then restore the original line breaks:

ReplStr '<bol><eol>' '' '<eol>' '#0d#0a'

This gives us the following:

<bol>[FILES]
<bol>Order=ba
<bol>a=c:\file a.txt
<bol>b=c:\file b.txt

<bol>[SPOOLER]
<bol>Header=2C
<bol>State=03

Finally, the inserted "<bol>" markers are removed:

DelChars 1 5

Here's the final result:

[FILES]
Order=ba
a=c:\file a.txt
b=c:\file b.txt

[SPOOLER]
Header=2C
State=03

Following is the completed (and commented) pipe:

; Insert marker strings to help identify the
; beginning and end of each line:

InsStr 1 '<bol>'
AppendStr '<eol>'

; Join all the lines onto a single line and then
; force each .INI section onto its own line:

JoinLines
ReplStr '<bol>[' '#0d#0a<bol>['
DelBlankLines

; Remove the targeted section:

ExclLines '<bol>[TCPIP'

; Finally, return the lines to their original
; structure:

ReplStr '<bol><eol>' '' '<eol>' '#0d#0a'
DelChars 1 5

 

 

[Home] [Contact Us] [Downloads] [Purchase/Register]

Copyright © 2005 Firefly Software