{"id":67103,"date":"2006-06-15T15:03:00","date_gmt":"2006-06-15T15:03:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/06\/15\/how-can-i-insert-text-into-a-specific-column-of-a-text-file\/"},"modified":"2006-06-15T15:03:00","modified_gmt":"2006-06-15T15:03:00","slug":"how-can-i-insert-text-into-a-specific-column-of-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-insert-text-into-a-specific-column-of-a-text-file\/","title":{"rendered":"How Can I Insert Text Into a Specific Column of a Text 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 insert text into a specific column of a text file? I don\u2019t want the text to be inserted at the beginning of a line or at the end of a line, but at a specific spot <I>in<\/I> the line.<BR><BR>&#8212; D<\/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, D. You know, over the past two years we\u2019ve answered a lot of <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/textfiles.mspx\"><B>questions about text files<\/B><\/A>, and if there\u2019s one thing we\u2019ve learned in that time it\u2019s this: we should never answer any questions about text files. Yes, we know that a lot of people are interested in text files, and we know that the scripts to read and write these files are generally short and easy to create. Unfortunately, though, those scripts are usually anything but elegant. Instead of doing something cool, like calling the <B>InsertTextInThisSpot<\/B> method, we always have to resort to crazy little workarounds. Sure, we end up with a script that works, but, like we said, that script is never very elegant. And we Scripting Guys <I>want<\/I> to be elegant!<\/P>\n<P>Really? So you\u2019re saying that the baseball hat and the Seattle Mariners 2005 Spring Training shirt we wear each day <I>isn\u2019t<\/I> elegant? Interesting; that never occurred to us. And the old black sweatpants we wear on weekends? Well, what do you know?<\/P>\n<P>OK, D, maybe we\u2019re not quite as elegant as we thought we were. With that mind, we might as well go ahead and show you a script that can insert text into a specific spot in a line:<\/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>Do Until objFile.AtEndOfStream\n    strLine = objFile.ReadLine\n    intRight = Len(strLine) &#8211; 10\n    strRight = Right(strLine, intRight)\n    strLeft = Left(strLine, 10)\n    strInsert = &#8220;This is inserted text&#8221;\n    strText = strLeft &amp; strInsert &amp; strRight\n    strContents = strContents &amp; strText &amp; vbCrLf\nLoop<\/p>\n<p>objFile.Close<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)\nobjFile.WriteLine strContents<\/p>\n<p>objFile.Close\n<\/PRE>\n<P>Let\u2019s start out by giving you a quick overview of what this script does. For the sake of argument, let\u2019s assume your text file looks like this:<\/P><PRE class=\"codeSample\">000000000  111111111\n222222222  333333333\n444444444  555555555\n666666666  777777777\n888888888  999999999\n<\/PRE>\n<P>In other words, we have nine characters, two blank spaces, and then nine more characters. (Granted, that\u2019s an odd-looking text file, but setting it up this way will make it easier for us determine whether or not the script worked.) What we want to do is insert some text in between the two blank spaces. Because the first blank space is character 10, we want the inserted text to start at character (or column) 11. If everything goes well that will give us a line that looks like this:<\/P><PRE class=\"codeSample\">000000000 This is inserted text 111111111\n<\/PRE>\n<P>Can we do that? We\u2019re about to find out.<\/P>\n<P>Our script starts out by defining a pair of constants, ForReading and ForWriting; we\u2019ll use these constants to tell the script which mode (reading or writing) we want to use when opening the text file. This, by the way, is one of those less-than-elegant things we were talking about. For better or worse, we can\u2019t read and write to a text file at the same time; instead we\u2019re limited to reading from <I>or<\/I> writing to it. Therefore, we actually need to open this text file twice: once to read the existing contents and once to write the revised contents back to the file.<\/P>\n<P>After defining our constants we use these two lines of code to create an instance of the <B>Scripting<\/B>.<B>FileSystemObject <\/B>and then use the <B>OpenTextFile<\/B> method to open the file C:\\Scripts\\Test.txt 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>Seeing as how we opened the file for reading we figured we might as well start by reading in the contents. To do that we set up a Do loop that runs until we\u2019ve reached the end of the file (that is, until the <B>AtEndOfStream<\/B> property is True). Inside that loop we use the <B>ReadLine<\/B> method to read the first line from the file and store that value in a variable named strLine:<\/P><PRE class=\"codeSample\">strLine = objFile.ReadLine\n<\/PRE>\n<P>What that means, of course, is that strLine is currently equal to this: <\/P><PRE class=\"codeSample\">000000000  111111111\n<\/PRE>\n<P>Cute, but not exactly what we want for the revised text file. But how are we going to insert our text into the middle of that line?<\/P>\n<P>Here\u2019s how. We start out by using this line of code:<\/P><PRE class=\"codeSample\">intRight = Len(strLine) &#8211; 10\n<\/PRE>\n<P>Why? Well, we need to construct a new line for the text file. That new line will consist of the first 10 characters in the existing line, the new text we want to insert, and then the remaining characters from the existing line. To get at these remaining characters we need to start at the end of the string and count backwards until we get to character 10. That\u2019s fine, except for one thing: how far back are we supposed to count?<\/P>\n<P>That\u2019s what Len(strLine) &#8211; 10 tells us. The <B>Len<\/B> function tells us the total number of characters in the variable strLine; in this case that\u2019s equal to 20. We then subtract 10 from that amount. Why? Because we don\u2019t want to include the first 10 characters. Seeing as how 20 minus 10 equals 10, we now know that if we start at the end of the string and count backwards 10 characters we\u2019ll end up exactly where we need to end up.<\/P>\n<P>That\u2019s exactly what we do here:<\/P><PRE class=\"codeSample\">strRight = Right(strLine, intRight)\n<\/PRE>\n<P>In this line of code we use the <B>Right <\/B>function to take the last 10 characters (10 being the value stored in the variabled intRight) from the end of the string and store them in the variable strRight. In this sample script, that means strRight will equal this:<\/P><PRE class=\"codeSample\">111111111\n<\/PRE>\n<P>We then use the <B>Left<\/B> function to take the first 10 characters in strLine and store them in the variable strLeft. That means, for the first line in the text file, strLeft will equal this:<\/P>\n<P>000000000 <\/P>\n<P>Note that there\u2019s a blank space at the end of strLeft and a blank space at the beginning of strRight.<\/P>\n<P>And now, at long last, we can construct our new line. To do that, we store the text to be inserted into a variable named strInsert, then simply combine the values of strLeft, strInsert, and strRight (plus a carriage return-linefeed tacked on to the end). That\u2019s what we do here:<\/P><PRE class=\"codeSample\">strInsert = &#8220;This is inserted text&#8221;\nstrText = strLeft &amp; strInsert &amp; strRight\n<\/PRE>\n<P>After all that, guess what the value of strText is? Good guess:<\/P><PRE class=\"codeSample\">000000000 This is inserted text 111111111\n<\/PRE>\n<P>Yes, we know: that\u2019s a lot of work just to insert some text into a single line in a text file. Sadly, we\u2019re not quite finished, though. Because we can\u2019t write this line directly back to the text file (remember, it\u2019s currently open for reading only) we need to construct a new text file in memory, and then, later on, write the complete contents of that virtual text file to C:\\Scripts\\Test.txt. Therefore, we use this line of code to add the value of our revised text line to a variable named strContents:<\/P><PRE class=\"codeSample\">strContents = strContents &amp; strText &amp; vbCrLf\n<\/PRE>\n<P>As we loop through the text file, we\u2019ll continue appending values (lines of text) to strContents. When we\u2019re through, strContents will be a virtual representation of what we want Test.txt to look like.<\/P>\n<P>So what <I>does<\/I> happen when we\u2019re through with the text file? Well, first we close it, then we immediately re-open it, this time for writing:<\/P><PRE class=\"codeSample\">objFile.Close\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)\n<\/PRE>\n<P>Once the file is open we call the <B>WriteLine<\/B> method to write the value of strContents to the text file, and then close C:\\Scripts\\Test.txt for good. And what does that text file look like now? We\u2019re glad you asked:<\/P><PRE class=\"codeSample\">000000000 This is inserted text 111111111\n222222222 This is inserted text 333333333\n444444444 This is inserted text 555555555\n666666666 This is inserted text 777777777\n888888888 This is inserted text 999999999\n<\/PRE>\n<P>Now <I>that\u2019s<\/I> elegant.<\/P>\n<P>And so\u2019s the baseball hat, right? <\/P>\n<P>Right?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I insert text into a specific column of a text file? I don\u2019t want the text to be inserted at the beginning of a line or at the end of a line, but at a specific spot in the line.&#8212; D Hey, D. You know, over the past two years [&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-67103","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 insert text into a specific column of a text file? I don\u2019t want the text to be inserted at the beginning of a line or at the end of a line, but at a specific spot in the line.&#8212; D Hey, D. You know, over the past two years [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67103","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=67103"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67103\/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=67103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}