Jump to content

Search for a string in a txt file


Recommended Posts

Told you I'd be back

I'm nearly there I just cant get my head 'round these new loops,

so I am looking for a string in a text log file like "error" and net send a message if found

I'm trying to do it with the filereadline to read each line in turn and search for the string error in each line. Pretty ungraceful I know but I guess I'm still in autoit V2 mode.

#include <file.au3>

$CountLines = _FileCountLines("C:\bkuplogs\mostrecent.txt")

MsgBox(64, "LOG","There are " & $CountLines & " Lines in the file")

$i=1

;

;

$file=fileopen("C:\bkuplogs\mostrecent.txt",0)

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

MsgBox(0, "Line read:", $line); so far so good

If $result=stringinstr($line,"status") then run(@comspec "/c" & net send USER skipped found in file

Elseif $result=stringinstr($line,"error") then run(@comspec "/c" & net send USER error found in file

Elseif $result=stringinstr($line,"No media") then run(@comspec "/c" & net send USER error found in file

endif

;...Etc. for lots of different different stings to search for

Wend

FileClose($file)

the elseif's are looking for a coresponding IF statement, any ideas I think a case statement might work but I've no idea how to get it working

Any help appreciated. Thanks

Link to comment
Share on other sites

  • Developers

If $result = StringInStr($line, "status") Then
    Run(@ComSpec "/c net send USER skipped found in file")
ElseIf $result = StringInStr($line, "error") Then
    Run(@ComSpec "/c net send USER error found in file ")
ElseIf $result = StringInStr($line, "No media") Then
    Run(@ComSpec "/c net send USER error found in file")
EndIf

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

the elseif's are looking for a coresponding IF statement, any ideas I think a case statement might work but I've no idea how to get it working

<{POST_SNAPBACK}>

Like JdeB said plus you need to remove the "$result = ", add "&" after @Comspec, and a space before "/c".

If StringInStr($line, "status") Then
   Run(@ComSpec & " /c net send USER 'skipped found in file'")
ElseIf StringInStr($line, "error") Then
   Run(@ComSpec & " /c net send USER 'error found in file'")
ElseIf StringInStr($line, "No media") Then
   Run(@ComSpec & " /c net send USER 'error found in file'")
EndIf

Phillip

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