Powershell在许多Microsoft Word文档中搜索正则表达式

时间:2014-07-29 07:48:43

标签: regex powershell ms-word

我有一个powershell脚本来搜索word文件中的字符串,如下所示:

$searchStr = "ABC/2014/N/123"
$files = gci -path "c:\doc","d:\doc" -include "*.doc*","*.tp?" -recurse 

$word = new-object -ComObject "word.application"
foreach ($file in $files) {

   $doc = $word.documents.open($file.fullname)
   if ($doc.content.find.execute($searchStr)) {

      echo $file.fullname
   }
   $doc.close()
}

我想增强它以启用正则表达式并在.execute()之前插入它:

set $doc.content.find.text = "[A-Z]{2-5}\/[0-9]{4}\/N\/[0-9]{3}"
set $doc.content.find.matchwildcards = $true

但是,这些属性是抱怨的,因为它是抱怨的。 所以,我尝试将它们作为.execute()

中的参数传递
PS C:\> $doc.content.find.execute

OverloadDefinitions
-------------------
bool Execute (Variant, Variant, Variant, Variant, Variant, Variant, Variant,
Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant)

我怎么能这样做?

$doc.content.find.execute(Text:="[A-Z]{2-5}\/[0-9]{4}\/N\/[0-9]{3}", matchwildcards:=$true)

非常感谢。

现在有效,谢谢。

$searchStr = "CIS\/[0-9]{4}\/N\/[0-9]{3}"
$isRegexp = $true
$files = gci -path "g:\spc","h:\spc" -include "*.doc*","*.tp?" -recurse 

$default = [Type]::Missing
$word = new-object -ComObject "word.application"
foreach ($file in $files) {

   $doc = $word.documents.open($file.fullname, )
   # expression.Execute(FindText, MatchCase, MatchWholeWord, MatchWildcards, 
   #  MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, 
   #  Replace, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl)
   if ($doc.content.find.execute($searchStr,$default,$default,$isRegexp)) {

      echo $file.fullname
   }
   $doc.close()
}

0 个答案:

没有答案