Sign in to follow this  
Followers 0
hacksawbob

Search for a string in a txt file

4 posts in this topic

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

Share this post


Link to post
Share on other sites



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


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Thanks for your help struggling to get my head round the new syntax, it requires thought which I'm not used to!

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