{"id":10731,"date":"2006-04-25T12:18:00","date_gmt":"2006-04-25T12:18:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/04\/25\/check-spelling-script\/"},"modified":"2019-02-18T13:25:00","modified_gmt":"2019-02-18T20:25:00","slug":"check-spelling-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/check-spelling-script\/","title":{"rendered":"Check Spelling Script"},"content":{"rendered":"<p>After reading <font color=\"#800080\"><u><a href=\"http:\/\/blogs.msdn.com\/monad\/archive\/2005\/12\/16\/504524.aspx\">Marcel&#8217;s introductory&nbsp;piece on ScriptBlock,<\/a><\/u> <\/font>I decided to rewrite an old&nbsp;script of mine&nbsp;that checks the spelling of all text files in&nbsp;a directory.&nbsp;Here is what I came out with.<\/p>\n<p><font face=\"Courier New\" size=\"2\">#region Parse a line to a list of words<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">$StringFind =<br \/>{param ($str, $start, $cond)<br \/>&nbsp; if ($start -ge 0 -and $start -lt $str.Length) {<br \/>&nbsp;&nbsp;&nbsp; for($i = $start; $i -lt $str.Length; $i++) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (&amp; $cond $str[$i]) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return $i<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp; }<br \/>&nbsp; }<br \/>&nbsp; return $str.Length<br \/>}<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">$IsLetter =<br \/>{param ([char]$c) [char]::isletter($c)}<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">$IsNotLetter =<br \/>{param ([char] $c) ! (&amp; $IsLetter $c)}<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">function ParseFileForWord<br \/>{param ([string]$str)<br \/>&nbsp; $end = 0<br \/>&nbsp; for ($start = &amp; $StringFind $str $end $IsLetter;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $start -lt $str.Length;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $start = &amp; $StringFind $str ($end + 1) $IsLetter) {<br \/>&nbsp;&nbsp;&nbsp; $end = &amp; $StringFind $str $start $IsNotLetter<br \/>&nbsp;&nbsp;&nbsp; write-object $str.substring($start, ($end &#8211; $start))<br \/>&nbsp; }<br \/>}<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">#endregion Parse a line to a list of words<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">$wordApp = New-Object -com Word.Application<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">get-content (dir *.txt) | foreach {ParseFileForWord $_} | where {!$wordApp.CheckSpelling($_)} | Sort -unique<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">$wordApp.quit()<\/font><\/p>\n<p>A&nbsp;couple of things here. First I used a script block to parameterize StringFind. This saves me from writing similar-looking code for&nbsp;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&nbsp;<font face=\"Courier New\" size=\"2\">$StringFind $str $i $IsLetter<\/font> while&nbsp;<font face=\"Courier New\" size=\"2\">$StringFind $str $i $IsNotLetter<\/font>&nbsp;does the latter.&nbsp;Also, thanks to New-Object&#8217;s COMOBJECT parameter, it takes only several lines to carry out the spelling check part.<\/p>\n<p>[<i>Edit: Monad has now been renamed to Windows PowerShell.  This script or discussion may require slight adjustments before it applies directly to newer builds.<\/i>]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After reading Marcel&#8217;s introductory&nbsp;piece on ScriptBlock, I decided to rewrite an old&nbsp;script of mine&nbsp;that checks the spelling of all text files in&nbsp;a directory.&nbsp;Here is what I came out with. #region Parse a line to a list of words $StringFind ={param ($str, $start, $cond)&nbsp; if ($start -ge 0 -and $start -lt $str.Length) {&nbsp;&nbsp;&nbsp; for($i = $start; [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10731","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>After reading Marcel&#8217;s introductory&nbsp;piece on ScriptBlock, I decided to rewrite an old&nbsp;script of mine&nbsp;that checks the spelling of all text files in&nbsp;a directory.&nbsp;Here is what I came out with. #region Parse a line to a list of words $StringFind ={param ($str, $start, $cond)&nbsp; if ($start -ge 0 -and $start -lt $str.Length) {&nbsp;&nbsp;&nbsp; for($i = $start; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=10731"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10731\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=10731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}