Sign in to follow this  
Followers 0
Phantomasss

Searching text in .txt file

6 posts in this topic

Hi, there is file (C:\test.txt), in him is written:

text
text
text
xxxx
text
text

ok, I want that AutoIt3 has found the text in file which I shall indicate, in this instance this "xxxx". As this do? I have done:

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

If $file = -1 Then
    MsgBox(0, "Error!", "File not found!")
    Exit
EndIf
    *FileReadLine("xxxx")???*

*FileReadLine("xxxx")???* - that here to write? Thx

Share this post


Link to post
Share on other sites



Check manual for file and string functions

Share this post


Link to post
Share on other sites

I do not know english, wanted that you have shown example of searching for in .txt file. Thx.

Share this post


Link to post
Share on other sites

Hi,

depends on what you want to do if the word is found.

If you just need yes or no, then do a

If StringInStr(FileRead(FileOpen ....)), 'xxxx') <> 0 Then

MsgBox(64, 'Found', 'Found xxxx', 5)

Endif

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hi,

$FTIF = _FindTextInFile("C:\test.txt", "xxxx")
If @error Then
    MsgBox(0, "Error!", "Error Code: " & @error)
ElseIf Not $FTIF Then
    MsgBox(0, "No Match", "Text was not found in file")
Else
    MsgBox(0, "Match", "Text was found in file at line #: " & $FTIF)
EndIf

; #FUNCTION# =====================================================================================
; Name...........: _FindTextInFile
; Description ...: Find text in a text file. (only first instance of text)
; Syntax.........: _FindTextInFile($sFile, $sText[, $iCasesense = 0])
; Parameters ....: $sFile       - Filename of the text file to open.
;                  $sText       - Text to find.
;                  $iCasesense  - Flag to indicate if the operations should be case sensitive.
;                                 0 = Not case sensitive (Default)
;                                 1 = Case sensitive
;                                 2 = Not case sensitive, using a basic/faster comparison
; Return values .: Success      - Return the line number the text was found at (First line is 1).
;                  Failure      - Return 0 and set @error
;                                 @error 1 Text file doesn't exist.
;                                 @error 2 Problem opening the text file.
;                                 @error 3 Problem reading the file.
; ================================================================================================
Func _FindTextInFile($sFile, $sText, $iCasesense = 0)
    Local $FO, $FR, $aStr
    If Not FileExists($sFile) Then Return SetError(1, 0, 0)
    $FO = FileOpen($sFile, 0)
    If $FO = -1 Then Return SetError(2, 0, 0)
    $FR = FileRead($FO)
    If @error > 0 Then
        FileClose($FO)
        Return SetError(3, 0, 0)
    EndIf
    FileClose($FO)
    $aStr = StringSplit(StringStripCR($FR), @LF)
    For $i = 1 To $aStr[0]
        If StringInStr($aStr[$i], $sText, $iCasesense) Then Return $i
    Next
    Return 0
EndFunc

Cheers

Edited by smashly

Share this post


Link to post
Share on other sites

Big Thanks!

Thereby possible to switch off computer having sent ICQ message with telephone :P, .txt file - a history ICQ messager.

Thx!

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