{"id":69763,"date":"2005-05-20T19:06:00","date_gmt":"2005-05-20T19:06:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/05\/20\/how-can-i-remove-the-last-carriage-return-linefeed-in-a-text-file\/"},"modified":"2005-05-20T19:06:00","modified_gmt":"2005-05-20T19:06:00","slug":"how-can-i-remove-the-last-carriage-return-linefeed-in-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-remove-the-last-carriage-return-linefeed-in-a-text-file\/","title":{"rendered":"How Can I Remove the Last Carriage Return-Linefeed in 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 remove the last carriage return-linefeed in a text file?<BR><BR>&#8212; LEK<\/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, LEK. From the rest of your email you note that you\u2019re using the FileSystemObject to read a text file, then using the contents of that text file as a query in another application. Unfortunately, though, the text file invariably has a carriage return-linefeed at the end, and that carriage return-linefeed is causing problems when you try passing the contents of the file as a query.<\/P>\n<P>What does all that mean? Well, consider the simple little text file below. We opened the file and then pressed <B>Ctrl+End<\/B> to position the cursor at the end of the file. Note that the cursor does not appear immediately after the period in line 1; instead, the cursor appears at the beginning of line 2. That\u2019s because there\u2019s a carriage return-linefeed immediately following the period (as if someone typed in the first line and then pressed ENTER). You might not see anything on the screen, but it\u2019s there and it\u2019s creating problems:<\/P><IMG height=\"143\" alt=\"Carriage Return-Linefeed\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/crlf.jpg\" width=\"246\" border=\"0\"> \n<P><BR>So how are we going to get rid of that pesky little carriage return-linefeed? Here\u2019s how:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>We\u2019re going to open the file and read the contents of that file into a variable named strFile.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>We\u2019re going to grab the last two characters stored in that variable. Why the last <I>two<\/I> characters? Well, the carriage return-linefeed actually consists of two separate characters: the carriage return (with an ASCII value of 13) and the linefeed (with an ASCII value of 10). Although we could probably get by with stripping away just the linefeed character we\u2019re going to do things nice and neat and get rid of both characters.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>We\u2019re going to check and see if the last characters have an ASCII value of 13 and an ASCII value of 10; an easy way to do that is to see if they are equal to the VBScript constant <B>vbCrLf<\/B> (which happens to be a carriage return-linefeed). If those last two characters are equal to vbCrLf, we\u2019ll chop the two characters off the string and save the new value (minus the carriage return-linefeed) back to the original text file. If they don\u2019t, then we won\u2019t do anything at all; in that case there\u2019s no reason to do anything.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Got all that? Here\u2019s a script that carries out all those tasks:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForWriting = 2<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;C:\\scripts\\test.txt&#8221;, ForReading)\nstrFile = objFile.ReadAll\nobjFile.Close<\/p>\n<p>intLength = Len(strFile)\nstrEnd = Right(strFile, 2)<\/p>\n<p>If strEnd = vbCrLf Then\n    strFile = Left(strFile, intLength &#8211; 2)\n    Set objFile = objFSO.OpenTextFile(&#8220;C:\\scripts\\test.txt&#8221;, ForWriting)\n    objFile.Write strFile\n    objFile.Close\nEnd If\n<\/PRE>\n<P>We start off by defining a pair of constants &#8211; ForReading and ForWriting &#8211; that we\u2019ll use when we work with the text file. We create an instance of the FileSystemObject and use the <B>OpenTextFile<\/B> method to open the file C:\\Scripts\\Test.txt. We then use the <B>ReadAll<\/B> method to read in the entire contents of the text file and store that information in a variable named strFile. After all that we immediately close the text file. (Why? Well, the FileSystemObject allows you to open a file for reading <I>or<\/I> for writing. Because we have to read the file we had to open it for reading; if we later need to make changes to the file we\u2019ll have to reopen it for writing. We\u2019re done reading the file so we might as well close it.)<\/P>\n<P>Next we use the <B>Len<\/B> function to determine the length of the variable strFile (that is, how many characters are in the string). This value &#8211; which we\u2019ll use in a minute &#8211; is stored in the variable intLength. <\/P>\n<P>So far so good, right? Now we need to determine whether or not the existing file has a carriage return-linefeed on the end. As we noted earlier, this means checking to see if the last two characters in the file consist of a carriage return character and a linefeed character. Thus we use the <B>Right<\/B> function to grab the two characters at the very end of the string and store these in the variable strEnd. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">strEnd = Right(strFile, 2)\n<\/PRE>\n<P>We can then check to see if strEnd is equal to vbCrLf. If it is, we use this line of code to grab all the characters <I>except<\/I> the two at the very end:<\/P><PRE class=\"codeSample\">strFile = Left(strFile, intLength &#8211; 2)\n<\/PRE>\n<P>Remember when we used the Len function to determine how many characters are in the string? Well, now we use that value to take the first <I>length &#8211; 2<\/I> characters; for example, if our string has 37 characters, we\u2019ll take only the first 35 (37 &#8211; 2). That will effectively chop the carriage return-linefeed off the end of the string.<\/P>\n<P>We then reopen our text file &#8211; this time for writing &#8211; and replace the existing contents with the new string value. That will leave us with our original text file, <I>minus<\/I> the trailing carriage return-linefeed. And that\u2019s just exactly what you were hoping to end up with.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I remove the last carriage return-linefeed in a text file?&#8212; LEK Hey, LEK. From the rest of your email you note that you\u2019re using the FileSystemObject to read a text file, then using the contents of that text file as a query in another application. Unfortunately, though, the text file [&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-69763","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 remove the last carriage return-linefeed in a text file?&#8212; LEK Hey, LEK. From the rest of your email you note that you\u2019re using the FileSystemObject to read a text file, then using the contents of that text file as a query in another application. Unfortunately, though, the text file [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69763","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=69763"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69763\/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=69763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}