{"id":65023,"date":"2007-04-23T21:24:00","date_gmt":"2007-04-23T21:24:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/04\/23\/how-can-i-replace-the-first-five-characters-in-a-text-file-with-the-first-five-characters-in-the-computer-name\/"},"modified":"2007-04-23T21:24:00","modified_gmt":"2007-04-23T21:24:00","slug":"how-can-i-replace-the-first-five-characters-in-a-text-file-with-the-first-five-characters-in-the-computer-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-replace-the-first-five-characters-in-a-text-file-with-the-first-five-characters-in-the-computer-name\/","title":{"rendered":"How Can I Replace the First Five Characters in a Text File With the First Five Characters in the Computer Name?"},"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 open a text file and replace the first 5 characters with the first 5 characters of the local computer name?<BR><BR>&#8212; EC<\/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, EC. You know, you might find this hard to believe (his fellow Scripting Guys absolutely <I>refuse<\/I> to believe it), but sometimes the Scripting Guy who writes this column is all talk and no action. For example, a <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/feb07\/hey0227.mspx\"><B>few months<\/B><\/A> ago he told everyone that he was planning on going back to college and getting a second Master\u2019s degree (taking advantage of an email that promised he could get this degree in just two weeks). But was he <I>really<\/I> planning on going back to college? To tell you the truth, no: he\u2019s way past the point where he ever wants to work hard or learn anything ever again.<\/P>\n<P>Or so he thought. But then, last night, his life changed forever.<\/P>\n<P>You might find this even harder to believe, but there was a time, long ago, when the Scripting Guy who writes this column was actually fairly smart; in fact, after he took his SAT test he was inundated with offers to attend universities all over the country. Harvard, Yale, Stanford, you name it. In the end, he opted to stay in the state of Washington, partly due to the costs involved, but largely because of the ho-hum factor. No offense to Harvard or Yale, but, to tell you the truth, places like that didn\u2019t sound very exciting. As far as the Scripting Guy who writes this column was concerned, it didn\u2019t matter: if you\u2019ve seen one university you\u2019ve seen them all.<\/P>\n<P>Or so he thought. But then, last night, the Scripting Son was flipping through channels on the TV when he landed on the Food Network. It was there that the Scripting Guy who writes this column finally figured out what he was meant to do with his life: graduate from <A href=\"https:\/\/www.dunkindonuts.com\/aboutus\/company\/HistoryPrint.aspx\" target=\"_blank\"><B>Dunkin\u2019 Donuts University<\/B><\/A>. That\u2019s right, Dunkin\u2019 Donuts University (founded in 1966), a school where the curriculum is based entirely on making \u2013 and eating \u2013 donuts. Just try to find courses on donut-making and donut-eating at one of those hoity-toity Ivy League colleges. <\/P>\n<P>Well, OK, maybe Princeton. But other than that \u2026.<\/P>\n<P>There <I>is<\/I> a problem, however. Back when the Scripting Guy who writes this column was being inundated with invitations to attend various colleges and universities he did <I>not<\/I> receive an invitation from Dunkin\u2019 Donuts U. Does that mean he isn\u2019t qualified to attend Dunkin\u2019 Donuts University? Considering the number of donuts he\u2019s eaten in his life he expected, if anything, to be <I>over-qualified.<\/I> On the other hand, however, Dunkin\u2019 Donuts is probably looking for smart, hard-working, and responsible people.<\/P>\n<P>Dang! There\u2019s always a catch, isn\u2019t there?<\/P>\n<P>Unless \u2026. Hey, admissions committee at Dunkin\u2019 Donuts University. How would you feel about a smart, hard-working and responsible guy who can write a script that can replace the first 5 characters of a text file with the name of the local computer? <\/P>\n<P>Well, OK. Then how about this same script written by a not-so-smart, lazy, and somewhat-irresponsible guy:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForWriting = 2<\/p>\n<p>Set objFSo = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)<\/p>\n<p>strContents = objFile.ReadAll\nobjFile.Close<\/p>\n<p>Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\nstrComputer = objNetwork.ComputerName\nstrComputer = Left(strComputer, 5)<\/p>\n<p>intlength = Len(strContents)\nstrRemainder = Right(strContents, intLength &#8211; 5)\nstrNewContents = strComputer &amp; strRemainder<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)\nobjFile.WriteLine strNewContents<\/p>\n<p>objFile.Close\n<\/PRE>\n<P>On the off-chance that the admissions committee thinks the Scripting Guy who writes this column did nothing more than download this script off the Internet (like he did his master\u2019s thesis) let\u2019s take a few minutes to explain how this baby works. To begin with, we have a text file named C:\\Scripts\\Test.txt, a text file that (for our example purposes) looks like this:<\/P><PRE class=\"codeSample\">XXXXX This is our text file. We want to replace the first five\ncharacters (the Xs) with the first five characters of the\nlocal computer&#8217;s name.\n<\/PRE>\n<P>As you can see, and as you can read for yourself, the first five characters in the text file are nothing but Xs. Our job is to replace those five characters with the first five characters in the name of the local computer.<\/P>\n<P>To do that, we start out by defining a pair of constants, ForReading and ForWriting; we\u2019ll need to use these two constants when we open Test.txt. (And yes, for better or worse we need to open the text file twice: once to read in the existing contents, the second time to replace those contents with our new, modified contents.) After defining the two constants we then use these two lines of code to create an instance of the <B>Scripting.FileSystemObject<\/B> and to open the text file for reading:<\/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>As soon as we have the file open we use the <B>ReadAll<\/B> method to read the contents of the file (storing that information in a variable named strContents), then we close Test.txt:<\/P><PRE class=\"codeSample\">strContents = objFile.ReadAll\nobjFile.Close\n<\/PRE>\n<P>Now what?<\/P>\n<P>Funny you should ask that. (We hope that the admissions committee will take note of our prompt response to a customer inquiry.) Our next step is to run this block of code:<\/P><PRE class=\"codeSample\">Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\nstrComputer = objNetwork.ComputerName\n<\/PRE>\n<P>Here we\u2019re creating an instance of the <B>Wscript.Network<\/B> object, then assigning the value of the <B>ComputerName<\/B> property to the variable strComputer. It probably doesn\u2019t come as any great surprise that the value of the ComputerName property just happens to be the name of the computer itself. Of course, we don\u2019t actually want the entire computer name; all we want are the first five characters. With that in mind we then use the <B>Left<\/B> function to grab those first five characters and store them in the variable strComputer:<\/P><PRE class=\"codeSample\">strComputer = Left(strComputer, 5)\n<\/PRE>\n<P>The net result? If we happen to be running this script on a computer named GIZMONIC (which we are) then strComputer will be equal to this:<\/P><PRE class=\"codeSample\">GIZMO\n<\/PRE>\n<P>We now have the first five characters in the computer name. Our next task is to replace the first five characters in the text file with these computer name characters. Depending on the kind of information in your text file you might be able to do this using a search-and-replace operation; that will work as long as the first five characters in the text fill will always be XXXXX, and as long as it\u2019s OK to replace any other instances of XXXXX with GIZMO. We can\u2019t be sure that those conditions will always hold true, so we decided to replace the first five characters using this approach:<\/P><PRE class=\"codeSample\">intlength = Len(strContents)\nstrRemainder = Right(strContents, intLength &#8211; 5)\nstrNewContents = strComputer &amp; strRemainder\n<\/PRE>\n<P>In the first line of this code block we use the <B>Len<\/B> method to count the number of characters in strContents; if you\u2019re keeping score at home, that\u2019s 149. As you know, we want to replace only the first five characters in this string; we want to keep characters 6 through 149 exactly as they are. That\u2019s what our second line of code is for; in that line we use the <B>Right<\/B> function to start at the end of the string and work our way backwards, scooping up characters as we go. How <I>many<\/I> characters do we scoop up? We want to grab a number equal to the length of the string minus 5. (149 \u2013 5 equals 144, which means that we want to start at the end of the string and, working from right-to-left, grab the first 144 characters.) In turn, our variable strRemainder will be assigned everything except the first five characters in the string (because we stopped grabbing characters before we got to those). <\/P>\n<P>Or, to put it another way, strRemainder ends up equal to this:<\/P><PRE class=\"codeSample\">This is our text file. We want to replace the first five\ncharacters (the Xs) with the first five characters of the\nlocal computer&#8217;s name.\n<\/PRE>\n<P>As you can see, that\u2019s the entire string except for the first five characters.<\/P>\n<P>Finally, in line 3 we combine the variables strComputer and strRemainder, then assign <I>that<\/I> value to a variable named strNewContents. That makes this new variable equal to the following:<\/P><PRE class=\"codeSample\">GIZMO This is our text file. We want to replace the first five\ncharacters (the Xs) with the first five characters of the\nlocal computer&#8217;s name.\n<\/PRE>\n<P>Well, what do you know: we\u2019ve replaced the first five characters in the text file (XXXXX) with the first five characters in the computer name (GIZMO).<\/P>\n<P>All that\u2019s left now is to reopen the file Test.txt (this time for writing), use the <B>WriteLine<\/B> method to overwrite the old contents with our new, modified contents, and then close the file:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)\nobjFile.WriteLine strNewContents<\/p>\n<p>objFile.Close\n<\/PRE>\n<P>That should do it.<\/P>\n<P>We hope that helps, EC. (And we hope that impresses you, Dunkin\u2019 Donuts University admissions committee.) We\u2019d like to stay and take questions from the audience, but we need to hurry down to the local bakery and pick up a couple dozen donuts. After all, it\u2019s never too early to start studying for our entrance exam. <\/P>\n<P>At least not when you\u2019re hoping to attend Dunkin\u2019 Donuts University.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I open a text file and replace the first 5 characters with the first 5 characters of the local computer name?&#8212; EC Hey, EC. You know, you might find this hard to believe (his fellow Scripting Guys absolutely refuse to believe it), but sometimes the Scripting Guy who writes this [&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-65023","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 open a text file and replace the first 5 characters with the first 5 characters of the local computer name?&#8212; EC Hey, EC. You know, you might find this hard to believe (his fellow Scripting Guys absolutely refuse to believe it), but sometimes the Scripting Guy who writes this [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65023","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=65023"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65023\/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=65023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}