abdulrahmanok Posted November 4, 2016 Posted November 4, 2016 (edited) Hello Brothers, Firstly this is Contains of : Test.txt File : <TR> <TD CLASS="dddefault"> <SELECT NAME="selected_day"> <OPTION VALUE="14-OCT-16">14-OCT-16 <OPTION VALUE="13-OCT-16">13-OCT-16 <OPTION VALUE="12-OCT-16">12-OCT-16 <OPTION VALUE="11-OCT-16">11-OCT-16 <OPTION VALUE="10-OCT-16">10-OCT-16 </SELECT> </TR> And This Code Reading Dates from text file to array and simply add this dates to Listview : #include <GUIConstantsEx.au3> #include <GuiListBox.au3> Local $nMsg = GUICreate("Example") GUISetState(@SW_SHOW, $nMsg) $ListviewBannerDate = GUICtrlCreatelistview("My Banner Dates | |",200,150,160,150,-1) Local $sFilePath = @ScriptDir & "\test.txt" ; Read the current script file into an array using the filepath. Local $aDate = FileReadToArray($sFilePath) If @error Then MsgBox(0, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file. Else For $i = 0 To UBound($aDate) - 1 ; Loop through the array. $iString = StringInStr($aDate[$i], "OPTION VALUE") $sMyString = StringMid($aDate[$i], 16, 9) If $iString > 1 Then Local $cr1 = GUICtrlCreateListViewItem($sMyString, $ListviewBannerDate) Else ContinueLoop EndIf Next EndIf while 1 Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch Wend What I Want : 1- Check which Listview item selected and searching for selected in Test.txt File. 2- If found Then Replace founded value with selected value from List View, If not found don't do anything... Edited November 5, 2016 by abdulrahmanok
AutoBert Posted November 4, 2016 Posted November 4, 2016 (edited) 2 hours ago, abdulrahmanok said: What I Want : 1- Check which Listview item selected and searching for selected in Test.txt File. 2- If found Then Replace founded value with selected value from List View, If not found don't do anything... When LV-Item is found in test.txt (1) then value in test.txt is already same as in LV-Item and there is no replacement needed. Edited November 4, 2016 by AutoBert
abdulrahmanok Posted November 4, 2016 Author Posted November 4, 2016 1 hour ago, AutoBert said: When LV-Item is found in test.txt (1) then value in test.txt is already same as in LV-Item and there is no replacement needed. I know but i need this because i will detect and change the line after this founded Value
abdulrahmanok Posted November 4, 2016 Author Posted November 4, 2016 After Searching I did most of working : expandcollapse popup#include <File.au3> #include <array.au3> Local $sFileName = "test.txt" Local $searchStr = "14-OCT-16" $aResult = _LineNumsOfSearchStr($sFileName, $searchStr, False) _ArrayDisplay($aResult) ; Returns array of found line numbers that contain $searchStr contents. ; Optional delete found line from file. Func _LineNumsOfSearchStr($sFileName, $searchString, $bDeleteLine = False) Local $location, $aCurrentLineNum, $iCurrentLineNum, $sFile, $iOccur = 1, $sRes = "" If FileExists($sFileName) = 0 Then Return 1 Do $sFile = FileRead($sFileName) $location = StringInStr($sFile, $searchString, 0, $iOccur) ; Find the $iOccur occurrence of the "substring" If $location > 0 Then $aCurrentLineNum = StringRegExp(StringRegExpReplace($sFile, "(?s)(.{" & $location & "})(.*)$", "\1"), "(?s)(\v+)", 3) ;Find line number $iCurrentLineNum = UBound($aCurrentLineNum) + 1 ; Line number MsgBox(0,"",$searchStr) MsgBox(0,"",$iCurrentLineNum) _FileWriteToLine(@ScriptDir&"\test.txt", $iCurrentLineNum+1, "my replacement for line +1 ", 1) ;ConsoleWrite("CharPos: " & $location & " Ln: " & $iCurrentLineNum & @CRLF) $sRes &= $iCurrentLineNum & "|" If $bDeleteLine Then _FileWriteToLine($sFileName, $iCurrentLineNum, "", 1) ; Remove found line from file. Else $iOccur += 1 EndIf Else ExitLoop EndIf Sleep(10) Until 0 ;ShellExecute($sFileName) Return StringSplit(StringTrimRight($sRes, 1), "|") EndFunc ;==>_LineNumsOfSearchStr Now It Searching for some Value and Add line After this Founded Value Still need : Check which Listview item selected and searching for selected in Test.txt File.
kylomas Posted November 5, 2016 Posted November 5, 2016 (edited) AB, This will detect a single click on a listview item... expandcollapse popup#include <GUIConstantsEx.au3> #include <GuiListBox.au3> #include <WindowsConstants.au3> #include <GuiListView.au3> Local $hGui = GUICreate("Example") $ListviewBannerDate = GUICtrlCreateListView("My Banner Dates", 200, 150, 160, 150, -1) ; dummy control actioned by notify routine when user clicks on control $dummy_lv = GUICtrlCreateDummy() GUISetState(@SW_SHOW) ; routine to recieve notification messages from listview GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") Local $sFilePath = @ScriptDir & "\test.txt" ; Read the current script file into an array using the filepath. Local $aDate = FileReadToArray($sFilePath) If @error Then MsgBox(0, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file. Else For $i = 0 To UBound($aDate) - 1 ; Loop through the array. $iString = StringInStr($aDate[$i], "OPTION VALUE") $sMyString = StringMid($aDate[$i], 16, 9) If $iString > 1 Then GUICtrlCreateListViewItem($sMyString, $ListviewBannerDate) Else ContinueLoop EndIf Next EndIf Local $msg While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE Exit Case $dummy_lv ; ; this is where you would do your search and replace routine ; ConsoleWrite(_GUICtrlListView_GetItemTextString($ListviewBannerDate, -1) & @LF) EndSwitch WEnd Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) Switch $tNMHDR.IDFrom Case $ListviewBannerDate Switch $tNMHDR.Code Case $nm_click GUICtrlSendToDummy($dummy_lv) ; action dummy control in the message loop EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_NOTIFY kylomas Edited November 5, 2016 by kylomas AutoBert and abdulrahmanok 2 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
abdulrahmanok Posted November 5, 2016 Author Posted November 5, 2016 Ty Very much @kylomas I hope you get the best in your life Finally This Nightmare had finished , This is Final Code : expandcollapse popup#include <GUIConstantsEx.au3> #include <GuiListBox.au3> #include <WindowsConstants.au3> #include <GuiListView.au3> #include <File.au3> Local $hGui = GUICreate("Example") $ListviewBannerDate = GUICtrlCreateListView("My Banner Dates", 200, 150, 160, 150, -1) ; dummy control actioned by notify routine when user clicks on control $dummy_lv = GUICtrlCreateDummy() GUISetState(@SW_SHOW) ; routine to recieve notification messages from listview GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") Local $sFilePath = @ScriptDir & "\test.txt" ; Read the current script file into an array using the filepath. Local $aDate = FileReadToArray($sFilePath) If @error Then MsgBox(0, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file. Else For $i = 0 To UBound($aDate) - 1 ; Loop through the array. $iString = StringInStr($aDate[$i], "OPTION VALUE") $sMyString = StringMid($aDate[$i], 16, 9) If $iString > 1 Then GUICtrlCreateListViewItem($sMyString, $ListviewBannerDate) Else ContinueLoop EndIf Next EndIf Local $msg While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE Exit Case $dummy_lv ; ; this is where you would do your search and replace routine $asd= _GUICtrlListView_GetItemTextString($ListviewBannerDate, -1) ; If $asd ="" Then MsgBox(0,"sdfsd","") Else ConsoleWrite(_GUICtrlListView_GetItemTextString($ListviewBannerDate, -1) & @LF) ExitLoop EndIf EndSwitch WEnd Local $sFileName = "test.txt" Local $searchStr = $asd $aResult = _LineNumsOfSearchStr($sFileName, $searchStr, False) _ArrayDisplay($aResult) ; Returns array of found line numbers that contain $searchStr contents. ; Optional delete found line from file. Func _LineNumsOfSearchStr($sFileName, $searchString, $bDeleteLine = False) Local $location, $aCurrentLineNum, $iCurrentLineNum, $sFile, $iOccur = 1, $sRes = "" If FileExists($sFileName) = 0 Then Return 1 Do $sFile = FileRead($sFileName) $location = StringInStr($sFile, $searchString, 0, $iOccur) ; Find the $iOccur occurrence of the "substring" If $location > 0 Then $aCurrentLineNum = StringRegExp(StringRegExpReplace($sFile, "(?s)(.{" & $location & "})(.*)$", "\1"), "(?s)(\v+)", 3) ;Find line number $iCurrentLineNum = UBound($aCurrentLineNum) + 1 ; Line number MsgBox(0,"",$searchStr) MsgBox(0,"",$iCurrentLineNum) _FileWriteToLine(@ScriptDir&"\test.txt", $iCurrentLineNum+1, "my replacement for line 3", 1) ;ConsoleWrite("CharPos: " & $location & " Ln: " & $iCurrentLineNum & @CRLF) $sRes &= $iCurrentLineNum & "|" If $bDeleteLine Then _FileWriteToLine($sFileName, $iCurrentLineNum, "", 1) ; Remove found line from file. Else $iOccur += 1 EndIf Else ExitLoop EndIf Sleep(10) Until 0 ;ShellExecute($sFileName) Return StringSplit(StringTrimRight($sRes, 1), "|") EndFunc ;==>_LineNumsOfSearchStr Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) Switch $tNMHDR.IDFrom Case $ListviewBannerDate Switch $tNMHDR.Code Case $nm_click GUICtrlSendToDummy($dummy_lv) ; action dummy control in the message loop EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_NOTIFY its Search For Selected value and write line after it.
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