{"id":70153,"date":"2005-03-28T17:08:00","date_gmt":"2005-03-28T17:08:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/28\/how-can-i-add-a-word-to-a-text-file-if-that-word-isnt-already-in-the-file\/"},"modified":"2005-03-28T17:08:00","modified_gmt":"2005-03-28T17:08:00","slug":"how-can-i-add-a-word-to-a-text-file-if-that-word-isnt-already-in-the-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-add-a-word-to-a-text-file-if-that-word-isnt-already-in-the-file\/","title":{"rendered":"How Can I Add a Word to a Text File if That Word Isn\u2019t Already in the File?"},"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 check a text file to see if a word appears in that file and, if it doesn\u2019t, add the word to the file?<BR><BR>&#8212; JO<\/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, JO. We seem to have gotten quite a few questions on this topic lately. You mentioned adding words to a custom dictionary for Microsoft Word; other people mentioned adding an entry to an LMHosts file or something similar. What we\u2019ll do today is provide a generic solution &#8211; adding a computer to a list of computer names stored in a text file &#8211; but you shouldn\u2019t have any problem adapting our generic example to your specific needs.<\/P>\n<P>To begin with, let\u2019s assume we have a very simple text file (C:\\Scripts\\Computers.txt) that features the names of all our computers, one computer per line:<\/P><PRE class=\"codeSample\">atl-dc-01\natl-dc-02\natl-dc-03\n<\/PRE>\n<P>We just got a new computer &#8211; atl-dc-99 &#8211; and we want to add that to the list, <I>provided<\/I>, of course, that atl-dc-99 isn\u2019t already on the list. To do that we need to read through Computers.txt and see if atl-dc-99 is already in there. If it is, we\u2019re done; if it isn\u2019t, we need to programmatically add the computer to the list. This is going to require three steps: read the file; check to see if the computer is already in there; and, if necessary, add the computer to the file.<\/P>\n<P>Let\u2019s start with reading the file. Here\u2019s some code that will open the file C:\\Scripts\\Computers.txt, read the entire contents into a variable named strWordList, and then close the file:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForAppending = 8<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile _\n    (&#8220;C:\\Scripts\\Computers.txt&#8221;, ForReading)\nstrWordList = objFile.ReadAll\nobjFile.Close\n<\/PRE>\n<P>We begin by defining two constants &#8211; <B>ForReading<\/B> and <B>ForAppending<\/B> &#8211; that we\u2019ll use when working with the text file. For now the only constant we need is ForReading, which tells the FileSystemObject that we want to open a file and read the contents. (You can open a file for reading, for writing, or for appending, but you can\u2019t perform more than a single operation at any one time.)<\/P>\n<P>Next we create an instance of the FileSystemObject and use the <B>OpenTextFile <\/B>method to open the file for reading. We then use the <B>ReadAll<\/B> method to read the entire file and store the contents in the variable strWordList. With that done, we call the <B>Close<\/B> method to close the file. <\/P>\n<P><B>Note<\/B>. Why close the file so soon after opening it? Well, if atl-dc-99 is already in the file, then we\u2019re basically done; we won\u2019t need to do anything more with Computers.txt. But what if atl-dc-99 <I>isn\u2019t<\/I> in the file? In that case, we need to append the new computer to the file; however, as we mentioned, we can\u2019t simultaneously read from and append to a file. Consequently, we need to close the file (which was initially opened for reading) and then reopen it, this time for appending.<\/P>\n<P>So much for step 1. In step 2, we need to determine whether atl-dc-99 is already included in our text file. To do that, we start by defining the string we\u2019re searching for, something we do here:<\/P><PRE class=\"codeSample\">strSearchWord = &#8220;atl-dc-99&#8221; &amp; vbCrLf\n<\/PRE>\n<P>Notice that we\u2019re constructing a string that consists of the search word (atl-dc-99) plus another carriage return-linefeed. Why go to all that trouble? Well, we want to make sure that any instance of atl-dc-99 we find is a complete computer name and not part of a computer name. Suppose we didn\u2019t specify that a carriage return-linefeed comes before and after our search word, suppose we just said, \u201cFind all instances of atl-dc-99.\u201d In that case, all of these computer names would be scored as \u201chits:\u201d<\/P><PRE class=\"codeSample\">atl-dc-999\natl-dc-9999\natl-dc-99-mail-server\n<\/PRE>\n<P>You get the idea. We want to ensure that any instance of atl-dc-99 is on a line all by itself, like this:<\/P><PRE class=\"codeSample\">atl-dc-99\n<\/PRE>\n<P>In other words, did the person who created the list type the name <I>atl-dc-99<\/I> and then press ENTER? That\u2019s why we look for a carriage return-linefeed immediately following atl-dc-99. <\/P>\n<P>Note that even this approach isn\u2019t foolproof; for example, the script will be \u201cfooled\u201d by a name like this:<\/P><PRE class=\"codeSample\">seast-atl-dc-99\n<\/PRE>\n<P>There are ways to work around problems like this, but they get a little more complicated. If you\u2019re interested, you might take a look at the <A href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=44007\"><B>Scripting Guys webcast<\/B><\/A> on using regular expressions. <\/P>\n<P>So how can we tell if our search term appears in the text file? Well, remember, we have the contents of the file stored in the variable strWordList. Because of that we can use the VBScript <B>InStr<\/B> method to determine whether or not our search term appears anywhere in the file:<\/P><PRE class=\"codeSample\">If InStr(strWordList, strSearchWord) = 0 Then\n<\/PRE>\n<P>If found, InStr will return the character position in the variable where the search term begins; if the search term can\u2019t be found, then InStr returns a 0. To determine whether or not atl-dc-99 is already in the text file, we just check the return value for InStr. If this value is anything other than 0, that means the search term was found and we\u2019re done. If the return value is 0, then we need to add atl-dc-99 to the text file. We do that with these lines of code:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.OpenTextFile _\n    (&#8220;C:\\Scripts\\Computers.txt &#8220;, ForAppending)\nobjFile.WriteLine &#8220;atl-dc-99&#8221;\nobjFile.Close\n<\/PRE>\n<P>All we do here is reopen the file &#8211; this time for appending &#8211; and then use the <B>WriteLine<\/B> method to write atl-dc-99 at the bottom of the file. (By definition, \u201cappending\u201d to a file adds the information to the end of the file.) We then close the file, and move on to bigger and better things.<\/P>\n<P>Here\u2019s what the completed script looks like: <\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForAppending = 8<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile _\n    (&#8220;C:\\Scripts\\Computers.txt&#8221;, ForReading)\nstrWordList = objFile.ReadAll\nobjFile.Close<\/p>\n<p>strSearchWord = &#8220;atl-dc-99&#8221; &amp; vbCrLf<\/p>\n<p>If InStr(strWordList, strSearchWord) = 0 Then\n    Set objFile = objFSO.OpenTextFile _\n        (&#8220;C:\\Scripts\\Computers.txt &#8220;, ForAppending)\n    objFile.WriteLine &#8220;atl-dc-99&#8221;\n    objFile.Close\nEnd If\n<\/PRE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I check a text file to see if a word appears in that file and, if it doesn\u2019t, add the word to the file?&#8212; JO Hey, JO. We seem to have gotten quite a few questions on this topic lately. You mentioned adding words to a custom dictionary for Microsoft [&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-70153","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 check a text file to see if a word appears in that file and, if it doesn\u2019t, add the word to the file?&#8212; JO Hey, JO. We seem to have gotten quite a few questions on this topic lately. You mentioned adding words to a custom dictionary for Microsoft [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70153","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=70153"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70153\/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=70153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}