Delete a section in an ini-file

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
Tester
Junior Coder
Posts: 22
Joined: Fri Mar 20, 2009 2:48 pm

Delete a section in an ini-file

Post by Tester » Tue Jul 14, 2009 3:44 am

How can I delete a complete section in an ini-file?

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Jul 14, 2009 4:19 am

I'm sure there are other ways.

Be sure to make the value of the variable "file1" the full path and file name of the ini file.

The value of the variable "SectionName" will be case sensitive and must match the section name to be deleted exactly.

Code: Select all

Let>file1=IniFileToBeEdited
Let>SectionName=SectionNameToBeDeleted

ReadFile>File1,data
Separate>data,[,var
Let>kk=1
Let>newdata=
Repeat>kk
  add>kk,1
  Let>value=var_%kk%
  Separate>value,],test
  If>%test_1%=%SectionName%
  Else
    Let>newdata=%newdata%[%value%
  EndIf
Until>kk,%var_count%

Deletefile>file1
Let>WLN_NOCRLF=1
WriteLn>file1,wres,newdata
Last edited by JRL on Tue Jul 14, 2009 12:57 pm, edited 1 time in total.

Tester
Junior Coder
Posts: 22
Joined: Fri Mar 20, 2009 2:48 pm

Post by Tester » Tue Jul 14, 2009 5:27 am

I do not want the section name to be deleted, but the whole section including all entries. For example:

[section to be deleted] (somewhere in the ini-file)
entry1
entry2
entry3
.....

[maybe another section]

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Jul 14, 2009 12:57 pm

Code: Select all

I do not want the section name to be deleted, but the whole section including all entries.
Good... that's exactly what the above script will do.

Invent an ini file, call it C:\JRLtest.ini. Paste in the following:

Code: Select all

[Section1]
item1=1
item2=2

[Section2]
item1=1
item2=2

[Section3]
item1=1
item2=2

[Section4]
item1=1
item2=2
run the script above using C:\JRLtest.ini as variable "file1" and "Section2" as variable "SectionName". The resulting ini file will look like:

Code: Select all

[Section1]
item1=1
item2=2

[Section3]
item1=1
item2=2

[Section4]
item1=1
item2=2
Section2 and all Section2 items are deleted.

I apologize for an error in the script as originally posted. I left my test data in the text. It has now been removed.

Tester
Junior Coder
Posts: 22
Joined: Fri Mar 20, 2009 2:48 pm

Post by Tester » Tue Jul 14, 2009 1:04 pm

One last case that is not covered by the script. What if the section is the last section in the ini-file?

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Jul 14, 2009 1:10 pm

I just tested removing Section4 from the sample ini file. It removed it as planned.

What specific result are you seeing?

Tester
Junior Coder
Posts: 22
Joined: Fri Mar 20, 2009 2:48 pm

Post by Tester » Tue Jul 14, 2009 1:14 pm

Mistyped something. Works now. Thanks!

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Jul 14, 2009 1:18 pm

Glad that it works for you.

Happy scripting.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts