2

I have a powershell script to search a string from word files as follows:

$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()
}

I want to enhance it to enable regex and insert it before the .execute() as this:

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

However, the properties are read-only as it complains. So, I try passing them as the parms in .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)

How can I do it like this?

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

Many thanks.

It works now, thank you.

$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()
}
chingNotCHing
  • 394
  • 1
  • 9

0 Answers0