Advertisement
mdelatorre

File in Find with AutoIt

Dec 7th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 2.71 KB | Source Code | 0 0
  1. ; #FUNCTION# ====================================================================================================================
  2. ; Name ..........: _FindInFile
  3. ; Description ...: Search for a string within files located in a specific directory.
  4. ; Syntax ........: _FindInFile($sSearch, $sFilePath[, $sMask = '*'[, $fRecursive = True[, $fLiteral = Default[,
  5. ;                  $fCaseSensitive = Default[, $fDetail = Default]]]]])
  6. ; Parameters ....: $sSearch             - The keyword to search for.
  7. ;                  $sFilePath           - The folder location of where to search.
  8. ;                  $sMask               - [optional] A list of filetype extensions separated with ';' e.g. '*.au3;*.txt'. Default is all files.
  9. ;                  $fRecursive          - [optional] Search within subfolders. Default is True.
  10. ;                  $fLiteral            - [optional] Use the string as a literal search string. Default is False.
  11. ;                  $fCaseSensitive      - [optional] Use Search is case-sensitive searching. Default is False.
  12. ;                  $fDetail             - [optional] Show filenames only. Default is False.
  13. ; Return values .: Success - Returns a one-dimensional and is made up as follows:
  14. ;                            $aArray[0] = Number of rows
  15. ;                            $aArray[1] = 1st file
  16. ;                            $aArray[n] = nth file
  17. ;                  Failure - Returns an empty array and sets @error to non-zero
  18. ; Author ........: guinness
  19. ; Remarks .......: For more details: http://ss64.com/nt/findstr.html
  20. ; Example .......: Yes
  21. ; ===============================================================================================================================
  22. Func _FindInFile($sSearch, $sFilePath, $sMask = '*', $fRecursive = True, $fLiteral = Default, $fCaseSensitive = Default, $fDetail = Default)
  23.     Local $sCaseSensitive = $fCaseSensitive ? '' : '/i', $sDetail = $fDetail ? '/n' : '/m', $sRecursive = ($fRecursive Or $fRecursive = Default) ? '/s' : ''
  24.     If $fLiteral Then
  25.         $sSearch = ' /c:' & $sSearch
  26.     EndIf
  27.     If $sMask = Default Then
  28.         $sMask = '*'
  29.     EndIf
  30.  
  31.     $sFilePath = StringRegExpReplace($sFilePath, '[\\/]+$', '') & '\'
  32.     Local Const $aMask = StringSplit($sMask, ';')
  33.     Local $iPID = 0, $sOutput = ''
  34.     For $i = 1 To $aMask[0]
  35.         $iPID = Run(@ComSpec & ' /c ' & 'findstr ' & $sCaseSensitive & ' ' & $sDetail & ' ' & $sRecursive & ' "' & $sSearch & '" "' & $sFilePath & $aMask[$i] & '"', @SystemDir, @SW_HIDE, $STDOUT_CHILD)
  36.         ProcessWaitClose($iPID)
  37.         $sOutput &= StdoutRead($iPID)
  38.     Next
  39.     Return StringSplit(StringStripWS(StringStripCR($sOutput), BitOR($STR_STRIPLEADING, $STR_STRIPTRAILING)), @LF)
  40. EndFunc   ;==>_FindInFile
Tags: AutoIT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement