{"id":69483,"date":"2005-06-30T22:22:00","date_gmt":"2005-06-30T22:22:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/06\/30\/how-can-i-add-a-line-to-the-top-of-a-text-file\/"},"modified":"2005-06-30T22:22:00","modified_gmt":"2005-06-30T22:22:00","slug":"how-can-i-add-a-line-to-the-top-of-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-add-a-line-to-the-top-of-a-text-file\/","title":{"rendered":"How Can I Add a Line to the Top of a Text File?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I add a line to the top of a text file?<BR><BR>&#8212; FT<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, FT. You know, at one point in his career Sir Arthur Conan Doyle actually killed off Sherlock Holmes, figuring he\u2019d written everything anyone could ever write about the master detective. Public outcry soon made him change his mind, and in short order he brought Sherlock Holmes back to life. (Creating, along the way, the basic plotline for every soap opera ever written.)<\/P>\n<P>We Scripting Guys can empathize with Sir Arthur Conan Doyle. After all, we periodically think, \u201cWell, that\u2019s it; we\u2019ve finally written everything anyone could ever write about text files.\u201d No sooner do we think that than we seem to get a flurry of questions about text files, including three people who asked this same thing: how can I add a line to the top of a text file?<\/P>\n<P>Elementary, my dear Watson (uh, my dear FT). All you have to do is use a script similar to this:<\/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>strFirstLine = &#8220;This is the new first line in the text file.&#8221;\nstrNewContents = strFirstLine &amp; vbCrLf &amp; strContents<\/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<TABLE id=\"E2C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Interestingly enough, in the original stories Sherlock Homes never said, \u201cElementary, my dear Watson.\u201d No, we don\u2019t know <I>why<\/I> he didn\u2019t say it; he just didn\u2019t.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Ah, yes, now the game <I>is<\/I> afoot, isn\u2019t it? We begin by creating a pair of constants &#8211; ForReading and ForWriting &#8211; that we\u2019ll use when working with our text file. We create an instance of the <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 objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)\n<\/PRE>\n<P>Now that the file is open we use the <B>ReadAll<\/B> method to read the entire contents of the file and store those contents in a variable named strContents. We then immediately close the file Test.txt. Why? Well, the FileSystemObject allows you to open a file for reading <I>or<\/I> for writing, but you can\u2019t do both at the same time. To add a new line to the top of the file, we\u2019re going to have to write to the file; that means we\u2019ll have to reopen it, but for writing.<\/P>\n<P>Next we need to construct the <I>new<\/I> contents for the file. We can\u2019t directly add a line to the top of the text file; the FileSystemObject only allows you to add new lines to the <I>end<\/I> of a text file. Therefore, what we\u2019ll have to do is create an entirely new file in memory and then replace the existing contents of Test.txt with this new file. Our new file will consist of three parts: a new first line; a carriage return-linefeed; and the existing contents of the file. To construct this file we start off by using this code to store the new first line in a variable named strFirstLine:<\/P><PRE class=\"codeSample\">strFirstLine = &#8220;This is the new first line in the text file.&#8221;\n<\/PRE>\n<P>We then use this line of code to concatenate the new first line, a carriage return-linefeed (using the VBScript constant vbCrLf) and the existing contents of the file (which we stored in the variable strContents):<\/P><PRE class=\"codeSample\">strNewContents = strFirstLine &amp; vbCrLf &amp; strContents\n<\/PRE>\n<P>All that\u2019s left now is to reopen Test.txt (for writing this time), then use the WriteLine method to replace the exiting contents with our new file:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)\nobjFile.WriteLine strNewContents\n<\/PRE>\n<P>We then call the <B>Close<\/B> method and, just like that, case closed. Well, OK, <I>file<\/I> closed. Hey, we\u2019re just trying to do what Sherlock Holmes would do.<\/P>\n<P>Speaking of which, did you know that Sherlock Holmes had an older brother named Mycroft? It\u2019s true. Supposedly Mycroft had a lot of potential, but was too lazy to really do anything interesting or useful.<\/P>\n<P>Hey, what do you mean that sounds familiar? Never mind; you\u2019re probably thinking about some other daily scripting columnist.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I add a line to the top of a text file?&#8212; FT Hey, FT. You know, at one point in his career Sir Arthur Conan Doyle actually killed off Sherlock Holmes, figuring he\u2019d written everything anyone could ever write about the master detective. Public outcry soon made him change his [&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":[40,3,4,14,5],"class_list":["post-69483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-filesystemobject","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I add a line to the top of a text file?&#8212; FT Hey, FT. You know, at one point in his career Sir Arthur Conan Doyle actually killed off Sherlock Holmes, figuring he\u2019d written everything anyone could ever write about the master detective. Public outcry soon made him change his [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69483","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=69483"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69483\/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=69483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}