Sign in to follow this  
Followers 0
Eviltim

log check

8 posts in this topic

is there a way to check the last line of a text document for a particular phrase?

Share this post


Link to post
Share on other sites

is there a way to check the last line of a text document for a particular phrase?

<{POST_SNAPBACK}>

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
$line = ""
$line_temp = ""
While 1
    $line = FileReadLine($file)
    If @error = -1 Then
        ExitLoop
    Else
        $line_temp = $line
    EndIf
;MsgBox(0, "Line read:", $line)
Wend

FileClose($file)
;MsgBox(0, "Line read:", $line_temp)
$line_temp should have the last line.

Now go read about StringInStr in the help file...


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Yea that definetly worked but the last line of the file is always blank :dance:

searching through the help file i found _FileCountLines...

it returns the last line number without counting the final @LF

#include <file.au3>
$file = "C:\Temp\Command.txt"
Do
$i = _FileCountLines($file)
$string = FileReadLine($file, $i)
$string = StringTrimLeft($string, 8)
sleep(5000)
Until $string = "Close"

now im looking for a way to search the string for the phrase instead of trimming the info from the left. :whistle:

EDIT: Oh, herewasplato, i got the pm. i dont mind at all, thanks for all your help.

Edited by Eviltim

Share this post


Link to post
Share on other sites

Plato told you how

Now go read about StringInStr in the help file...

<{POST_SNAPBACK}>

8)

NEWHeader1.png

Share this post


Link to post
Share on other sites

Plato told you how

8)

<{POST_SNAPBACK}>

oh I did several stupid things:

posted untested code

later thought of a problem with that code

went to correct the code

noticed the Eviltim was reading my bad code

deleted my post - but probably too late and wasted Eviltim's time

hastely added the $line_temp = $line stuff

posted what you see above

(my original code only used $line which ends in EOF?)

@Eviltim,

as my penance, try this:

#include <file.au3>
$file = "C:\Temp\Command.txt"
While 1
    $i = _FileCountLines($file)
    $string = FileReadLine($file, $i)
    If StringInStr ($string, "Close") Then ExitLoop
    Sleep(5000)
WEnd

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

oh I did several stupid things:

posted untested code

later thought of a problem with that code

went to correct the code

noticed the Eviltim was reading my bad code

deleted my post - but probably too late and wasted Eviltim's time

hastely added the $line_temp = $line stuff

posted what you see above

(my original code only used $line which ends in EOF?)

@Eviltim,

as my penance, try this:

#include <file.au3>
$file = "C:\Temp\Command.txt"
While 1
    $i = _FileCountLines($file)
    $string = FileReadLine($file, $i)
    If StringInStr ($string, "Close") Then ExitLoop
    Sleep(5000)
WEnd

<{POST_SNAPBACK}>

Thanks, one last question.

Is it possible to add more StringInStr Lines to it?

i tried this.

While 1
    $i = _FileCountLines($file)
    $string = FileReadLine($file, $i)
    If StringInStr ($string, "FTP_Connect", 1) Then
    $command = "FTP_Start"
    ExitLoop
    If StringInStr ($string, "FTP_Stop", 1) Then
    $command = "FTP_Stop"
    ExitLoop
    If StringInStr ($string, "FTP_Help", 1) Then
    $command = "FTP_Help"
    ExitLoop
    If StringInStr ($string, "FTP_Pause", 1) Then
    $command = "FTP_Pause"
    ExitLoop
    If StringInStr ($string, "FTP_Report On", 1) Then
    $command = "FTP_Report On"
    ExitLoop
    If StringInStr ($string, "FTP_Report Off", 1) Then
    $command = "FTP_Report Off"
    ExitLoop
    Sleep(5000)
WEnd

i get a "wend" statement with no matching "while" statement error

i think its just bad formatting on my part.

Share this post


Link to post
Share on other sites

like this

#include <file.au3>
Dim $file

While 1
    $i = _FileCountLines($file)
    $string = FileReadLine($file, $i)
    If StringInStr ($string, "FTP_Connect", 1) Then
    $command = "FTP_Start"
    ExitLoop
    EndIf
    If StringInStr ($string, "FTP_Stop", 1) Then
    $command = "FTP_Stop"
    ExitLoop
    EndIf
    If StringInStr ($string, "FTP_Help", 1) Then
    $command = "FTP_Help"
    ExitLoop
    EndIf
    If StringInStr ($string, "FTP_Pause", 1) Then
    $command = "FTP_Pause"
    ExitLoop
    EndIf
    If StringInStr ($string, "FTP_Report On", 1) Then
    $command = "FTP_Report On"
    ExitLoop
    EndIf
    If StringInStr ($string, "FTP_Report Off", 1) Then
    $command = "FTP_Report Off"
    ExitLoop
    EndIf
    Sleep(5000)
WEnd

hope that helps

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

like this

#include <file.au3>
Dim $file

While 1
    $i = _FileCountLines($file)
    $string = FileReadLine($file, $i)
    If StringInStr ($string, "FTP_Connect", 1) Then
    $command = "FTP_Start"
    ExitLoop
    EndIf
    If StringInStr ($string, "FTP_Stop", 1) Then
    $command = "FTP_Stop"
    ExitLoop
    EndIf
    If StringInStr ($string, "FTP_Help", 1) Then
    $command = "FTP_Help"
    ExitLoop
    EndIf
    If StringInStr ($string, "FTP_Pause", 1) Then
    $command = "FTP_Pause"
    ExitLoop
    EndIf
    If StringInStr ($string, "FTP_Report On", 1) Then
    $command = "FTP_Report On"
    ExitLoop
    EndIf
    If StringInStr ($string, "FTP_Report Off", 1) Then
    $command = "FTP_Report Off"
    ExitLoop
    EndIf
    Sleep(5000)
WEnd

hope that helps

8)

<{POST_SNAPBACK}>

Thanks! works great!

Also noticed another typo error i made in this script.

If StringInStr ($string, "FTP_Connect", 1) Then

$command = "FTP_Start" -- (Should be FTP_Connect)

Share this post


Link to post
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
Sign in to follow this  
Followers 0