{"id":70373,"date":"2005-02-23T15:23:00","date_gmt":"2005-02-23T15:23:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/02\/23\/how-can-i-read-text-from-a-file-and-then-rename-the-file-using-the-text-i-just-read\/"},"modified":"2005-02-23T15:23:00","modified_gmt":"2005-02-23T15:23:00","slug":"how-can-i-read-text-from-a-file-and-then-rename-the-file-using-the-text-i-just-read","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-read-text-from-a-file-and-then-rename-the-file-using-the-text-i-just-read\/","title":{"rendered":"How Can I Read Text From a File and Then Rename the File Using the Text I Just Read?"},"content":{"rendered":"<p><P>Guy! I would like to be able to open a file, read the first 10 characters, and then rename the file to those 10 characters plus a .txt file extension. How can I do that?<BR><BR>&#8212; KA<\/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, KA. Ah, for once a text file question that can be answered without some weird and convoluted workaround. This is actually pretty easy: we can use the FileSystemObject to open the text file and read in the first 10 characters, then use the FileSystemObject to rename the file to those 10 characters plus a .txt file extension. As you can see, all that takes just a few lines of code:<\/P><PRE class=\"codeSample\">Const ForReading = 1<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Log.txt&#8221;, ForReading)\nstrCharacters = objFile.Read(10)\nobjFile.Close<\/p>\n<p>strNewName = &#8220;C:\\Scripts\\&#8221; &amp; strCharacters &amp; &#8220;.txt&#8221;<\/p>\n<p>objFSO.MoveFile &#8220;C:\\Scripts\\Log.txt&#8221;, strNewName\n<\/PRE>\n<P>We begin by defining a constant named ForReading and assigning it the value 1; this constant is used when we open the text file for reading. We then create an instance of the FileSystemObject and use the <B>OpenTextFile<\/B> method to open the file C:\\Scripts\\Log.txt. With the file open we then read the first 10 characters and store that data on the variable strCharacters:<\/P><PRE class=\"codeSample\">strCharacters = objFile.Read(10)\n<\/PRE>\n<P>As you can see, we can use the <B>Read<\/B> method to read a specified number of characters from a text file. Suppose we wanted to read the first 37 characters from the file? No problem; we\u2019d just use this line of code:<\/P><PRE class=\"codeSample\">strCharacters = objFile.Read(37)\n<\/PRE>\n<P>After closing the file we construct a new file name, one consisting of the folder path (<B>C:\\Scripts\\<\/B>), the 10 characters we just read in (represented by the variable strCharacters), and the new file extension (<B>.txt<\/B>). That, too requires just one line of code:<\/P><PRE class=\"codeSample\">strNewName = &#8220;C:\\Scripts\\&#8221; &amp; strCharacters &amp; &#8220;.txt&#8221;\n<\/PRE>\n<P>All that\u2019s left is to rename the file. We can do that by using the FileSystemObject\u2019s <B>MoveFile<\/B> method. (Don\u2019t let the name fool you; in this case the file is simply being \u201cmoved\u201d from its old name to its new name; it\u2019s not changing locations within the file system). We pass MoveFile two parameters: the current path to the file, and the new path to the file. Sounds crazy, but \u201cmoving\u201d a file within the same folder has the net effect of renaming it. Weird but true!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Guy! I would like to be able to open a file, read the first 10 characters, and then rename the file to those 10 characters plus a .txt file extension. How can I do that?&#8212; KA Hey, KA. Ah, for once a text file question that can be answered without some weird and convoluted workaround. [&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-70373","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>Guy! I would like to be able to open a file, read the first 10 characters, and then rename the file to those 10 characters plus a .txt file extension. How can I do that?&#8212; KA Hey, KA. Ah, for once a text file question that can be answered without some weird and convoluted workaround. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70373","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=70373"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70373\/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=70373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}