Jump to content

Delete string from INI


Ozi
 Share

Recommended Posts

I need something that can search my INI file and with a specified string delete it from the INI completely.

I doubt this can be done with INIDelete because it delete every key from the INI. Please correct me if I'm wrong.

Link to comment
Share on other sites

This is my code and it didn't delete the string from the INI although it removed it from the listview.

Case $msg = $RemoveSelected

Global $i_Index = _GUICtrlListViewGetCurSel( $nListview)

Global $varRemoved = _GUICtrlListViewDeleteItem($nListview, $i_index)

$file = FileOpen("C:\testini", 1)

If $file = -1 Then

MsgBox(0, "", "Unable to open file.")

EndIf

$chars = FileRead($file)

StringReplace($file, $varRemoved, "")

Link to comment
Share on other sites

This is my code and it didn't delete the string from the INI although it removed it from the listview.

Case $msg = $RemoveSelected

Global $i_Index = _GUICtrlListViewGetCurSel( $nListview)

Global $varRemoved = _GUICtrlListViewDeleteItem($nListview, $i_index)

$file = FileOpen("C:\testini", 1)

If $file = -1 Then

MsgBox(0, "", "Unable to open file.")

EndIf

$chars = FileRead($file)

StringReplace($file, $varRemoved, "")

Your file operation is using the wrong variables. Read the help file for each of these functions and look carefully at what they return.

The returned value in $varRemoved from _GuiCtrlListViewDeleteItem() is True/False, not the text of the item. You might consider using _GUICtrlListViewGetItemText($nListview, -1) instead.

$chars contains the data in the file specified by the file handle $file. You are performing your StringReplace() on the file handle instead of the data. You will also have to write the data back to the file after changing it.

<_<

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

So it would be this instead?:

Case $msg = $RemoveSelected

Global $i_Index = _GUICtrlListViewGetCurSel( $nListview)

Global $varRemoved = _GUICtrlListViewDeleteItem($nListview, $i_index)

$file = FileOpen("C:\test.ini", 1)

If $file = -1 Then

MsgBox(0, "", "Unable to open file.")

EndIf

$chars = FileRead($file)

StringReplace($char, $varRemoved, "")

Link to comment
Share on other sites

So it would be this instead?:

Case $msg = $RemoveSelected

So this is one case in a Select or Switch...

Global $i_Index = _GUICtrlListViewGetCurSel($nListview)

Index of the selected item in $i_Index... so far so good.

Global $varRemoved = _GUICtrlListViewDeleteItem($nListview, $i_index)

Now you've deleted the item before you read it. All you saved in $varRemoved is the success/fail status of the deletion.

If you want the text of the item removed from a file, you'll have to read that text with _GuiCtrlListViewGetItemText() before you delete it. And since it's a ListView, you'll have to specify the column to read.

$file = FileOpen("C:\test.ini", 1)

This opens the file for append. If you are going to change something and write back, you need to open mode 2 = overwrite.

Safer would be to just read it without a FileOpen() and then open it mode 2 only when you are ready to write back.

If $file = -1 Then

MsgBox(0, "", "Unable to open file.")

EndIf

This is fine, but just for trivia's sake, you can write it in one line without EndIf when there is only one conditional action to take:

If $file = -1 Then MsgBox(0, "", "Unable to open file.")

$chars = FileRead($file)

Reads the whole file into $chars.

StringReplace($char, $varRemoved, "")

Since all you saved to $varRemoved was the status of the item deletion - what are you replacing here?

Also, and ini file has a specific format, and you might be screwing that up by just deleting text in the middle of it.

Post a sample of the ini file so we can see how it is formatted. How are the ListView items listed in the ini file? It might be much easier to use the IniWrite() and/or IniDelete() functions.

<_<

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

My INI looks like this:

[section]

Key=listviewitem1,listviewitem2,listviewitem3

; The listview items are separated by commas.

BTW:

Is there something that can read every item in a listview and then I can just INIWrite(...) whatever

items it retrieved. Because I have no problem deleting the items in the listview, its the rewriting the

INI part after the listview item has been deleted.

Edited by Ozi
Link to comment
Share on other sites

My INI looks like this:

[section]

Key=listviewitem1,listviewitem2,listviewitem3

; The listview items are separated by commas.

BTW:

Is there something that can read every item in a listview and then I can just INIWrite(...) whatever

items it retrieved. Because I have no problem deleting the items in the listview, its the rewriting the

INI part after the listview item has been deleted.

OK, that's easy. Make sure you get the text of item before you delete it. Then, you want to read the key, remove the item that was deleted, then write the key back:

$sINI = "C:\MyDir\MyFile.ini"
$sItem = "listviewitem2" ; Text of item just deleted
$sData = IniRead($sINI, "Section", "Key")
If StringRight($sData, StringLen($sItem)) = $sItem Then
    $sData = StringTrimRight($sData, StringLen($sItem) + 1) ; Take out item and preceeding comma
Else
    $sData = StringReplace($sData, $sItem & ",", "") ; Delete item with following comma
EndIf
IniWrite($sINI, "Section", "Key", $sData)

<_<

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

OK, that's easy. Make sure you get the text of item before you delete it. Then, you want to read the key, remove the item that was deleted, then write the key back:

$sINI = "C:\MyDir\MyFile.ini"
$sItem = "listviewitem2" ; Text of item just deleted
$sData = IniRead($sINI, "Section", "Key")
If StringRight($sData, StringLen($sItem)) = $sItem Then
    $sData = StringTrimRight($sData, StringLen($sItem) + 1) ; Take out item and preceeding comma
Else
    $sData = StringReplace($sData, $sItem & ",", "") ; Delete item with following comma
EndIf
IniWrite($sINI, "Section", "Key", $sData)

<_<

Thanks, and that works great but what if I don't "directly" give it the string of the listview item that was deleted.

Would this variable hold the string to the listview item that was deleted?: $sItem = _GUICtrlListViewGetItemText($nListview, $i_index, -1)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...