Jump to content

StringInStr


Recommended Posts

hi guys,

before I continue, I wish to thank Smoke_N for his codes.

i modified his codes a little to suit my needs but it doesn't work, obviously.

would appreciate if someone can help me out here....thanks.

Global $nLastEOF = 0
Global $sTextFound = ""
Global $nMonitorSleep = 500
Global $sFileLocationName = "test.txt"

Dim $array

If FileOpen($sFileLocationName, 0) = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    $sTextFound = _FileMonitorLog($sFileLocationName, $nLastEOF)
    If $sTextFound <> "" Then
        MsgBox(0, "", $sTextFound)
        If StringInStr($sTextFound, "Apple") Then
            $found = 0

            For $x = 0 To UBound($array) - 1
                If $sTextFound == $array[$x] Then
                    $found = 1
                    ExitLoop
                EndIf
            Next
            
            If Not $found Then
                If IsArray($array) Then
                    ReDim $array[UBound($array) + 1]
                Else
                    Dim $array[1]
                EndIf
                $array[UBound($array) - 1] = $sTextFound
            EndIf
            
        EndIf
    EndIf
WEnd

For $x = 0 To UBound($array) - 1
    MsgBox(0, "", $array[$x])
Next

FileClose($sFileLocationName)

Sleep($nMonitorSleep)

Func _FileMonitorLog($sFile, ByRef $nLastEOF)
    Local $aSplit = StringSplit(StringStripCR(FileRead($sFile)), @LF)
    Local $sLog = ""
    If ($aSplit[0] > $nLastEOF) Then
        For $iCC = $nLastEOF + 1 To $aSplit[0]
            $sLog &= $aSplit[$iCC] & @CRLF
        Next
        $nLastEOF = $aSplit[0]
    EndIf
    Return $sLog
EndFunc  ;==>_FileMonitorLog

Even though "Apple" is displayed in the message box, it is not passed on to the next lines in the codes. What I further need to do is this :

1) the whole lines containing the words "Apple" needs to be written in another txt file

2) after the Sleep, when/if the text file is modifed, the search should continue from the last searched line.

Thanks alot.

mouse not found....scroll any mouse to continue.

Link to comment
Share on other sites

Even though "Apple" is displayed in the message box, it is not passed on to the next lines in the codes.

How do you know that? I don't see anything to show that information, or store it anywhere, or even any way to exit the While/wend loop.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

i am refering to line 17. it displays the read text file in a msgbox, if the word "Apple" exists in the text file.

but how to i proceed from there....tats where i am lost....????

mouse not found....scroll any mouse to continue.

Link to comment
Share on other sites

ok i have now understood the error a little...and i have made a few modifications....am i in the right track?

Global $nLastEOF = 0
Global $sTextFound = ""
Global $nMonitorSleep = 500
Global $sFileLocationName = "test.txt"
Dim $array

If FileOpen($sFileLocationName, 0) = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    $sTextFound = _FileMonitorLog($sFileLocationName, $nLastEOF)
    If $sTextFound <> "" Then
        MsgBox(0, "", $sTextFound)
        $pass = StringInStr($sTextFound, "Apple")
        If $pass = True Then
            MsgBox(0, "", "TRUE")
            For $x = 0 To UBound($array) - 1
                If $sTextFound == $array[$x] Then
                    ExitLoop
                EndIf
            Next
        Else
            MsgBox(0, "", "FALSE")
            If IsArray($array) Then
                ReDim $array[UBound($array) + 1]
            Else
                Dim $array[1]
            EndIf
            $array[UBound($array) - 1] = $sTextFound
            ExitLoop
        EndIf
    EndIf
WEnd

For $x = 0 To UBound($array) - 1
    MsgBox(0, "", $array[$x])
Next

FileClose($sFileLocationName)
Sleep($nMonitorSleep)

Func _FileMonitorLog($sFile, ByRef $nLastEOF)
    Local $aSplit = StringSplit(StringStripCR(FileRead($sFile)), @LF)
    Local $sLog = ""
    If ($aSplit[0] > $nLastEOF) Then
        For $iCC = $nLastEOF + 1 To $aSplit[0]
            $sLog &= $aSplit[$iCC] & @CRLF
        Next
        $nLastEOF = $aSplit[0]
    EndIf
    Return $sLog
EndFunc  ;==>_FileMonitorLog

mouse not found....scroll any mouse to continue.

Link to comment
Share on other sites

i am refering to line 17. it displays the read text file in a msgbox, if the word "Apple" exists in the text file.

but how to i proceed from there....tats where i am lost....????

Yes I know it does, and after that the text is saved in the array. Add a line ConsoleWrite("Text added to array" & CRLF) before the line

$array[uBound($array) - 1] = $sTextFound for example.

You just need to have some way of exiting the loop so that you can get to the bit .

For $x = 0 To UBound($array) - 1
    MsgBox(0, "", $array[$x])
Next

Then instead of displaying the array you could write it to a file.

Global $nLastEOF = 0
Global $sTextFound = ""
Global $nMonitorSleep = 500
Global $sFileLocationName = "test.txt"
HotKeySet("{F7}","leaveloop");<-----------------F7 to get out of loop
$ll = False
Dim $array

If FileOpen($sFileLocationName, 0) = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    $sTextFound = _FileMonitorLog($sFileLocationName, $nLastEOF)
    If $sTextFound <> "" Then
        MsgBox(0, "", $sTextFound)
        If StringInStr($sTextFound, "Apple") Then
            $found = 0

            For $x = 0 To UBound($array) - 1
                If $sTextFound == $array[$x] Then
                    $found = 1
                    ExitLoop
                EndIf
            Next
            
            If Not $found Then
                If IsArray($array) Then
                    ReDim $array[UBound($array) + 1]
                Else
                    Dim $array[1]
                EndIf
                ConsoleWrite("add to array" & @CRLF)
                $array[UBound($array) - 1] = $sTextFound
            EndIf
            
        EndIf
    EndIf
    If $ll Then ExitLoop
WEnd

For $x = 0 To UBound($array) - 1
    MsgBox(0, "", $array[$x])
Next

FileClose($sFileLocationName)

Sleep($nMonitorSleep)
Func LeaveLoop()
    $ll = True
EndFunc

Func _FileMonitorLog($sFile, ByRef $nLastEOF)
    Local $aSplit = StringSplit(StringStripCR(FileRead($sFile)), @LF)
    Local $sLog = ""
    If ($aSplit[0] > $nLastEOF) Then
        For $iCC = $nLastEOF + 1 To $aSplit[0]
            $sLog &= $aSplit[$iCC] & @CRLF
        Next
        $nLastEOF = $aSplit[0]
    EndIf
    Return $sLog
EndFunc ;==>_FileMonitorLog
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

oh wow!

yours works fine....thanks.

just another question....

if i write it to the file, what do i need to use?

FileWrite("testing.txt", "X" & @CRLF)

what do i put in the "X" field? I only want to write the whole lines of in which the word "Apple" is. Not the entire modified text file.

thanks once again.

mouse not found....scroll any mouse to continue.

Link to comment
Share on other sites

oh wow!

yours works fine....thanks.

just another question....

if i write it to the file, what do i need to use?

FileWrite("testing.txt", "X" & @CRLF)

what do i put in the "X" field? I only want to write the whole lines of in which the word "Apple" is. Not the entire modified text file.

thanks once again.

Instead of your loop at the end with MsgBox you could have this

For $x = 0 To UBound($array) - 1
    $ans = StringRegExp($array[$x],".*Apple.*",3)
    For $r = 0 To UBound($ans) - 1
        FileWriteLine("testing.txt", $ans[$r])
    Next
    
Next
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

WOW! thanks again!

thanks alot for all the trouble....but i need one LAST FINAL help.

in the above codes scenario, the script detects changes while the script is still running.

how do i modify the codes such that the script runs the check on the text file once and then "remembers" the EOF and then exits the program. when the script is run again, it begins its searches from the last EOF to the current EOF?

thanks alot once more!

edit : i need 2 diff scenarios as i hv 2 diff needs.

Edited by iceberg

mouse not found....scroll any mouse to continue.

Link to comment
Share on other sites

WOW! thanks again!

thanks alot for all the trouble....but i need one LAST FINAL help.

in the above codes scenario, the script detects changes while the script is still running.

how do i modify the codes such that the script runs the check on the text file once and then "remembers" the EOF and then exits the program. when the script is run again, it begins its searches from the last EOF to the current EOF?

When you exit save the value of $nlastEOF to an ini file, and read that when the script starts.

At the start of the script

Global $inifile = "SomeSettings.ini"

;Read the last value recorded. If no value then you will get 0
Global $nlastEOF = Number(IniRead($inifile,"FileData","LastEOF",''))

Then add this function

Func OnAutoItExit();will be executed when AutoIt exits
    IniWrite($inifile,"FileData","LastEOF",$nlastEOF)
;if the ini file does not exist it will be created.
EndFunc

thanks alot once more!

That's OK. Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

thanks a million.

appreciate it alot, Mr Martin!

you hv been very very helpful.

That's fine, glad you're happy. But just martin (or Martin), no Mr. :)
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • Moderators

That's fine, glad you're happy. But just martin (or Martin), no Mr. :)

There you go making those opposite gender remarks again :) .

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

There you go making those opposite gender remarks again :) .

Oh boy, I guess I walked into that one. :)

(or should I say "Oh girl"?)

Well in that case what I meant was, either call me martin or Martin or if you want then Mr Gibson but not "Mr Martin" and not "Gibson" by itself and of course there are a lot of other things I prefer not to be called. But really I don't worry about what people call me. :)

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

hi martin,

i need a little bit of help here once more....the script has been set to sleep for 5 mins. i would like it to be such that when it goes to the sleep function, the testing.txt must have been already written to. so once 5 mins of sleep is up, the script has to read from the LastEOF and then continue again and again until script exit.

currently, it is set to write the texting.txt file only when the script exits. i tried moving the codes around but i only end up in errors after errors.

appreciate your help here once again.

thank you.

Global $nLastEOF = 0
Global $sTextFound = ""
Global $nMonitorSleep = 300000
Global $sFileLocationName = "test.txt"
HotKeySet("{F7}","leaveloop")
$ll = False
Dim $array

If FileOpen($sFileLocationName, 0) = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    $sTextFound = _FileMonitorLog($sFileLocationName, $nLastEOF)
    If $sTextFound <> "" Then
        If StringInStr($sTextFound, "Apple") Then
            $found = 0
            For $x = 0 To UBound($array) - 1
                If $sTextFound == $array[$x] Then
                    $found = 1
                    ExitLoop
                EndIf
            Next
            If Not $found Then
                If IsArray($array) Then
                    ReDim $array[UBound($array) + 1]
                Else
                    Dim $array[1]
                EndIf
                $array[UBound($array) - 1] = $sTextFound
            EndIf
        EndIf
    EndIf
    If $ll Then ExitLoop
WEnd

For $x = 0 To UBound($array) - 1
    $ans = StringRegExp($array[$x],".*Apple.*",3)
    For $r = 0 To UBound($ans) - 1
        FileWriteLine("testing.txt", $ans[$r] & @CRLF);file must exists first
    Next
Next

FileClose($sFileLocationName)
Sleep($nMonitorSleep)

Func LeaveLoop()
    $ll = True
EndFunc

Func _FileMonitorLog($sFile, ByRef $nLastEOF)
    Local $aSplit = StringSplit(StringStripCR(FileRead($sFile)), @LF)
    Local $sLog = ""
    If ($aSplit[0] > $nLastEOF) Then
        For $iCC = $nLastEOF + 1 To $aSplit[0]
            $sLog &= $aSplit[$iCC] & @CRLF
        Next
        $nLastEOF = $aSplit[0]
    EndIf
    Return $sLog
EndFunc

mouse not found....scroll any mouse to continue.

Link to comment
Share on other sites

hi martin,

i need a little bit of help here once more....the script has been set to sleep for 5 mins. i would like it to be such that when it goes to the sleep function, the testing.txt must have been already written to. so once 5 mins of sleep is up, the script has to read from the LastEOF and then continue again and again until script exit.

currently, it is set to write the texting.txt file only when the script exits. i tried moving the codes around but i only end up in errors after errors.

appreciate your help here once again.

thank you.

Global $nLastEOF = 0
Global $sTextFound = ""
Global $nMonitorSleep = 300000
Global $sFileLocationName = "test.txt"
HotKeySet("{F7}","leaveloop")
$ll = False
Dim $array

If FileOpen($sFileLocationName, 0) = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    $sTextFound = _FileMonitorLog($sFileLocationName, $nLastEOF)
    If $sTextFound <> "" Then
        If StringInStr($sTextFound, "Apple") Then
            $found = 0
            For $x = 0 To UBound($array) - 1
                If $sTextFound == $array[$x] Then
                    $found = 1
                    ExitLoop
                EndIf
            Next
            If Not $found Then
                If IsArray($array) Then
                    ReDim $array[UBound($array) + 1]
                Else
                    Dim $array[1]
                EndIf
                $array[UBound($array) - 1] = $sTextFound
            EndIf
        EndIf
    EndIf
    If $ll Then ExitLoop
WEnd

For $x = 0 To UBound($array) - 1
    $ans = StringRegExp($array[$x],".*Apple.*",3)
    For $r = 0 To UBound($ans) - 1
        FileWriteLine("testing.txt", $ans[$r] & @CRLF);file must exists first
    Next
Next

FileClose($sFileLocationName)
Sleep($nMonitorSleep)

Func LeaveLoop()
    $ll = True
EndFunc

Func _FileMonitorLog($sFile, ByRef $nLastEOF)
    Local $aSplit = StringSplit(StringStripCR(FileRead($sFile)), @LF)
    Local $sLog = ""
    If ($aSplit[0] > $nLastEOF) Then
        For $iCC = $nLastEOF + 1 To $aSplit[0]
            $sLog &= $aSplit[$iCC] & @CRLF
        Next
        $nLastEOF = $aSplit[0]
    EndIf
    Return $sLog
EndFunc
Maybe you need something like this but I haven't tested it

Global $nLastEOF = 0
Global $sTextFound = ""
Global $nMonitorSleep = 300000
Global $sFileLocationName = "test.txt"
HotKeySet("{F7}", "LeaveLoop")
HotKeySet("{F8}", "endall")
Global $ll, $kk = False
Global $work = True
Global $inifile = "SomeSettings.ini"

;Read the last value recorded. If no value then you will get 0
Global $nlastEOF
While 1
    
    If $work Then
        $work = False
        MonitorChanges ()
        AdlibEnable("setgoing", $nMonitorSleep)
    EndIf
    If $kk Then ExitLoop
    Sleep(100)
    
WEnd

Func setgoing()
    $work = True
    AdlibDisable()
EndFunc  ;==>setgoing

Func LeaveLoop()
    $ll = True
EndFunc  ;==>LeaveLoop

Func endall()
    $ll = True
    $kk = True
EndFunc  ;==>endall


Func MonitorChanges()
    Dim $array
    $nlastEOF = Number(IniRead($inifile,"FileData","LastEOF",''))
    $ll = false
    
    If FileOpen($sFileLocationName, 0) = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf
    
    While 1
        $sTextFound = _FileMonitorLog($sFileLocationName, $nLastEOF)
        If $sTextFound <> "" Then
            If StringInStr($sTextFound, "Apple") Then
                $found = 0
                For $x = 0 To UBound($array) - 1
                    If $sTextFound == $array[$x] Then
                        $found = 1
                        ExitLoop
                    EndIf
                Next
                If Not $found Then
                    If IsArray($array) Then
                        ReDim $array[UBound($array) + 1]
                    Else
                        Dim $array[1]
                    EndIf
                    $array[UBound($array) - 1] = $sTextFound
                EndIf
            EndIf
        EndIf
        If $ll Then ExitLoop
    WEnd
    FileClose($sFileLocationName)
    AppendText($array)
    IniWrite($inifile,"FileData","LastEOF",$nlastEOF)
EndFunc  ;==>MonitorCahnges

Func AppendText($aT)
    For $x = 0 To UBound($aT) - 1
        $ans = StringRegExp($aT[$x], ".*Apple.*", 3)
        For $r = 0 To UBound($ans) - 1
            FileWriteLine("testing.txt", $ans[$r] & @CRLF);file must exists first
        Next
    Next
    
EndFunc  ;==>AppendText





Func _FileMonitorLog($sFile, ByRef $nLastEOF)
    Local $aSplit = StringSplit(StringStripCR(FileRead($sFile)), @LF)
    Local $sLog = ""
    If ($aSplit[0] > $nLastEOF) Then
        For $iCC = $nLastEOF + 1 To $aSplit[0]
            $sLog &= $aSplit[$iCC] & @CRLF
        Next
        $nLastEOF = $aSplit[0]
    EndIf
    Return $sLog
EndFunc  ;==>_FileMonitorLog
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

hi martin,

the codes work perfectly if i amend the following:

1) disable the while loop in the MonitorChanges()

2) move dim $array to the start of the script

thanks alot once again.

mouse not found....scroll any mouse to continue.

Link to comment
Share on other sites

  • 3 weeks later...

hi martin,

sorry...just realised that the Func AppendText($aT) copies the whole lines of the documents with the matching word....not the new ones.

pls help once again.

mouse not found....scroll any mouse to continue.

Link to comment
Share on other sites

hi martin,

sorry...just realised that the Func AppendText($aT) copies the whole lines of the documents with the matching word....not the new ones.

pls help once again.

I have to go now, if no-one else helps I'll be back within the next 24 hours.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

ok let me rephrase my queries again....

if i quit the script, only then does the output file gets written.

i tried modifying the codes to such that it just keeps updating the added lines to the output file while the screen is still running.

and what did i get.....it just starts copying everything all over again....not adding to the already existing ones. maybe its too confusing....i will brief it below

expected results while script is running:

line 1

line 2

line 3

when the text file is added with new lines it should be like this:

line 1

line 2

line 3

line 4

line 5

but what i am getting is this

line 1

line 2

line 3

line 1

line 2

line 3

line 4

line 5

hope this is clear to help me out here. thanks.

mouse not found....scroll any mouse to continue.

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