Sign in to follow this  
Followers 0
quicksilver

Open a Textfile an Search a String

3 posts in this topic

Hi what is the fastest way to open a Textfile and search a string with return .. i need a statement if the string in the file or not.

Can Anyone help me ?

Share this post


Link to post
Share on other sites



Hi what is the fastest way to open a Textfile and search a string with return .. i need a statement if the string in the file or not.

Can Anyone help me ?

If I understood what you are looking for, this should do it.

$file = FileOpen("c:\test.txt", 0)

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

$StringTest = 0
While 1
    $lines = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $result = StringInStr($lines,"STRING TO SEARCH FOR")
    If $result > 0 Then
        $StringTest = 1
    EndIf
Wend

FileClose($file)

If $StringTest = 0 Then
    MsgBox(0,"","String NOT Found")
Else
    MsgBox(0,"","String is Present")
EndIf

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

A simple FileRead and StringInStr will do it quickly:

$file = "C:\boot.ini"
$search = "Windows"

If FileExists($file) Then
    $contents = FileRead($file)
    If @error Then
        MsgBox(0, 'File Error', $file & ' could not be read.')
    Else
        If StringInStr($contents, $search) Then
            MsgBox(0, 'Positive', $file & ' does contain the text "' & $search & '"')
        Else
            MsgBox(0, 'Negative', $file & ' does NOT contain the text "' & $search & '"')
        EndIf
    EndIf
EndIf
Edited by c0deWorm

My UDFs: ExitCodes

Share this post


Link to post
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
Sign in to follow this  
Followers 0