Jump to content

reading a text file


Recommended Posts

I have a .txt file that is about 1000 lines long and is constantly being added to when I am running a 3rd part application. I also have an .ini file that has 3 keys that I want to look for in that .txt file. How would I read in all the data in that text file and then match it to see if my keys (SOME or ALL) are also written in that txt file and if so, display a msgbox() for each one.

I know I need FileRead(), and IniReadSection() maybe? Don't know how to go about this...

Link to comment
Share on other sites

1000 lines is not too long, read it all to a single string, then do a stringinstr search, something like this

$hFile = FileOpen("test.txt", 0)

If $hFile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$sFile = FileRead($hFile)

FileClose($hFile)

Select
    Case StringInStr($sFile, "A") > 0 And StringInStr($sFile, "B") > 0 And StringInStr($sFile, "C") > 0
        MsgBox(0, "", "All flags found")
    Case StringInStr($sFile, "A") > 0
        MsgBox(0, "", "Flag A found")
    Case StringInStr($sFile, "B") > 0
        MsgBox(0, "", "Flag B found")
    Case StringInStr($sFile, "C") > 0
        MsgBox(0, "", "Flag C found")
EndSelect

Edit: Maybe this is even more efficient, didn't test it though :)...

$hFile = FileOpen("test.txt", 0)

If $hFile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$sFile = FileRead($hFile)

FileClose($hFile)

$iFoundA = StringInStr($sFile, "A")
$iFoundB = StringInStr($sFile, "B")
$iFoundC = StringInStr($sFile, "C")

Select
    Case $iFoundA > 0 AND $iFoundB > 0 AND $iFoundC > 0
        MsgBox(0, "", "All flags found")
    Case $iFoundA > 0
        MsgBox(0, "", "Flag A found")
    Case $iFoundB > 0
        MsgBox(0, "", "Flag B found")
    Case $iFoundC > 0
        MsgBox(0, "", "Flag C found")
EndSelect
Edited by KaFu
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...