Jump to content

_FindInFile() - Search for a string within files located in a specific directory.


guinness
 Share

Recommended Posts

  • 5 months later...

Hi guinness

I tried your code, and I tried below example, but for failure scenario its not setting @error as non zero, for all cases its showing @error as 0

Local $array = _FindInFile("example ", "C:\ABC\", "*.txt")

Actually I wanted to search a string in single text file, So I was trying to read error code to check search is successful or not in my case, but error code is coming as zero always...can you please help......Thanks

Edited by praveenvp
Link to comment
Share on other sites

  • 1 year later...

I can't get this to work. Maybe updates to AutoIt are causing it to fail?

I also noticed it looks like you hard coded $sMask to '*' in your function. Is that right?

When I run your code with my search parameters I get an array with a 1 in array[0] and my file name in array[1].

The test file I am searching has 4 instances of '*.mp4" in it. Should the array[0] return 4?

This is my modified code.

#include <Array.au3>
#include <Constants.au3>

Local $sFile = '*.json'
Local $sSearchText = '.mp4'
Local $sDirectory = 'C:\MyDir\Data'

Example()

Func Example()
    Local $hTimer = TimerInit()
    Local $aArray = _FindInFile($sSearchText, $sDirectory, $sFile) ; Search for '.mp4' within the Data Dir and only in .json files
    ConsoleWrite(Ceiling(TimerDiff($hTimer) / 1000) & ' second(s)' & @CRLF)
    _ArrayDisplay($aArray)
EndFunc   ;==>Example


; #FUNCTION# ====================================================================================================================
; Name ..........: _FindInFile
; Description ...: Search for a string within files located in a specific directory.
; Syntax ........: _FindInFile($sSearch, $sFilePath[, $sMask = '*'[, $fRecursive = True[, $fLiteral = Default[, $fCaseSensitive = Default[, $fDetail = Default]]]]])
; Parameters ....: $sSearch             - The keyword to search for.
;                  $sFilePath           - The folder location of where to search.
;                  $sMask               - [optional] A list of filetype extensions separated with ';' e.g. '*.au3;*.txt'. Default is all files.
;                  $fRecursive          - [optional] Search within subfolders. Default is True.
;                  $fLiteral            - [optional] Use the string as a literal search string. Default is False.
;                  $fCaseSensitive      - [optional] Use Search is case-sensitive searching. Default is False.
;                  $fDetail             - [optional] Show filenames only. Default is False.
; Return values .: Success - Returns a one-dimensional and is made up as follows:
;                            $aArray[0] = Number of rows
;                            $aArray[1] = 1st file
;                            $aArray[n] = nth file
;                  Failure - Returns an empty array and sets @error to non-zero
; Author ........: guinness
; Remarks .......: For more details: http://ss64.com/nt/findstr.html
; Example .......: Yes
; ===============================================================================================================================
Func _FindInFile($sSearch, $sFilePath, $sMask, $fRecursive = False, $fLiteral = Default, $fCaseSensitive = Default, $fDetail = Default)
    Local $sCaseSensitive = $fCaseSensitive ? '' : '/i', $sDetail = $fDetail ? '/n' : '/m', $sRecursive = ($fRecursive Or $fRecursive = Default) ? '/s' : ''
    If $fLiteral Then
        $sSearch = ' /c:' & $sSearch
    EndIf
    If $sMask = Default Then
        $sMask = '*'
    EndIf

    $sFilePath = StringRegExpReplace($sFilePath, '[\\/]+$', '') & '\'
    Local Const $aMask = StringSplit($sMask, ';')
    Local $iPID = 0, $sOutput = ''
    For $i = 1 To $aMask[0]
        $iPID = Run(@ComSpec & ' /c ' & 'findstr ' & $sCaseSensitive & ' ' & $sDetail & ' ' & $sRecursive & ' "' & $sSearch & '" "' & $sFilePath & $aMask[$i] & '"', @SystemDir, @SW_HIDE, $STDOUT_CHILD)
        ProcessWaitClose($iPID)
        $sOutput &= StdoutRead($iPID)
    Next
    Return StringSplit(StringStripWS(StringStripCR($sOutput), BitOR($STR_STRIPLEADING, $STR_STRIPTRAILING)), @LF)
EndFunc   ;==>_FindInFile

 

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