Powershell search matching string in word document

后端 未结 3 1991
暖寄归人
暖寄归人 2021-01-13 11:41

I have a simple requirement. I need to search a string in Word document and as result I need to get matching line / some words around in document.

So far, I could s

3条回答
  •  旧巷少年郎
    2021-01-13 12:01

    Thanks! You provided a great solution to use PowerShell regex expressions to look for information in a Word document. I needed to modify it to meet my needs. Maybe, it will help someone else. It reads each line of the word document, and then uses the regex expression to determine if the line is a match. The output could easily be modified or dumped to a log file.

        Set-StrictMode -Version latest
        $path = "c:\Temp\pii"
        $files    = Get-Childitem $path -Include *.docx,*.doc -Recurse | Where-Object { !($_.psiscontainer) }
        $application = New-Object -comobject word.application
        $application.visible = $False
        $findtext = "[0-9]" #regex
    
        Function getStringMatch
        {
        # Loop through all *.doc files in the $path directory
    
        Foreach ($file In $files) {
              $document = $application.documents.open($file.FullName,$false,$true)
              $arrContents = $document.content.text.split()
              $varCounter = 0
              ForEach ($line in $arrContents) {
                    $varCounter++
                    If($line -match $findtext) {
                        "File: $file Found: $line Line: $varCounter"
                     }
              }
        $document.close()
        }
        $application.quit()
    
        }
    
    getStringMatch
    

提交回复
热议问题