Forum Discussion

joffre's avatar
joffre
Regular Contributor
14 years ago

Count strings in a file



Dim LogFile, Count

Path = "D:\Scripts\Scripts"

PathLogs = "D:\Scripts\Logs\Oracle\Ora10g"

PathLogCreate = "D:\Scripts\Logs\Oracle\Ora10g\CountCreateTable"

Count = 0

Set LogFile = aqFile.OpenTextFile(Path&"\11.ST\Oracle\3.sql", aqFile.faRead, aqFile.ctANSI, false)

Call Log.Message("###############")

Call Log.Message("11.ST:")

aqFile.Create(PathLogCreate&"\teste.txt")

While not LogFile.IsEndOfFile() 

    s = LogFile.ReadLine() 

    If (aqString.Find(s, "CREATE TABLE") >= 0) Then 

        Count = Count + 1 

    End If

WEnd

If Count <> 0 Then

    Call aqFile.WriteToTextFile(PathLogCreate&"\teste.txt", VarToStr(Count), aqFile.ctANSI)

    Call Log.Message("Arquivo 3.sql:")

    Call Log.Message("Número de CREATE TABLE = " + VarToStr(Count))

Else

    Call Log.Message("O arquivo 3.sql não possui CREATE TABLE.")

End If

Call Log.Message("###############")

Call LogFile.Close()




I'm using the code above to count how many times the string "CREATE TABLE" appears in a file. I'm using the same code with a lot of files, and they are all returning to me the correct value, except when looking inside 3 files (this and 2 more), but the code is exactly the same.

In this case, the correct would be 114 found, but it returns to me that just 1 CREATE TABLE was found.



The file is attached.



Ideas?
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Looking at that file attached, it's not a nice, "neat" text file with different lines seperated by carriage return/line feed characters.  If you run something like the following code



    Sub Test2





    Dim file

    set File = aqFile.OpenTextFile("C:\SQLServer\3.SQL", aqFile.faRead, aqFile.ctANSI, false)

    Call Log.Message(File.LinesCount)

    End Sub




    You'll find that TestComplete sees that file as having only 1 line.  So, the code you have will find the first instance of "CREATE TABLE" in that file and, since there is only 1 line, it will not find any more instances.



    You have two things you can do here.



    1)  It looks like the file was not created as a text file but directly from SQL query editor or something like that.  I would suggest creating the file and saving it using some other tool that will create a standard formatted text file with each line seperated by a CR/LF

    2)  You would have to write additional code to go through the line and find all instances on that line of "CREATE TABLE" rather than depending on there being only one instance per line.




  • joffre's avatar
    joffre
    Regular Contributor
    Hi again.



    Robert, which TestComplete's function I can use to open a txt file and see if some string is present?



    Something like this.



    I have a folder with 50 txt files. I need to open each file and look for the string "Not Connected".



    If the string was found, a Log.Error is received. Else, Log.Message is received.
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Okay, you're going to need several objects for this.



    First, to get the files to operate on, you'll need aqFileSystem.FindFiles.  This will return an object that you can then use to iterate through your list of files and operate on them.  You'll need to use the path property of each file to get the file itself because you'll then need to use aqFile.ReadWholeTextFile on each file to get the contents into a string.  You'll then use aqString.Contains to check the contents of each file for your substring.  If it returns a -1, you can assume that the string is not found.  Anything other than a -1 will mean the string is found.



    Does this help?