Jump to content

read listView Items and check in text file,replace if found (SOLVED)


Recommended Posts

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 by abdulrahmanok
Link to comment
Share on other sites

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 by AutoBert
Link to comment
Share on other sites

After Searching I did most of working :

#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.

Link to comment
Share on other sites

AB,

This will detect a single click on a listview item...

#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 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

Link to comment
Share on other sites

Ty Very much @kylomas I hope you get the best in your life o:)

Finally This Nightmare had finished , This is Final Code :

#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.

 

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...