I've recently been uncovering the useful commandline tools that can be found natively in Windows, one of which was findstr (there is also a GUI interface available in SciTE4AutoIt3.) After coming across this little gem and implementing in >SciTE Jump, it felt only right that I should share this on the forums as a standalone UDF. Thanks Function: ; #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 = True, $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 ;==>_FindInFileExample use of Function: #include <Array.au3>
#include <Constants.au3>
Example()
Func Example()
Local $hTimer = TimerInit()
Local $aArray = _FindInFile('findinfile', @ScriptDir, '*.au3;*.txt') ; Search for 'findinfile' within the @ScripDir and only in .au3 & .txt files.
ConsoleWrite(Ceiling(TimerDiff($hTimer) / 1000) & ' second(s)' & @CRLF)
_ArrayDisplay($aArray)
$hTimer = TimerInit()
$aArray = _FindInFile('autoit', @ScriptDir, '*.au3') ; Search for 'autoit' within the @ScripDir and only in .au3 files.
ConsoleWrite(Ceiling(TimerDiff($hTimer) / 1000) & ' second(s)' & @CRLF)
_ArrayDisplay($aArray)
EndFunc ;==>Example