Jump to content

_FindInFile() search for a string

Recommended Posts


First I will apologise. I was not sure if I should post a new topic or reply to the original topic that was posted four years ago. 

I haven’t used autoit in quite some time and I was not really great at it back then so I am really rusty now.

I was reading this post https://www.autoitscript.com/forum/topic/132159-_findinfile-search-for-a-string-within-files-located-in-a-specific-directory/?page=1

It appears  to do what I would like, which is to look in a folder that contains several hundred files for a particular string in each file and return those results.  But I am not sure exactly where I need to put certain declarations in it.

Directory is o:/RFP

The files are word docx documents

String to find is: BR_Field_Auditor_Inventory_Threshold

Ideally I would like the input to read from a file and then place the “found” string along with the name of the document in another file. I see that guinnes has provide a link that will help me with that. I just wanted to state what my intentions are so as not yup leave anything out.  

The only thing I mustard so far was that this    $sSearch = ' /c:' & $sSearch  may need to be changed to:        $sSearch = ' /O:' & $sSearch  I  see leading space between the quotes ‘ /c: does that matter?

I know I must declare $sFilePath and $sSearch but I am not seeing or understanding where.

I have been using thishttps://www.autoitscript.com/forum/topic/85567-find-string-in-files/ for finding a couple of strings from time to time but now I need something that will find multiple strings within multiple files.

Sorry again for asking probably a very basic question. 

I appreciate any help.




Share this post

Link to post
Share on other sites

Something like this?

#Include <Array.au3>
#Include <Constants.au3>

$sSearch = "BR_Field_Auditor_Inventory_Threshold"
$sFilePath = "O:\RFP"
$sMask = "*.docx"
$sResultsFile = @ScriptDir & "\Results.txt"

$aFiles = _FindInFile($sSearch, $sFilePath, $sMask)
;_ArrayDisplay($aFiles) ;Uncomment to see results

If FileExists($sResultsFile) Then FileDelete($sResultsFile)
$hFile = FileOpen($sResultsFile,  $FO_APPEND)
FileWrite($hFile, "Scan Compiled On: " & @MON & "/" & @MDAY & "/" & @YEAR & @CRLF)
FileWrite($hFile, "Result Files Containing String: " & $sSearch & @CRLF & @CRLF)

For $i = 1 to $aFiles[0]
    FileWrite($hFile, $aFiles[$i] & @CRLF)


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
    If $sMask = Default Then
        $sMask = '*'

    $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)
        $sOutput &= StdoutRead($iPID)
    Return StringSplit(StringStripWS(StringStripCR($sOutput), BitOR($STR_STRIPLEADING, $STR_STRIPTRAILING)), @LF)
EndFunc   ;==>_FindInFile


Share this post

Link to post
Share on other sites

Hi ViciousXUSMC,

it always help to declare the variables....:'(

I also was running an older version of autoit, helps if I had the latest version.

Thank you for help and the code.





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

  • Create New...