After reading Marcel’s introductory piece on ScriptBlock, I decided to rewrite an old script of mine that checks the spelling of all text files in a directory. Here is what I came out with.
#region Parse a line to a list of words
$StringFind =
{param ($str, $start, $cond)
if ($start -ge 0 -and $start -lt $str.Length) {
for($i = $start; $i -lt $str.Length; $i++) {
if (& $cond $str[$i]) {
return $i
}
}
}
return $str.Length
}
$IsLetter =
{param ([char]$c) [char]::isletter($c)}
$IsNotLetter =
{param ([char] $c) ! (& $IsLetter $c)}
function ParseFileForWord
{param ([string]$str)
$end = 0
for ($start = & $StringFind $str $end $IsLetter;
$start -lt $str.Length;
$start = & $StringFind $str ($end + 1) $IsLetter) {
$end = & $StringFind $str $start $IsNotLetter
write-object $str.substring($start, ($end – $start))
}
}
#endregion Parse a line to a list of words
$wordApp = New-Object -com Word.Application
get-content (dir *.txt) | foreach {ParseFileForWord $_} | where {!$wordApp.CheckSpelling($_)} | Sort -unique
$wordApp.quit()
A couple of things here. First I used a script block to parameterize StringFind. This saves me from writing similar-looking code for two functions: one to find the next letter in a string and the other to find the next non-letter . To accomplish the former I do $StringFind $str $i $IsLetter while $StringFind $str $i $IsNotLetter does the latter. Also, thanks to New-Object’s COMOBJECT parameter, it takes only several lines to carry out the spelling check part.
[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]
0 comments