umairrajan Posted March 25, 2015 Share Posted March 25, 2015 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 More sharing options...
JohnOne Posted March 25, 2015 Share Posted March 25, 2015 #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 More sharing options...
umairrajan Posted March 25, 2015 Author Share Posted March 25, 2015 you are a saviour john. thanks Link to comment Share on other sites More sharing options...
JohnOne Posted March 25, 2015 Share Posted March 25, 2015 (edited) 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 March 25, 2015 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 More sharing options...
SadBunny Posted March 25, 2015 Share Posted March 25, 2015 (edited) 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 March 25, 2015 by SadBunny Roses are FF0000, violets are 0000FF... All my base are belong to you. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now