我需要在带有powershell的Microsoft Word文档中使用regexp获取通配符的所有发生。
我找到了这个解决方案,但只获得了通配符的第一次发生。
"如何让powershell在Word文档中搜索通配符并返回它找到的单词?" How do I make powershell search a Word document for wildcards and return the word it found?
你能帮助我吗?
答案 0 :(得分:0)
最后,我使用相关问题的代码稍作修改。 在搜索Word文档内容中不同通配符( $ finTest )的循环中,我使用( $)初始化的临时内容( $ tempContent ) document.Content.Text )值,然后对于我在文件中找到的每个通配符,我将它们从de临时内容中删除,并在创建通配符(regexp)时重复(同时)该进程。
$filePath = "C:\files\"
$textPath = "C:\strings.txt"
$outputPath = "C:\output.txt"
$findTexts = (Get-Content $textPath)
$docs = Get-childitem -path $filePath -Recurse -Include *.docx
$application = New-Object -comobject word.application
Foreach ($doc in $docs)
{
$document = $application.documents.open("$doc", $false, $true)
$application.visible = $False
$matchCase = $false
$matchWholeWord = $false
$matchWildCards = $true
$matchSoundsLike = $false
$matchAllWordForms = $false
$forward = $true
$wrap = 1
$range = $document.content
$null = $range.movestart()
Foreach ($findtext in $findTexts)
{
#Set tempContent with de Content of the document
$tempContent=$document.Content.Text
while ($tempContent -match "\b$($findText)\w+\b")
{
#Remove all de ocurrences of wildcard founded, for the tempContent
$tempContent=$tempContent -replace $matches[0],''
$docName = $doc.Name
"$($matches[0])`t$docName" | Out-File -append $outputPath
}
} #end foreach $findText
$document.close()
} #end foreach $doc
$application.quit()