Jump to content

Recommended Posts

Posted

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?

Posted

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

Posted

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

Posted

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

Posted (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 by storme
Posted

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...