{"id":65493,"date":"2007-02-17T00:55:00","date_gmt":"2007-02-17T00:55:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/02\/17\/how-can-i-insert-text-into-a-bookmark-in-a-text-file\/"},"modified":"2007-02-17T00:55:00","modified_gmt":"2007-02-17T00:55:00","slug":"how-can-i-insert-text-into-a-bookmark-in-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-insert-text-into-a-bookmark-in-a-text-file\/","title":{"rendered":"How Can I Insert Text Into a \u201cBookmark\u201d in a Text File?"},"content":{"rendered":"<p><H2><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\"> <\/H2>\n<P>Hey, Scripting Guy! I saw <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept06\/hey0925.mspx\">your article<\/A> about inserting text into a Microsoft Word bookmark. How can I do the same sort of thing in a text file?<BR><BR>&#8212; LR<\/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, LR. You\u2019ll have to forgive the Scripting Guy who writes this column if he seems a little less enthusiastic today than usual. As it turns out, he happened to see a commercial on TV right before he left for work this morning, and he\u2019s been a little down-in-the-dumps ever since.<\/P>\n<TABLE class=\"dataTable\" id=\"E6C\" 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>. Yes, for reasons the Scripting Guy who writes this column doesn\u2019t fully understand, the TV in the Scripting Household must be on at all times. One time the Scripting Guy who writes this column was home by himself, and didn\u2019t bother to turn the TV on. When the rest of the family returned home and saw that the TV was off, they immediately called 9-1-1, figuring that the Scripting Guy who writes this column must be dead. After all, if he <I>wasn\u2019t<\/I> dead then why wouldn\u2019t he have turned the TV on?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Anyway, in this commercial there\u2019s a guy in a brand-new car, a car perched on the road. High in the air is a helicopter toting a similar new car. On the signal, the guy on the ground hits the gas pedal and starts racing down the road; meanwhile, the helicopter cuts the other car loose. The whole thing turns out to be a race: can the car on the ground accelerate quickly enough to avoid being hit by the car falling from the sky.<\/P>\n<P>And, yes, it turns out that he <I>can<\/I> accelerate quickly enough to avoid getting squashed. Whew!<\/P>\n<P>So why did this commercial make the Scripting Guy who writes this column so sad? Well, it wasn\u2019t so much the commercial as it was the disclaimer accompanying that commercial:<\/P>\n<TABLE class=\"dataTable\" id=\"EUD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Professional driver on a closed course. Do not attempt this at home.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Do not attempt this at home?!? Oh, great; <I>now<\/I> what is the Scripting Guy who writes this column supposed to do with his helicopter and the two new cars he just bought? If, like this Scripting Guy, you had hoped to spend the weekend dropping cars from helicopters, well, you might want to think about changing those plans.<\/P>\n<P>Of course, that said, we aren\u2019t convinced that car companies have the right to tell us what we can and can\u2019t drop from our own helicopters. We\u2019ll look into this and let you know what we find out.<\/P>\n<P>In the meantime, you are more than welcome to insert text into a text file \u201cbookmark\u201d at home, at work, or anywhere else you might desire:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForWriting = 2<\/p>\n<p>Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\nstrUser = objNetwork.UserName<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)<\/p>\n<p>strText = objFile.ReadAll\nobjFile.Close<\/p>\n<p>strText = Replace(strText, &#8220;[BOOKMARK #1]&#8221;, Date)\nstrText = Replace(strText, &#8220;[BOOKMARK #2]&#8221;, strUser)<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)\nobjFile.WriteLine strText\nobjFile.Close\n<\/PRE>\n<P>Before we discuss how the script works we should note that this script <I>won\u2019t<\/I> work unless you have a text file that includes the specified \u201cbookmarks\u201d (which, in this case, simply refer to readily-identifiable chunks of text). For this example, we\u2019re using a simple little text file (C:\\Scripts\\Test.txt) that looks like this:<\/P><PRE class=\"codeSample\">This text file was updated on [BOOKMARK #1] by [BOOKMARK #2].\n<\/PRE>\n<P>Our script is going to replace the text <B>[BOOKMARK #1]<\/B> with the current date, then replace the text <B>[BOOKMARK #2]<\/B> with the name of the logged-on user. Note that the text we used for the bookmarks is arbitrary; the only caveat here is that you need to make sure that the text you use is separate and distinct from any other text found in the file. For example, if the word <I>computer<\/I> appears multiple times in your file then using <B>computer<\/B> as a bookmark name would be a bad idea. Why? Because the script will replace <I>every<\/I> instance of the word <I>computer<\/I> \u2013 not just the bookmark text \u2013 with the new text. That\u2019s why a unique construction such as <B>[<\/B><B>COMPUTER NAME]<\/B> is a better way to go.<\/P>\n<P>As for the script itself, we start out by defining a pair of constants \u2013 ForReading and ForWriting \u2013 that we\u2019ll use when working with the file. We then use these two lines of code to create an instance of the <B>Wscript.Network<\/B> object, get the logon name of the logged-on user, and then store that value in a variable named strUser:<\/P><PRE class=\"codeSample\">Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\nstrUser = objNetwork.UserName\n<\/PRE>\n<P>Once that\u2019s done we create an instance of the <B>Scripting.FileSystemObject<\/B>, then use the <B>OpenTextFile<\/B> method to open the file C:\\Scripts\\Test.txt for reading (note the constant ForReading passed as a method parameter):<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)\n<\/PRE>\n<P>With the text file open we use the <B>ReadAll<\/B> method to read the entire contents of the file into a variable named strText; we then immediately close the file:<\/P><PRE class=\"codeSample\">strText = objFile.ReadAll\nobjFile.Close\n<\/PRE>\n<P>So <I>why<\/I> do we close the file, especially since we want to replace the bookmarks in that file? Well, that\u2019s due to the eccentricities of the FileSystemObject: this object lets you read from a file <I>or<\/I> write to a file, but it doesn\u2019t let you do both operations at the same time. Having read from the file, we have to close and reopen it before we can write to it.<\/P>\n<P>However, before we do that we need to modify the value of strText. That\u2019s what these two lines of code are for:<\/P><PRE class=\"codeSample\">strText = Replace(strText, &#8220;[BOOKMARK #1]&#8221;, Date)\nstrText = Replace(strText, &#8220;[BOOKMARK #2]&#8221;, strUser)\n<\/PRE>\n<P>Good question: what <I>are<\/I> we doing with these two lines of code? Well, in the first line we\u2019re using the <B>Replace<\/B> function to search through strText and look for the value <I>[BOOKMARK #1]<\/I>. If we find that value, we\u2019re going to replace <I>{BOOKMARK #1]<\/I> with the current date (hence the VBScript <B>Date<\/B> function passed as parameter 3). In line 2, we\u2019re going to look for the text <I>[BOOKMARK #2]<\/I> and, if found, replace it with the name of the logged-on user. In both cases, the resulting value will be stored in the variable strText.<\/P>\n<P>What that means, of course, is that strText will then contain a \u201cvirtual text file\u201d in which the bookmark designators have been replaced by the specified values. Because of that we can then replace the <I>real<\/I> text file (C:\\Scripts\\Test.txt) with this modified version. That\u2019s what this block of code is for:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)\nobjFile.WriteLine strText\nobjFile.Close\n<\/PRE>\n<P>When that\u2019s done C:\\Scripts\\Test.txt will look like this:<\/P><PRE class=\"codeSample\">This text file was updated on 2\/15\/2007 by kenmyer.\n<\/PRE>\n<P>And there you have it.<\/P>\n<P>As a public service, we should also note that there is another car commercial on TV these days, one which shows a car driving up and down a bunch of buildings. Before you get too excited, however, keep in mind that this commercial also includes a disclaimer, one that notes \u201cThis is a fantasy. Car cannot really drive on buildings.\u201d Are they serious: a brand-new car and you can\u2019t even drive on buildings with it? In that case, you can go ahead and remove the Scripting Guy who writes this column from the list of potential buyers. After all, he already <I>has<\/I> a car that can\u2019t drive on buildings; why would he want another one?<\/P>\n<P>Well, unless they\u2019ll let him drop <I>that<\/I> car from a helicopter. Hmmm \u2026.<\/P>\n<TABLE class=\"dataTable\" id=\"EJH\" 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>. What, no mention of the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><B>2007 Winter Scripting Games<\/B><\/A> and how there\u2019s still times to enter at least one event and win a <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games07\/bobble.mspx\"><B>Dr. Scripto Bobblehead doll<\/B><\/A> or a copy of the book <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/msh\/payette1.mspx\"><B>Windows PowerShell in Action<\/B><\/A>? No, sorry, we aren\u2019t even going to mention the Scripting Games today. After all, we don\u2019t want people to think we\u2019re pushy or anything.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I saw your article about inserting text into a Microsoft Word bookmark. How can I do the same sort of thing in a text file?&#8212; LR Hey, LR. You\u2019ll have to forgive the Scripting Guy who writes this column if he seems a little less enthusiastic today than usual. As it turns [&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-65493","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! I saw your article about inserting text into a Microsoft Word bookmark. How can I do the same sort of thing in a text file?&#8212; LR Hey, LR. You\u2019ll have to forgive the Scripting Guy who writes this column if he seems a little less enthusiastic today than usual. As it turns [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65493","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=65493"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65493\/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=65493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}