Steviep

How to delete .INI entries containing specific values?

5 posts in this topic

Hi all,

I have a script which is used as part of a wrapper for an installer. The script is responsible to performing certain actions, a lot of which involve IniRead & IniWrite.

Today I have been presented with a tricky situation, here's what I am trying to accomplish:

The AutoIT script will read an existing .INI file which contains a section like this:

[Customise]
CustomScript = "Bemaßen" , "Störmer" , "Schließn" , "Schnell schließen" , "" , "OnCloseNoImage.rpt" , "Schnelles schließen der Planung ohne Ansichten zu erzeugen" , "" , "0"
CustomScript = "Einfügen" , "Störmer" , "Schließn" , "Schnell schließen" , "" , "OnCloseNoImage.rpt" , "Schnelles schließen der Planung ohne Ansichten zu erzeugen" , "" , "0"
CustomScript = "Ansicht" , "Störmer" , "Schließn" , "Schnell schließen" , "" , "OnCloseNoImage.rpt" , "Schnelles schließen der Planung ohne Ansichten zu erzeugen" , "" , "0"
CustomScript = "Start" , "Insight" , "Planung ersetzen" , "Prüfen" , "" , "ValidateItems.rpt" , "Prüfen der Artikelkonfiguration" , "" , "0"
CustomScript = "Start" , "Insight" , "Prüfen" , "Preis" , "" , "PriceItems.rpt" , "Preise bestimmen" , "" , "0"
CustomScript = "Start" , "Insight" , "Preis" , "Abbrechen" , "" , "AbortChanges.rpt" , "Abbrechen der Planung ohne aktuelle Änderungen zu übernehmen" , "" , "0"
CustomScript = "Start" , "Insight" , "Abbrechen" , "Spiegeln" , "" , "OnMirror.rpt" , "Horizontales oder vertikales Spiegeln der aktuellen Planung" , "" , "0"
CustomScript = "Start" , "Insight" , "Spiegeln" , "Schnell schließen" , "" , "OnCloseNoImage.rpt" , "Schnelles schließen der Planung ohne Ansichten zu erzeugen" , "" , "0"
CustomScript = "Start" , "Insight" , "Schnell schließen" , "Schließn" , "" , "OnClose.rpt" , "Schnelles schließen der Planung mit Ansichten zu erzeugen" , "" , "0"

I need to delete only the lines which contain "CustomScript = "Start" , "Insight"".

I have not had any luck with IniDelete since this function only accepts Filename, Section and Key, but no value. Any suggestions?

Many thanks!

Share this post


Link to post
Share on other sites



This file structure is incompatible with .INI conventions: duplicate keys can't be handled properly using standard calls.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Thanks for the quick response JCHD. I had a feeling this might be the case. As a workaround, do you believe there would be a way to simply delete any line which starts with "CustomScript = "Start" , "Insight"" ?

Share this post


Link to post
Share on other sites

Steviep,

Read the file to an array and then check each line to see if it matches the required format - delete it if it does:

#include <Array.au3>

; Simulate reading file to an array
Global $aLines[] = ['[Customise]', _
             'CustomScript = "Bemaßen" , "Störmer" , "Schließn" , "Schnell schließen" , "" , "OnCloseNoImage.rpt" , "Schnelles schließen der Planung ohne Ansichten zu erzeugen" , "" , "0"', _
             'CustomScript = "Einfügen" , "Störmer" , "Schließn" , "Schnell schließen" , "" , "OnCloseNoImage.rpt" , "Schnelles schließen der Planung ohne Ansichten zu erzeugen" , "" , "0"', _
             'CustomScript = "Ansicht" , "Störmer" , "Schließn" , "Schnell schließen" , "" , "OnCloseNoImage.rpt" , "Schnelles schließen der Planung ohne Ansichten zu erzeugen" , "" , "0"', _
             'CustomScript = "Start" , "Insight" , "Planung ersetzen" , "Prüfen" , "" , "ValidateItems.rpt" , "Prüfen der Artikelkonfiguration" , "" , "0"', _
             'CustomScript = "Start" , "Insight" , "Prüfen" , "Preis" , "" , "PriceItems.rpt" , "Preise bestimmen" , "" , "0"', _
             'CustomScript = "Start" , "Insight" , "Preis" , "Abbrechen" , "" , "AbortChanges.rpt" , "Abbrechen der Planung ohne aktuelle Änderungen zu übernehmen" , "" , "0"', _
             'CustomScript = "Start" , "Insight" , "Abbrechen" , "Spiegeln" , "" , "OnMirror.rpt" , "Horizontales oder vertikales Spiegeln der aktuellen Planung" , "" , "0"', _
             'CustomScript = "Start" , "Insight" , "Spiegeln" , "Schnell schließen" , "" , "OnCloseNoImage.rpt" , "Schnelles schließen der Planung ohne Ansichten zu erzeugen" , "" , "0"', _
             'CustomScript = "Start" , "Insight" , "Schnell schließen" , "Schließn" , "" , "OnClose.rpt" , "Schnelles schließen der Planung mit Ansichten zu erzeugen" , "" , "0"']

$sString = 'CustomScript = "Start" , "Insight"'
$iLen = StringLen($sString)

; Now examine each line - note we start at the end
For $i = UBound($aLines) - 1 To 0 Step -1
    ; If the line starts with the required text
    If StringLeft($aLines[$i], $iLen) = $sString Then
        ; Delete it
        _ArrayDelete($aLines, $i)
    EndIf
Next

; And here is the result
_ArrayDisplay($aLines, "", Default, 8)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

Normally I would be very tempted to add that a highly generic, flexible, safe and powerful file storage for application data is best achieved by SQLite, but of course I wouldn't dare to say that here o:)

Yet all these kind of issues (and much more) would be quickly elegantly solved by doing so. Worth $0.02.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now