{"id":70013,"date":"2005-04-15T17:13:00","date_gmt":"2005-04-15T17:13:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/04\/15\/how-can-i-search-a-text-file-for-9-digit-numbers\/"},"modified":"2005-04-15T17:13:00","modified_gmt":"2005-04-15T17:13:00","slug":"how-can-i-search-a-text-file-for-9-digit-numbers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-search-a-text-file-for-9-digit-numbers\/","title":{"rendered":"How Can I Search a Text File for 9-Digit Numbers?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I search for user names in a text file? In my case, the user names all consist of 9 digits, although they can be any 9 digits.<BR><BR>&#8212; GG<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, GG. Interesting question. You want to search for 9-digit numbers (like 123456789) in a text file; the only problem is that these could be <I>any<\/I> 9-digit numbers. And that definitely is a problem. If you were looking for a specific 9-digit number you could use VBScript\u2019s InStr function:<\/P><PRE class=\"codeSample\">intMatch = InStr(strText, &#8220;123456789&#8221;)\n<\/PRE>\n<P>If intMatch is greater than 0 that means the value 123456789 was found in the search string strText; if intMatch is 0, then the value couldn\u2019t be found. Simple.<\/P>\n<P>But that won\u2019t work here, not unless you want to create separate InStr calls for each possible 9-digit value. (In case you\u2019re wondering, that would be around 1 billion lines of code.) So if InStr can\u2019t do the job for us, what can?<\/P>\n<P>Did someone say regular expressions? Well, you should have, because that\u2019s the answer. Regular expressions &#8211; which are actually built into VBScript &#8211; provide a very flexible and very powerful way to search through text data. Regular expressions are used quite a bit in the Unix world simply because so much of Unix administration is built around the use of text files. But as this question illustrates, regular expressions have their use in Windows as well. InStr can\u2019t find any 9-digit number, regardless of the 9 digits; regular expressions can.<\/P>\n<TABLE class=\"dataTable\" id=\"EAD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Actually, regular expressions can do almost anything. (Hey, we said <I>almost<\/I>.) We can\u2019t even begin to detail all the uses of regular expressions in one little column. If you\u2019re interested in learning more about this very powerful technology, view the Scripting Guys webcast <A href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=44007\"><B>String Theory for System Administrators: An Introduction to Regular Expressions<\/B><\/A>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Let\u2019s take a look at a script that can locate any 9-digit numbers in a search string. For this simple example we\u2019ll search a hard-coded value. After we\u2019ve explained how the script works we\u2019ll then modify it to search a text file.<\/P>\n<P>Here\u2019s the script:<\/P><PRE class=\"codeSample\">Set objRegEx = CreateObject(&#8220;VBScript.RegExp&#8221;)\nobjRegEx.Global = True   \nobjRegEx.Pattern = &#8220;\\d{9}&#8221;<\/p>\n<p>strSearchString = &#8220;aaaaaaa123456789qqqqqqqqqqqq234567891aaaa12345678&#8221;<\/p>\n<p>Set colMatches = objRegEx.Execute(strSearchString)  <\/p>\n<p>If colMatches.Count &gt; 0 Then\n   strMessage = &#8220;The following user accounts were found:&#8221; &amp; vbCrlf\n   For Each strMatch in colMatches   \n       strMessage = strMessage &amp;  strMatch.Value &amp; &#8221; (character position &#8221; &amp;  _\n           strMatch.FirstIndex &amp; &#8220;)&#8221; &amp; vbCrLf\n   Next\nEnd If<\/p>\n<p>Wscript.Echo strMessage\n<\/PRE>\n<P>If you run this script under Cscript you should get back information that looks like this; in other words, the script should have located (and reported the position of) the two 9-digit values in the search string:<\/P><PRE class=\"codeSample\">The following user accounts were found:\n123456789 (character position 7)\n234567891 (character position 28)\n<\/PRE>\n<P>So how did we do this? We used regular expressions, remember? Sheesh.<\/P>\n<P>Oh, right: sorry. Actually, we began by creating an instance of VBScript\u2019s Regular Expressions object (<B>RegExp<\/B>); that\u2019s what this line of code does:<\/P><PRE class=\"codeSample\">Set objRegEx = CreateObject(&#8220;VBScript.RegExp&#8221;)\n<\/PRE>\n<P>We then set two properties of the Regular Expressions object. By setting the <B>Global<\/B> property to True we\u2019re telling the script to go ahead and search through the entire string and locate all matches; by default, the Regular Expressions object locates only the first match and then stops searching. <\/P>\n<P>The <B>Pattern<\/B> property represents the item we\u2019re searching for. Note that, in this case at least, we really <I>are<\/I> specifying a pattern to search for. The <B>\\d<\/B> means \u201cSearch for digits; ignore anything that isn\u2019t a number.\u201d The <B>{9}<\/B> means \u201cNine consecutive matches.\u201d Taken together we\u2019re saying, \u201cLook for any 9 numbers in a row.\u201d 123456789 will be a match; that\u2019s because there are 9 numbers in a row. 12345678 9 <I>won\u2019t<\/I> be a match; that\u2019s because we have 8 numbers and then a blank space. Close, but no cigar.<\/P>\n<P>After specifying the pattern we then assign our search string to a variable named strSearchString. That happens here:<\/P><PRE class=\"codeSample\">strSearchString = &#8220;aaaaaaa123456789qqqqqqqqqqqq234567891aaaa12345678&#8221;\n<\/PRE>\n<P>And now we\u2019re ready to go. This line of code fires off the search and returns a collection of all the matches:<\/P><PRE class=\"codeSample\">Set colMatches = objRegEx.Execute(strSearchString)\n<\/PRE>\n<P>How do we know if our search actually found any 9-digit numbers? The easiest way is to simply check the <B>Count<\/B> property of the collection; if the count is greater than 0 then at least one match was found. If that\u2019s the case we then loop through the collection and grab the <B>Value<\/B> and <B>FirstIndex<\/B> values for each match. (FirstIndex is simply the character position in the string where the match can be found.) We store this information in a variable named strMessage; at the end of the script we then echo the value of this variable. As we noted earlier, that should look like this:<\/P><PRE class=\"codeSample\">The following user accounts were found:\n123456789 (character position 7)\n234567891 (character position 28)\n<\/PRE>\n<P>Is that cool or what? Of course, the preceding example only searches a string hard-coded into the script. To search a text file you need to use the <B>FileSystemObject<\/B> to open the file, read the contents into the variable strSearchString, and <I>then<\/I> execute the Regular Expressions search. Here\u2019s a modified script that searches the file C:\\Scripts\\Test.txt:<\/P><PRE class=\"codeSample\">Const ForReading = 1<\/p>\n<p>Set objRegEx = CreateObject(&#8220;VBScript.RegExp&#8221;)\nobjRegEx.Global = True   \nobjRegEx.Pattern = &#8220;\\d{9}&#8221;<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)\nstrSearchString = objFile.ReadAll\nobjFile.Close<\/p>\n<p>Set colMatches = objRegEx.Execute(strSearchString)  <\/p>\n<p>If colMatches.Count &gt; 0 Then\n   strMessage = &#8220;The following user accounts were found:&#8221; &amp; vbCrlf\n   For Each strMatch in colMatches   \n       strMessage = strMessage &amp;  strMatch.Value &amp; &#8221; (character position &#8221; &amp;  _\n           strMatch.FirstIndex &amp; &#8220;)&#8221; &amp; vbCrLf\n   Next\nEnd If<\/p>\n<p>Wscript.Echo strMessage\n<\/PRE>\n<P>Like we said, is that cool or what? To learn even more, don\u2019t forget about the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/webcasts\/default.mspx\"><B>regular expressions webcast<\/B><\/A> coming April 26th.<\/P><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr05\/hey0415.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr05\/hey0415.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I search for user names in a text file? In my case, the user names all consist of 9 digits, although they can be any 9 digits.&#8212; GG Hey, GG. Interesting question. You want to search for 9-digit numbers (like 123456789) in a text file; the only problem is that [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,14,5],"class_list":["post-70013","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I search for user names in a text file? In my case, the user names all consist of 9 digits, although they can be any 9 digits.&#8212; GG Hey, GG. Interesting question. You want to search for 9-digit numbers (like 123456789) in a text file; the only problem is that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70013"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70013\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}