{"id":65823,"date":"2007-01-02T17:02:00","date_gmt":"2007-01-02T17:02:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/01\/02\/how-can-i-append-a-blank-line-to-a-text-file-then-append-the-current-date-and-time\/"},"modified":"2007-01-02T17:02:00","modified_gmt":"2007-01-02T17:02:00","slug":"how-can-i-append-a-blank-line-to-a-text-file-then-append-the-current-date-and-time","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-append-a-blank-line-to-a-text-file-then-append-the-current-date-and-time\/","title":{"rendered":"How Can I Append a Blank Line to a Text File, Then Append the Current Date and Time?"},"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 append a blank line to a text file, then append the current date and time?<BR><BR>&#8212; PL<\/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, PL. You know, back in the 1600s people were inordinately worried about witches and witchcraft. Fortunately, though, they developed a sure-fire test to determine whether or not someone was a witch: the accused was weighted down with rocks, then tossed into a lake. If they floated back up to the top they were a witch; after all, no one can float in a lake if they\u2019ve been weighted down with rocks, at least not unless they were in league with the Devil. In that case the accused would be hauled out of the lake and hung. (No, we\u2019re not completely sure why their magic would work when weighted down with rocks and tossed in a lake, yet be useless when faced with the hangman\u2019s noose.)<\/P>\n<P>If you sank to the bottom and died, well, that was actually <I>good<\/I>; that meant you weren\u2019t a witch. In turn, that also meant that you were entitled to an honorable Christian burial. Hooray!<\/P>\n<P>Now, you might think that people were troubled by this: after all, if you were accused of witchcraft then, one way or the other, you were going to end up dead. But, for the most part, people didn\u2019t lose much sleep over the matter. After all, if you were truly innocent then no one would accuse you of being a witch, would they? To be blunt, this was all your fault: you must have done <I>something<\/I> to warrant such serious accusations.<\/P>\n<P>So how did an innocuous little question about text files get us started thinking about things like the <A href=\"http:\/\/www.salemwitchtrials.com\/\" target=\"_blank\"><B>Salem Witch Trials<\/B><\/A>? That\u2019s easy: when we saw a question about modifying a text file we immediately launched into accusatory mode. \u201cYou know, that\u2019s way harder than you might think. There\u2019s really no easy and obvious way to do this. Don\u2019t blame us; the FileSystemObject wasn\u2019t designed to handle situations like this.\u201d Without even blinking we ran through all our standard warnings, caveats and excuses about why it\u2019s so hard to modify the text in a text file. <\/P>\n<P>It was only after we unleashed this torrent of accusations that we realized that you don\u2019t really want to modify a text file, you just want to tack some additional information onto the tail end of that file. As it turns out, that <I>is<\/I> easy:<\/P><PRE class=\"codeSample\">Const ForAppending = 8<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForAppending)<\/p>\n<p>objFile.WriteLine\nobjFile.WriteLine\nobjFile.Write Now<\/p>\n<p>objFile.Close\n<\/PRE>\n<P>By the way, sorry about weighting you down with rocks and tossing you in the lake just because we thought you were asking a question about modifying text files. Would it help if we offered you an honorable Christian burial?<\/P>\n<P>That\u2019s what we were afraid you\u2019d say. OK, then what do you say we explain how this script works? Let\u2019s assume that we have a text file that looks like this:<\/P><PRE class=\"codeSample\">A\nB\nC\n<\/PRE>\n<P>Is our script <I>really<\/I> going to be able to add a blank line and a timestamp to the end of this file? Of course, it \u2013 hey, wait a minute: that sounds like something a <I>witch<\/I> would say. Hmmm \u2026.<\/P>\n<P>Actually there\u2019s very little magic involved here. We begin by defining a constant named ForAppending and setting to value to 8; we\u2019ll use this constant to specify the correct mode when opening our text file. 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 appending:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForAppending)\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EHE\" 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>. Why \u201cfor appending?\u201d That\u2019s easy. If you open a file for <I>writing<\/I> the FileSystemObject will erase the existing contents of the file and replace the old contents with whatever new stuff you\u2019re adding. When you open a file for appending, however, new content is simply tacked on to the end of the file.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>That was pretty easy; believe it or not, from here on it gets even <I>easier<\/I>. To add a blank line to the file we call the <B>WriteLine<\/B> method twice, without supplying any parameters; each time we call WriteLine in that fashion (that is, without parameters) that\u2019s equivalent to pressing ENTER on the keyboard. (And pressing ENTER twice will give you a blank line in a document.) We then call the <B>Write<\/B> method, appending the value of the VBScript <B>Now<\/B> function to the file. Because Now provides us with the current date and time, we end up with a file that looks like this:<\/P><PRE class=\"codeSample\">A\nB\nC<\/p>\n<p>1\/2\/2007 8:07:21 AM\n<\/PRE>\n<P>All that\u2019s left now is to close the file and call it a day.<\/P>\n<P>Before we go, we feel compelled to note that we 21<SUP>st<\/SUP> century types shouldn\u2019t feel smug and superior towards people of the 1600s; the truth is, things haven\u2019t changed all that much in the past 400 years. For example, that part about weighting people down with rocks, tossing them in a lake, and then standing around waiting to see what happens? That\u2019s the exact same method Microsoft uses when conducting annual performance reviews. Like they say, the more things change the more they stay the same. <\/P>\n<TABLE class=\"dataTable\" id=\"EKF\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Editor\u2019s Note:<\/B> No, Microsoft doesn\u2019t <I>really<\/I> do that. That would mean everyone who survives a review manages to float to the top, and we certainly wouldn\u2019t want anyone to jump to any conclusions as to how they managed to do that.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jan07\/hey0102.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jan07\/hey0102.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I append a blank line to a text file, then append the current date and time?&#8212; PL Hey, PL. You know, back in the 1600s people were inordinately worried about witches and witchcraft. Fortunately, though, they developed a sure-fire test to determine whether or not someone was a witch: the [&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":[13,3,4,14,5],"class_list":["post-65823","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I append a blank line to a text file, then append the current date and time?&#8212; PL Hey, PL. You know, back in the 1600s people were inordinately worried about witches and witchcraft. Fortunately, though, they developed a sure-fire test to determine whether or not someone was a witch: the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65823","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=65823"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65823\/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=65823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}