syno Posted April 30, 2009 Posted April 30, 2009 Hi guys I am currently writing a script that will read each line of a text file and then output the results to say if each line is valid or not, the code is shown below: Local $line Local $find1 = "1" ,$find2 = "2" ,$find3 = "3" For $i = 1 To _FileCountLines("C:\test.txt") $line = FileReadLine("C:\test.txt", $i) If StringRegExp($line, $find1) Or StringRegExp($line, $find2) Or StringRegExp($line, $find3) Then ConsoleWrite("Check passed successfully at line " & $i & @CRLF) Else MsgBox(0, "ERROR", "An erronous value was found at line " & $i & @CRLF) EndIf Next However if my text file, ie test.txt has "1" on the first line "1" on the second line and "1" on the third line I get the resulting output: Check passed successfully at line 1 Check passed successfully at line 2 Check passed successfully at line 3 Where infact the correct response would be: Check passed successfully at line 1 An erronous value was found at line 2 An erronous value was found at line 3 I have tried replacing OR with AND in my IF function but the output is; An erronous value was found at line 1, 2 and 3. Can anybody see what I am doing wrong, basically every line in test.txt needs to be unique, do I need to incorporate some kind of control parameter at some point?
Valuater Posted April 30, 2009 Posted April 30, 2009 place StringInStr($line, $find1) to replace StringRegExp($line, $find1) replace all reg express 8)
syno Posted April 30, 2009 Author Posted April 30, 2009 place StringInStr($line, $find1) to replace StringRegExp($line, $find1) replace all reg express 8) OK but what if I had: Local $line Local $find1 = "\b1\b" ,$find2 = "\b2\b" ,$find3 = "\b3\b" For $i = 1 To _FileCountLines("C:\test.txt") $line = FileReadLine("C:\test.txt", $i) If StringRegExp($line, $find1) Or StringRegExp($line, $find2) Or StringRegExp($line, $find3) Then ConsoleWrite("Check passed successfully at line " & $i & @CRLF) Else MsgBox(0, "ERROR", "An erronous value was found at line " & $i & @CRLF) EndIf Next In the above example I would need StringRegExp... Thanks
syno Posted May 1, 2009 Author Posted May 1, 2009 Hi I have also tried StringInStr but I get the same result. Is there anybody out there who could possibly help me with this? Thank you
WideBoyDixon Posted May 1, 2009 Posted May 1, 2009 I think I must be missing something here but: If StringRegExp($line, $find1) Or StringRegExp($line, $find2) Or StringRegExp($line, $find3) Then This line is always going to be true if test.txt has a "1" on each line as you say. StringRegExp($line, $find1) is always true. WBD [center]Wide by name, Wide by nature and Wide by girth[u]Scripts[/u]{Hot Folders} {Screen Calipers} {Screen Crosshairs} {Cross-Process Subclassing} {GDI+ Clock} {ASCII Art Signatures}{Another GDI+ Clock} {Desktop Goldfish} {Game of Life} {3D Pie Chart} {Stock Tracker}[u]UDFs[/u]{_FileReplaceText} {_ArrayCompare} {_ToBase}~ My Scripts On Google Code ~[/center]
storme Posted May 1, 2009 Posted May 1, 2009 (edited) G'day Syno I'm afraid the script is doing exactly what you asked it to do. If StringRegExp($line, $find1) Or StringRegExp($line, $find2) Or StringRegExp($line, $find3) Then ConsoleWrite("Check passed successfully at line " & $i & @CRLF) [\code] Will match any line that contains $find1, $find2 or $find3 So in your demo file it will match all the '1' lines or '2' or '3' lines eg ALL lines are valid if they contain 1 or 2 or 3. I'm not sure what your requirments are to what is "valid" or not but if you know what each line should contain you could use code like this #include <File.au3> Local $line Local $find[3] = ["1","2","3"] For $i = 1 To _FileCountLines("C:\test.txt") $line = FileReadLine("C:\test.txt", $i) If StringRegExp($line, $find[$i-1]) Then ConsoleWrite("Check passed successfully at line " & $i & @CRLF) Else MsgBox(0, "ERROR", "An erronous value was found at line " & $i & @CRLF) EndIf Next This code will ONLY validate files containing 3 lines (the size of the $find array). Hope that helps Good Luck John Morrison Edited May 1, 2009 by storme Some of my small contributions to AutoIt Browse for Folder Dialog - Automation SysTreeView32 | FileHippo Download and/or retrieve program information | Get installedpath from uninstall key in registry | RoboCopy function John Morrison aka Storm-E
syno Posted May 1, 2009 Author Posted May 1, 2009 G'day Syno I'm afraid the script is doing exactly what you asked it to do. If StringRegExp($line, $find1) Or StringRegExp($line, $find2) Or StringRegExp($line, $find3) Then ConsoleWrite("Check passed successfully at line " & $i & @CRLF) [\code] Will match any line that contains $find1, $find2 or $find3 So in your demo file it will match all the '1' lines or '2' or '3' lines eg ALL lines are valid if they contain 1 or 2 or 3. I'm not sure what your requirments are to what is "valid" or not but if you know what each line should contain you could use code like this #include <File.au3> Local $line Local $find[3] = ["1","2","3"] For $i = 1 To _FileCountLines("C:\test.txt") $line = FileReadLine("C:\test.txt", $i) If StringRegExp($line, $find[$i-1]) Then ConsoleWrite("Check passed successfully at line " & $i & @CRLF) Else MsgBox(0, "ERROR", "An erronous value was found at line " & $i & @CRLF) EndIf Next This code will ONLY validate files containing 3 lines (the size of the $find array). Hope that helps Good Luck John Morrison Thats magic, thanks very much for your help
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