Jump to content

_StringSearchInFile


broodplank
 Share

Recommended Posts

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
Link to comment
Share on other sites

  • Moderators

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

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.

Link to comment
Share on other sites

  • Moderators

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

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.

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...