Ozi Posted November 5, 2007 Share Posted November 5, 2007 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 More sharing options...
Moderators SmOke_N Posted November 5, 2007 Moderators Share Posted November 5, 2007 FileRead+StringReplace Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Ozi Posted November 5, 2007 Author Share Posted November 5, 2007 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 More sharing options...
JustinReno Posted November 5, 2007 Share Posted November 5, 2007 You forgot to put a period in test.ini Link to comment Share on other sites More sharing options...
PsaltyDS Posted November 5, 2007 Share Posted November 5, 2007 (edited) 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 November 5, 2007 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 More sharing options...
Ozi Posted November 6, 2007 Author Share Posted November 6, 2007 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 More sharing options...
PsaltyDS Posted November 6, 2007 Share Posted November 6, 2007 So it would be this instead?:Case $msg = $RemoveSelectedSo 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 ThenMsgBox(0, "", "Unable to open file.")EndIfThis 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 More sharing options...
Ozi Posted November 7, 2007 Author Share Posted November 7, 2007 (edited) 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 November 7, 2007 by Ozi Link to comment Share on other sites More sharing options...
Ozi Posted November 7, 2007 Author Share Posted November 7, 2007 Is there some way to INIWrite() everything into the INI except for the listview item that was just deleted? Link to comment Share on other sites More sharing options...
PsaltyDS Posted November 7, 2007 Share Posted November 7, 2007 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 More sharing options...
Ozi Posted November 8, 2007 Author Share Posted November 8, 2007 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 More sharing options...
Ozi Posted November 8, 2007 Author Share Posted November 8, 2007 (edited) Nevermind, problem solved. @PsaltyDS, thanks for all the great help you've given me. Edited November 8, 2007 by Ozi Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now