Sign in to follow this  
Followers 0
broodplank

_StringSearchInFile

5 posts in this topic

#1 ·  Posted (edited)

can be handy, output is:

---------- FILE

[LINE NUMBER]STRING

Usage:

_StringSearchInFile("Text File", "String to search")

Func:

#include <Process.au3>

$SEARCH = _StringSearchInFile("C:\lol.txt", "lol")

Func _StringSearchInFile($file, $qry)
FileDelete(@HomeDrive&"\results.txt")
_RunDOS("find /n /i """&$qry&""" "&$file& " >> " &@HomeDrive&"\results.txt")
If Not @error Then
FileSetAttrib(@HomeDrive&"\results.txt", "-N+H+T", 0)
$CHARS = FileGetSize(@HomeDrive&"\results.txt")
Return FileRead(@HomeDrive&"\results.txt", $CHARS)&@CRLF
EndIf
EndFunc

Example:

ConsoleWrite($SEARCH)
Edited by broodplank

Share this post


Link to post
Share on other sites



I wrote this a while ago, and made a gui to display the information returned from it.

It returns a 2 dimensional Array,

[n][0] = File Name

[n][1] = Line number the text was found on

[n][2] = The actual line of text

It will search a directory recursively if wanted, with an option of case sensitivity. I haven't looked at it in some time, but I use it all the time and hadn't noticed anything wrong with it as of yet... maybe it will give you some ideas to add.

Func _FileFindText($hDirectory, $sText, $sMask = '*.*', $iRecurse = True, $iDetail = False, $iCase = 0)
    Local $hFile, $sHold, $iPID
    If StringRight($hDirectory, 1) <> '\' Then
        $hFile = $hDirectory & '\' & $sMask
    Else
        $hFile = $hDirectory & $sMask
    EndIf
    Local $sShowDetail = '/m', $sCase = ''
    If $iDetail Then $sShowDetail = '/n'
    If Not $iCase Then $sCase = '/i '
    If $iRecurse Then
        $iPID = Run(@ComSpec & ' /c findstr ' & $sCase & $sShowDetail & ' /p /s /' & @HomeDrive & '"' & $sText & '" "' & $hFile & '"', '', @SW_HIDE, 6)
    Else
        $iPID = Run(@ComSpec & ' /c findstr ' & $sCase & $sShowDetail & ' /p /' & @HomeDrive & '"' & $sText & '" "' & $hFile & '"', '', @SW_HIDE, 6)
    EndIf
    While Not @error
        $sHold &= StdoutRead($iPID)
    WEnd
    If Not $sHold Then Return SetError(1, 0, 0)
    Local $aSplit = StringSplit(StringTrimRight(StringStripCR($sHold), 1), @LF)
    Local $aArray[1][1], $iAdd, $ahSplit
    If Not $iDetail Then
        For $iCC = 1 To UBound($aSplit) - 1
            $iAdd += 1
            ReDim $aArray[$iAdd + 1][1]
            $aArray[$iAdd][0] = $aSplit[$iCC]
        Next
        $aArray[0][0] = $iAdd + 1
        If $iAdd Then Return $aArray
    Else
        For $iCC = 1 To UBound($aSplit) - 1
            $ahSplit = StringSplit($aSplit[$iCC], ':', 1)
            If $ahSplit[0] = 3 Or $ahSplit[0] = 4 Then
                $iAdd += 1
                ReDim $aArray[$iAdd + 1][3]
                If $ahSplit[0] = 3 Then
                    $aArray[$iAdd][0] = $ahSplit[1]
                    $aArray[$iAdd][1] = $ahSplit[2]
                    $aArray[$iAdd][2] = $ahSplit[3]
                Else
                    $aArray[$iAdd][0] = $ahSplit[1] & ':' & $ahSplit[2]
                    $aArray[$iAdd][1] = $ahSplit[3]
                    $aArray[$iAdd][2] = $ahSplit[4]
                EndIf
            EndIf
        Next
        $aArray[0][0] = $iAdd + 1
        If $iAdd Then Return $aArray
    EndIf
    Return SetError(2, 0, 0)
EndFunc

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

well iam a noob at autoit and i must admit that yours is so much better :)

Share this post


Link to post
Share on other sites

Bigger, but also slower, but nice work broodplank


-jaenster

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Bigger, but also slower, but nice work broodplank

Why do you think it's slower (What does it do that the OP doesn't, is the only clue I'll give)? And just how much slower is it? Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

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