goss34 Posted January 23, 2015 Posted January 23, 2015 Morning Guys, I have worked all night and during dead spots I have been looking at trying to read a text file e.g. Test.txt Server1 Server2 Server3 The text file should basically populate into a list view so you can see all the entries in the text file. From the various example that seems pretty simple (although not for me). What i want to achieve is: Gui loads, It has an input box, fill in the input box, click an "add" button that writes the text of the input box into the file which then displays in the list view. I also want to have a button that will remove a line or multiple if clicked after being selected in the list view. So 2 buttons, 1 input box, 1 listview - doesn't sound that hard but i cannot piece together all the different pieces of code into one that does that i want. I have been at it since around 1am and now i'm running on empty and my eyes are stinging. It needs to be dynamic as i don't want to limit how many entries can be added to the file from the input box and want the listview to show them no matter how many are in the text file. Can one of the magicians knock me something up? It doesn't have to be exact but I am getting nowhere fast and its driving me up the wall. Im starting to think this AutoIT is too much for my brain to handle Cheers Dan P.s. Played with FileReadToArray("Test.txt"), _GUICtrlListView_AddArray, Even tried to swap to an INI but end up with the same problem where i cant fit all the pieces together.
JohnOne Posted January 23, 2015 Posted January 23, 2015 Post your gui code so people have somewhere to start. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
goss34 Posted January 23, 2015 Author Posted January 23, 2015 Hi JohnOne, Iv had a go in so many ways iv not settled on any code cause i cant get either to do what i want - I haven't written from scratch, these are examples in help or taken from dated forum posts. Here's what iv still got saved. Utilising ini and combobox - decided against combo as you cant multi delete so its not efficient. It does however remove an entry from the ini and sort of writes an entry to it just not in the correct format hence why i started looking to use a text file thinking it would be easier. expandcollapse popupOpt("GUIOnEventMode", 1) #include <Array.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> Global $CurrentComment, $CommentList, $RemoveCommentGUI _Test() Func _Test() $CurrentGui = "Comments" $RemoveCommentGui = GUICreate("Comments", 600, 400) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit") $CommentList = GUICtrlCreateCombo("", 208, 10, 160, 30) $CurrentComment = IniReadSection("Ini File.ini", "SERVERS") For $i = 1 To $CurrentComment[0][0] GUICtrlSetData(-1, $CurrentComment[$i][1]) Next $DeleteButton = GUICtrlCreateButton("Remove", 250, 40, 100, 30) GUICtrlSetOnEvent(-1, "_RemoveCommentFromList") $CancelButton = GUICtrlCreateButton("Cancel", 250, 80, 100, 30) GUISetBkColor(0xFFFFFF) GUISetFont(12) $Input = GUICtrlCreateInput("Type Server Name", 8, 8, 185, 21) $IniWrite = GUICtrlCreateButton("Add Server", 8, 65, 100, 20, 0) GUISetState() EndFunc Func _RemoveCommentFromList() $CommentToRemove = GUICtrlRead($CommentList) $Find = _ArraySearch($CurrentComment, $CommentToRemove) $Delete = _ArrayDelete($CurrentComment, $Find) IniDelete("Ini File.ini", "SERVERS") For $i = 1 To $CurrentComment[0][0] - 1 IniWrite("Ini File.ini", "SERVERS", $i, $CurrentComment[$i][1]) Next ;IniWrite("Ini File.ini", "SERVERS", 1, "") GuiDelete($RemoveCommentGUI) _test() EndFunc ;==>_RemoveCommentFromList Func _Exit() Exit EndFunc While 1 Sleep(10) WEnd This is reading the data from the text file - great #include <MsgBoxConstants.au3> Example() Func Example() ; Read the current script file into an array using the filepath. Local $aArray = FileReadToArray("logon.txt") If @error Then MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file. Else For $i = 0 To UBound($aArray) - 1 ; Loop through the array. MsgBox($MB_SYSTEMMODAL, "", $aArray[$i]) ; Display the contents of the array. Next EndIf EndFunc ;==>Example This is showing an array in a list view but i cant figure out how to get it to read from the txt file to populate the listview: #AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 #include <GuiConstantsEx.au3> #include <GuiListView.au3> Opt('MustDeclareVars', 1) ;$Debug_LV = False ; Check ClassName being passed to ListView functions, set to True and use a handle to another control to see it work _Main() Func _Main() Local $iI, $hListView ; Create GUI GUICreate("ListView Add Array", 400, 300) $hListView = GUICtrlCreateListView("", 2, 2, 394, 268) GUISetState() ; Add columns _GUICtrlListView_AddColumn($hListView, "Program", 100) ; this block of code only generates the array Local $aItems[200][1] For $iI = 0 To UBound($aItems) - 1 $aItems[$iI][0] = "Item " & $iI Next _GUICtrlListView_AddArray($hListView, $aItems) ; Loop until user exits Do Until GUIGetMsg() = $GUI_EVENT_CLOSE GUIDelete() EndFunc ;==>_Main I think the majority of what i am trying to do is in the first piece of code but i want to swap it from a combo to a list view and then fix the "add" button to write the value correctly. I dont need to use .ini - the txt file will essentially be a list, each item on a new line. Cheers Dan
javiwhite Posted January 23, 2015 Posted January 23, 2015 Hi Dan, I've had a quick fiddle with your first script, and shown an example of how to populate a listview whilst also writing to an ini file: expandcollapse popup#include <Array.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> Global $CurrentComment, $CommentList, $RemoveCommentGUI Global $ini = @Scriptdir & "\file.ini" ;PreDeclared as i don't know where your INI file is stored - Change this to your DIR if it's not in the same folder as the script. _Test() Func _Test() Local $CommentListItem[1] $CommentListItem[0] = 1 ;item value for the listview array ;Create The GUI $CurrentGui = "Comments" $RemoveCommentGui = GUICreate("Comments", 600, 400) $CommentList = GUICtrlCreatelistView("Server", 208, 10, 260, 130) $CurrentComment = IniReadSection($ini, "SERVERS") ;The filename for the ini file was incorrect. $Input = GUICtrlCreateInput("Type Server Name", 8, 8, 185, 21) $IniWrite = GUICtrlCreateButton("Add Server", 8, 65, 100, 20, 0) $DeleteButton = GUICtrlCreateButton("Remove", 50, 140, 100, 30) $CancelButton = GUICtrlCreateButton("Cancel", 50, 180, 100, 30) GUISetBkColor(0xFFFFFF) GUISetFont(12) GUISetState() ;Populate the ListView For $i = 1 To $CurrentComment[0][0] IncreaseArray($CommentListItem) $CommentListItem[$CommentListItem[0]] = GUICtrlCreateListViewItem($CurrentComment[$i][1],$CommentList) ;Use this function to add items to a listview $CommentListItem[0]+= 1 Next ;While loop to wait for input while 1 switch GUIgetMsg() Case $GUI_EVENT_CLOSE,$CancelButton Exit Case $DeleteButton GuiCtrlDelete(GUIctrlRead($CommentList)) Case $IniWrite $NewServer = GUIctrlRead($Input) ; Read the InputBox GUIctrlSetData($Input,"") ; Reset the input IncreaseArray($CommentListItem) ; Increase the Array size to add the new item IniWrite($ini,"SERVERS",$NewServer,$NewServer) ;I've had to put the same value for parameter and value, as you only have one input box (look at how INI's work, each value needs a parameter, and a value) $CommentListItem[$CommentListItem[0]] = GUIctrlCreateListViewItem($NewServer,$CommentList) ; Add the new item $CommentListItem[0] += 1 ; Increase the item count EndSwitch WEnd EndFunc Func IncreaseArray(ByRef $Array) local $Temp_Array = $Array ReDim $Array[Ubound($Temp_Array)+1] for $i = 0 to uBound($Temp_Array) - 1 $Array[$i] = $Temp_Array[$i] Next return 0 EndFunc In my hastiness, I didn't realise that this was not actually the script with the ListView in, so i do apologise for that, (I've swapped out the combo for the ListView... I would go back, and attempt an example on the script that does contain the listview, But i'm at work at the moment, So my time's limited. I've added comments to try and help you understand what's going on, Hopefully this will get you on the right tracks - Javi give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.
goss34 Posted January 23, 2015 Author Posted January 23, 2015 Hi Javi, Thanks for trying to help me, its really going over my head at the minute. I'm struggling to understand the Array code even after reading the Wiki article 3 times - not sure if its fatigue or just not going in. Iv tried to strip everything out below to start from scratch so hopefully i can pick it up as its rebuilt. I have set this to create the file i with some data so it doesn't matter who runs it everyone will see the same result at their end. After the file is ready, the lines are read output by msgbox, this is working for me. #include <MsgBoxConstants.au3> #include <File.au3> $sFilePath = (@TempDir & "\test.txt") ; Set Path Directory If Not FileExists($sFilePath) Then _FileCreate ($sFilePath) ; if file does not exist create file MsgBox(0, "Created", "Yes") ; confirmation file was created $file = FileOpen($sFilePath, 1) ; open file for editing If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Else FileWrite($file, "Server1" & @CRLF) ; write 1 line to file FileWrite($file, "Server2" & @CRLF) ; write additional line to file FileWrite($file, "Server3" & @CRLF) ; write additional line to file FileClose($file) ; close open file EndIf Else EndIf Example() Func Example() ; Read the current script file into an array using the filepath. Local $aArray = FileReadToArray($sFilePath) If @error Then MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file. Else For $i = 0 To UBound($aArray) - 1 ; Loop through the array. MsgBox($MB_SYSTEMMODAL, "", $aArray[$i]) ; Display the contents of the array. Next EndIf EndFunc ;==>Example Can you now set it so the array is read into the listview so i can follow just those changes? (not worried about buttons or functions at the minute i just want to try to understand how to get the array into the view). Thank you, Dan
kylomas Posted January 23, 2015 Posted January 23, 2015 Dan, Building on your code with comments... expandcollapse popup#include <GUIConstantsEx.au3> ;#include <MsgBoxConstants.au3> #include <File.au3> $sFilePath = (@TempDir & "\test.txt") ; Set Path Directory If Not FileExists($sFilePath) Then _FileCreate($sFilePath) ; if file does not exist create file MsgBox(0, "Created", "Yes") ; confirmation file was created $file = FileOpen($sFilePath, 1) ; open file for editing If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Else FileWrite($file, "Server1" & @CRLF) ; write 1 line to file FileWrite($file, "Server2" & @CRLF) ; write additional line to file FileWrite($file, "Server3" & @CRLF) ; write additional line to file FileClose($file) ; close open file EndIf Else EndIf Example() Func Example() ; Read the current script file into an array using the filepath. Local $aArray = FileReadToArray($sFilePath) If @error Then MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file. ; commented out the msgbox code ;~ Else ;~ For $i = 0 To UBound($aArray) - 1 ; Loop through the array. ;~ MsgBox($MB_SYSTEMMODAL, "", $aArray[$i]) ; Display the contents of the array. ;~ Next EndIf ; start gui code local $gui = guicreate('Listview From File Array',220,200) local $lv = GUICtrlCreateListView('Servers',20,20,61,120) local $btn = GUICtrlCreatebutton('Get Server',10,160,80,20) ; this routine reads the array created above and populates the listview for $1 = 0 to UBound($aArray) - 1 GUICtrlCreateListViewItem($aArray[$1],$lv) next guisetstate() while 1 switch guigetmsg() case $GUI_EVENT_CLOSE guidelete($gui) Exit case $btn msgbox( 0,'Selection','You selected Server = ' & stringtrimright( _ ; strip '|' from end of string guictrlread( _ ; get control id of selected item guictrlread($lv)), _ ; get listview item text using the control id 1) ) ; strip 1 char (parm of stringtrimright function ; ; you will generally see this written as "guictrlread(guictrlread($lv))". I broke it apart so I could comment it. ; ConsoleWrite(guictrlread(guictrlread($lv)) & @CRLF) ; this is how I would normally write this EndSwitch wend EndFunc ;==>Example Please ask if anything is not clear. This is probably the simplest use of listview. If you want to get into clicking on listview items or using context menus (right click and get options) just ask. kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
goss34 Posted January 26, 2015 Author Posted January 26, 2015 Thank you Kylomas, I made a couple of slight amendments (size of window and listview items) and added in a console write. I have a couple of questions to follow the full code with the few amendments i made: expandcollapse popup#include <GUIConstantsEx.au3> ;#include <MsgBoxConstants.au3> #include <File.au3> $sFilePath = (@TempDir & "\test.txt") ; Set Path Directory If Not FileExists($sFilePath) Then _FileCreate($sFilePath) ; if file does not exist create file MsgBox(0, "Created", "Yes") ; confirmation file was created $file = FileOpen($sFilePath, 1) ; open file for editing If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Else FileWrite($file, "Server1" & @CRLF) ; write 1 line to file FileWrite($file, "Server2" & @CRLF) ; write additional line to file FileWrite($file, "Server3" & @CRLF) ; write additional line to file FileClose($file) ; close open file EndIf Else EndIf Example() Func Example() ; Read the current script file into an array using the filepath. Local $aArray = FileReadToArray($sFilePath) If @error Then MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file. ; commented out the msgbox code ;~ Else ;~ For $i = 0 To UBound($aArray) - 1 ; Loop through the array. ;~ MsgBox($MB_SYSTEMMODAL, "", $aArray[$i]) ; Display the contents of the array. ;~ Next EndIf ; start gui code local $gui = guicreate('Listview From File Array',320,320) local $lv = GUICtrlCreateListView('Servers',20,20,66,150) local $btn = GUICtrlCreatebutton('Get Server',10,200,80,20) ; this routine reads the array created above and populates the listview for $1 = 0 to UBound($aArray) - 1 GUICtrlCreateListViewItem($aArray[$1],$lv) ConsoleWrite($aArray[$1] & @CRLF) ; added just so i can see the data received into the array next guisetstate() while 1 switch guigetmsg() case $GUI_EVENT_CLOSE guidelete($gui) Exit case $btn msgbox( 0,'Selection','You selected Server = ' & stringtrimright( _ ; strip '|' from end of string guictrlread( _ ; get control id of selected item guictrlread($lv)), _ ; get listview item text using the control id 1) ) ; strip 1 char (parm of stringtrimright function ; ; you will generally see this written as "guictrlread(guictrlread($lv))". I broke it apart so I could comment it. ; ConsoleWrite(guictrlread(guictrlread($lv)) & @CRLF) ; this is how I would normally write this EndSwitch wend EndFunc ;==>Example So here i just added the console write: ; this routine reads the array created above and populates the listview for $1 = 0 to UBound($aArray) - 1 GUICtrlCreateListViewItem($aArray[$1],$lv) ConsoleWrite($aArray[$1] & @CRLF) ; added just so i can see the data received into the array next Can you tell me where i can see some info on how this "GUICtrlCreateListViewItem($aArray[$1],$lv)" will know to populate multiple list view items and not just add it as one long string? The output of the console write below it shows Server1Server2Server3. Ok and question 2 is about this section: msgbox( 0,'Selection','You selected Server = ' & stringtrimright( _ ; strip '|' from end of string guictrlread( _ ; get control id of selected item guictrlread($lv)), _ ; get listview item text using the control id 1) ) ; strip 1 char (parm of stringtrimright function ; ; you will generally see this written as "guictrlread(guictrlread($lv))". I broke it apart so I could comment it. ; ConsoleWrite(guictrlread(guictrlread($lv)) & @CRLF) ; this is how I would normally write this The console write shows the | character and i can see how you removed it from the message box but why has it appeared in the first place as it was not included in the array text. Is this just the column separator produced as part of being in the list view item? Thanks again, sorry for the slow progress but id rather try and understand all the code before attempting to expand on it and this is helping. Dan
kylomas Posted January 26, 2015 Posted January 26, 2015 (edited) Hi Dan, No need to apologize, we all learn at our own pace. Regarding the '|' character, I have no idea. Frankly I was suprised to see it. Maybe one of the other members knows. The listview is populated by running GUICtrlCreateListViewItem for each member of the array in a loop. Each time it is executed an entery is added to the listview control. $aArray[0] = Server1 $aArray[1] = Server2 $aArray[2] = Server3 The loop starts with $1 = to 0. An item is added to the listview from $aArray[0] The Next statement increments $1 and goes back to the loop test statement (For...Next) Your output from the consolewrite should not be "Server1Server2Server3'. It should be Server1 Server2 Server3 I hope this helps, if not just ask. kylomas edit:spelling Edited January 26, 2015 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
goss34 Posted January 26, 2015 Author Posted January 26, 2015 Great, ok as for the "|" will just wait and see if anyone else can explain that. The consolewrite is displaying as you said since i added the @CRLF but prior it was in a long string which i presume is just how the consolewrite displays and would explain why generally i see @CRLF in most examples on the forum. So I have now added 2 more buttons and an input box to update the file from the gui: expandcollapse popup#include <GUIConstantsEx.au3> ;#include <MsgBoxConstants.au3> #include <File.au3> $sFilePath = (@TempDir & "\test.txt") ; Set Path Directory If Not FileExists($sFilePath) Then _FileCreate($sFilePath) ; if file does not exist create file MsgBox(0, "Created", "Yes") ; confirmation file was created $file = FileOpen($sFilePath, 1) ; open file for editing If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Else FileWrite($file, "Server1.domain.local" & @CRLF) ; write 1 line to file FileWrite($file, "Server2.domain.local" & @CRLF) ; write additional line to file FileWrite($file, "Server3.domain.local" & @CRLF) ; write additional line to file FileClose($file) ; close open file EndIf Else EndIf Example() Func Example() ; Read the current script file into an array using the filepath. Local $aArray = FileReadToArray($sFilePath) If @error Then MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file. ; commented out the msgbox code ;~ Else ;~ For $i = 0 To UBound($aArray) - 1 ; Loop through the array. ;~ MsgBox($MB_SYSTEMMODAL, "", $aArray[$i]) ; Display the contents of the array. ;~ Next EndIf ; start gui code local $gui = guicreate('Listview From File Array',320,320) local $lv = GUICtrlCreateListView('Monitored Servers',10,50,160,150) local $getserverbtn = GUICtrlCreatebutton('Get Server',180,50,80,20) local $addbtn = GUICtrlCreateButton("Add", 180, 20, 80, 20) local $removebtn = GUICtrlCreateButton("Remove", 180, 80, 80, 20) local $Input = GUICtrlCreateInput("Type Server Name To Add...", 10,20,160,20) ; this routine reads the array created above and populates the listview for $1 = 0 to UBound($aArray) - 1 GUICtrlCreateListViewItem($aArray[$1],$lv) ConsoleWrite($aArray[$1] & @CRLF) ; added just so i can see the data received into the array next guisetstate() while 1 switch guigetmsg() case $GUI_EVENT_CLOSE guidelete($gui) Exit case $addbtn $file = FileOpen($sFilePath, 1) ; open file for editing FileWrite($file, GUICtrlRead($Input) & @CRLF) ; write entry from input box into file FileClose($file) ; close open file case $removebtn MsgBox(0,"Remove", "Remove button pushed", 0) Global $success = False $file_name = $sFilePath ; $line_text_input = InputBox("Line's text", "Line must contain following text:", "line contains this text") $line_text_input = (StringTrimRight( _ guictrlread( _ ; get control id of selected item guictrlread($lv)), _ 1) ) ; get listview item text using the control id $file_count_lines = _FileCountLines($file_name) for $i = 0 to $file_count_lines $Lines_text_output = FileReadLine($file_name, $i) if StringInStr($Lines_text_output, $line_text_input) then _FileWriteToLine($file_name, $i, "", 1) $success = True ExitLoop EndIf Next if $success = True Then MsgBox(0, "Success", "Line has been deleted") Else MsgBox(0, "Failure", "Line wasn't found") EndIf case $getserverbtn msgbox( 0,'Selection','You selected Server = ' & stringtrimright( _ ; strip '|' from end of string guictrlread( _ ; get control id of selected item guictrlread($lv)), _ ; get listview item text using the control id 1) ) ; strip 1 char (parm of stringtrimright function ; ; you will generally see this written as "guictrlread(guictrlread($lv))". I broke it apart so I could comment it. ; ConsoleWrite(guictrlread(guictrlread($lv)) & @CRLF) ; this is how I would normally write this EndSwitch wend EndFunc ;==>Example The add button should now work and update the text file with whatever data was added to the input box - it does for me anyway. What i don't know how to do is to get the listview to update after clicking the add button? I have found some code to help me remove a matching line from the file when pushing the remove button which i think works fine however it may not be the most efficient way? Again the same issue with how do i get the listview to update after clicking the remove button? Thank you, Dan
javiwhite Posted January 26, 2015 Posted January 26, 2015 (edited) Great, ok as for the "|" will just wait and see if anyone else can explain that. The pipe key is used as the delimiter for ListViews, and is also applied to the last column item. (so if you inserted " col1 | col2 | col3 " a third pipe would be added to the end automatically) As you're only using the one column in your listview though, You will only see the one added at the end. Listviews are great for supplying additional information on an item; and to do this you would format it as such: #include<GUIconstants.au3> local $gui = GUIcreate("GUI",300,300) local $ListView = GUIctrlCreateListView("Col1|Col2|Col3",5,5,290,290) GUIctrlCreateListViewItem("Row1 Col1|Row1 Col2|Row1 Col3",$ListView) GUIctrlCreateListViewItem("Row2 Col1|Row2 Col2|Row2 Col3",$ListView) GUIctrlCreateListViewItem("Row3 Col1|Row3 Col2|Row3 Col3",$ListView) GUISetState(@sw_show) do until GUIgetMsg() = $GUI_EVENT_CLOSE Hopefully that helps to explain why you're seeing the pipe key when you read the Listview. EDIT: It may be worth mentioning that if a ListViewItem has more columns than the listview, it will not be inserted, But if it has less columns, it will still insert (and the remaining columns will simply be blank) Many Thanks Javi Edited January 26, 2015 by javiwhite give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.
goss34 Posted January 26, 2015 Author Posted January 26, 2015 Thank you Javi, that's what i thought would be the answer so good to see the confirmation. I think the only thing left now is how to get the listview to update after either the add or remove button click, for that i have no clue at the moment but am sure someone will know... Cheers Dan
Solution kylomas Posted January 26, 2015 Solution Posted January 26, 2015 Dan, What i don't know how to do is to get the listview to update after clicking the add button? The same way we updated it before only now you'll get the listview data from the input control instead of the array, e.g. GUICtrlCreateListViewItem(guictrlread($input_control),$lv) To delete an item see _GUICtrlListView_DeleteItem. Hint, you get the item index using another _GUICtrlListView_* function. kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
goss34 Posted January 26, 2015 Author Posted January 26, 2015 Cheers Kylomas, i think its complete as below: expandcollapse popup#include <GUIConstantsEx.au3> ;#include <MsgBoxConstants.au3> #include <File.au3> #include <GuiListView.au3> $sFilePath = (@TempDir & "\test.txt") ; Set Path Directory If Not FileExists($sFilePath) Then _FileCreate($sFilePath) ; if file does not exist create file MsgBox(0, "Created", "Yes") ; confirmation file was created $file = FileOpen($sFilePath, 1) ; open file for editing If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Else FileWrite($file, "Server1.domain.local" & @CRLF) ; write 1 line to file FileWrite($file, "Server2.domain.local" & @CRLF) ; write additional line to file FileWrite($file, "Server3.domain.local") ; write additional line to file FileClose($file) ; close open file EndIf Else EndIf Global $sort = False Example() Func Example() ; Read the current script file into an array using the filepath. Local $aArray = FileReadToArray($sFilePath) If @error Then MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file. ; commented out the msgbox code ;~ Else ;~ For $i = 0 To UBound($aArray) - 1 ; Loop through the array. ;~ MsgBox($MB_SYSTEMMODAL, "", $aArray[$i]) ; Display the contents of the array. ;~ Next EndIf ; start gui code local $gui = guicreate('Initial Configuration',270,200) local $lv = GUICtrlCreateListView('Monitored Servers',10,40,160,150) ;,$LVS_REPORT) ;local $getserverbtn = GUICtrlCreatebutton('Get Server',180,50,80,20) local $addbtn = GUICtrlCreateButton("Add", 180, 10, 80, 20) local $removebtn = GUICtrlCreateButton("Remove", 180, 40, 80, 20) local $Input = GUICtrlCreateInput("Type server name to add...", 10,10,160,20) ; this routine reads the array created above and populates the listview for $1 = 0 to UBound($aArray) - 1 GUICtrlCreateListViewItem($aArray[$1],$lv) ConsoleWrite($aArray[$1] & @CRLF) ; added just so i can see the data received into the array _GUICtrlListView_SimpleSort($LV, $Sort, 0, False) next guisetstate() while 1 switch guigetmsg() case $GUI_EVENT_CLOSE guidelete($gui) Exit case $addbtn $file = FileOpen($sFilePath, 1) ; open file for editing FileWrite($file, @CRLF) FileWrite($file, GUICtrlRead($Input)) ; write entry from input box into file FileClose($file) ; close open file GUICtrlCreateListViewItem(guictrlread($input),$lv) _GUICtrlListView_SimpleSort($LV, $Sort, 0, False) case $removebtn ;MsgBox(0,"Remove", "Remove button pushed", 0) Global $success = False $file_name = $sFilePath ; $line_text_input = InputBox("Line's text", "Line must contain following text:", "line contains this text") $line_text_input = (StringTrimRight( _ guictrlread( _ ; get control id of selected item guictrlread($lv)), _ 1) ) ; get listview item text using the control id $file_count_lines = _FileCountLines($file_name) for $i = 0 to $file_count_lines $Lines_text_output = FileReadLine($file_name, $i) if StringInStr($Lines_text_output, $line_text_input) then _FileWriteToLine($file_name, $i, "", 1) $success = True ExitLoop EndIf _GUICtrlListView_DeleteItemsSelected($lv) Next if $success = True Then MsgBox(0, "Success", "Server has been deleted") Else MsgBox(0, "Failure", "Server wasn't found - Case Sensitive") EndIf ;case $getserverbtn ; msgbox( 0,'Selection','You selected Server = ' & stringtrimright( _ ; strip '|' from end of string ; guictrlread( _ ; get control id of selected item ; guictrlread($lv)), _ ; get listview item text using the control id ; 1) ) ; strip 1 char (parm of stringtrimright function ; ; you will generally see this written as "guictrlread(guictrlread($lv))". I broke it apart so I could comment it. ; ; ConsoleWrite(guictrlread(guictrlread($lv)) & @CRLF) ; this is how I would normally write this EndSwitch wend EndFunc ;==>Example The sections amended: case $addbtn $file = FileOpen($sFilePath, 1) ; open file for editing FileWrite($file, @CRLF) FileWrite($file, GUICtrlRead($Input)) ; write entry from input box into file FileClose($file) ; close open file GUICtrlCreateListViewItem(guictrlread($input),$lv) _GUICtrlListView_SimpleSort($LV, $Sort, 0, False) & case $removebtn ;MsgBox(0,"Remove", "Remove button pushed", 0) Global $success = False $file_name = $sFilePath ; $line_text_input = InputBox("Line's text", "Line must contain following text:", "line contains this text") $line_text_input = (StringTrimRight( _ guictrlread( _ ; get control id of selected item guictrlread($lv)), _ 1) ) ; get listview item text using the control id $file_count_lines = _FileCountLines($file_name) for $i = 0 to $file_count_lines $Lines_text_output = FileReadLine($file_name, $i) if StringInStr($Lines_text_output, $line_text_input) then _FileWriteToLine($file_name, $i, "", 1) $success = True ExitLoop EndIf _GUICtrlListView_DeleteItemsSelected($lv) Next if $success = True Then MsgBox(0, "Success", "Server has been deleted") Else MsgBox(0, "Failure", "Server wasn't found - Case Sensitive") EndIf Notice the additional simple sort which would of been my next question but came across it while looking at the delete items from list view area. Are you happy that the code is correct? Spot any improvements? Hopefully that's all my questions, for now anyway ;-) I cant thank you both enough for your help and patience, its very much appreciated. Cheers Dan
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