Jump to content

searching for a phrase in a txt file


Recommended Posts

hi, this is probably a noob question but i did write a script to search for a text in a .txt file but it seems to be not working and i cant seem to figure out why any help would be appretiated

 
#include <File.au3>
Dim $array
 
$file = @ScriptDir & 'DEBUG.log.txt'
$find = 'Unable to Log in: Invalid Login or PIN'
 
If $file <> '' Then
_FileReadToArray($file, $array)
For $i = 1 To UBound($array) - 1
if StringInStr($array[$i], $find) Then
MsgBox(64, 'Failed', 'Login Failed')
Else
MsgBox(64, 'Passed','Login Succesfull')
EndIf
Next
Else
MsgBox(64, 'Failed','Empty file')
EndIf
 
the file contains the $find phrase but the script still returns Login Succesfull and it turns it into an infinite loop.
 
Link to comment
Share on other sites

#include <File.au3>
Dim $array

$file = @ScriptDir & '\DEBUG.log.txt'
$find = 'Unable to Log in: Invalid Login or PIN'
$flag = 0
If $file <> '' Then
    _FileReadToArray($file, $array)
    For $i = 1 To UBound($array) - 1
        If StringInStr($array[$i], $find) Then
            $flag = 1
            ExitLoop
        EndIf
    Next
    If $flag Then
        MsgBox(64, 'Failed', 'Login Failed')
    Else
        MsgBox(64, 'Win', 'Login')
    EndIf
Else
    MsgBox(64, 'Failed', 'Empty file')
EndIf

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I'm not completely certain why you use an array or a loop though.

$file = @ScriptDir & '\DEBUG.log.txt'
$find = 'Unable to Log in: Invalid Login or PIN'

If $file <> '' Then
    If StringInStr(FileRead($file), $find) Then
        MsgBox(64, 'Failed', 'Login Failed')
    Else
        MsgBox(64, 'Win', 'Login')
    EndIf
Else
    MsgBox(64, 'Failed', 'Empty file')
EndIf
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Also, if you define $file like that, the outer else block is unreachable code (because $file will never equal an empty string), so the whole outer if/else/endif can be removed.
 

$file = @ScriptDir & '\DEBUG.log.txt'
$find = 'Unable to Log in: Invalid Login or PIN'

If StringInStr(FileRead($file), $find) Then
    MsgBox(64, 'Failed', 'Login Failed')
Else
    MsgBox(64, 'Win', 'Login')
EndIf

... then again... reading the error message "Empty file", maybe the idea was to display this when the actual file was empty instead of the variable? How about some error checking (untested):

$file = @ScriptDir & '\DEBUG.log.txt'
$find = 'Unable to Log in: Invalid Login or PIN'

$fileContent = FileRead($file)

If @error Then
    MsgBox(16, 'Failed', 'Failed to read from file ' & $file & ". Does the file exist and can it be opened for reading?")
Else If $fileContent == ''
    MsgBox(16, 'Failed', 'No data read from file ' & $file & ". Is the file empty?")
Else If StringInStr($fileContent, $find) Then
    MsgBox(16, 'Failed', 'Login Failed')
Else
    MsgBox(64, 'Win', 'Login')
EndIf
Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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