{"id":68693,"date":"2005-10-21T14:29:00","date_gmt":"2005-10-21T14:29:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/10\/21\/how-can-i-read-a-text-file-on-a-remote-computer\/"},"modified":"2005-10-21T14:29:00","modified_gmt":"2005-10-21T14:29:00","slug":"how-can-i-read-a-text-file-on-a-remote-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-read-a-text-file-on-a-remote-computer\/","title":{"rendered":"How Can I Read a Text File on a Remote Computer?"},"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 read a text file on a remote computer?<BR><BR>&#8212; BM<\/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, BM. We have a confession to make: we\u2019re using you and your question for our own nefarious purposes. <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/oct05\/hey1018.mspx\"><B>Earlier this week<\/B><\/A> we answered a question about reading the last line in a group of text files; in our answer we promised we\u2019d show people how to use the FileSystemObject to perform that same feat on a remote computer. That was fine, except that &#8211; in order to maintain the spirit of this column &#8211; we couldn\u2019t do that unless we had a question about working with text files on a remote computer. So we chose yours.<\/P>\n<P>So, yes, we\u2019re using you. But on the bright side, you\u2019ll get your question answered. In fact, let\u2019s start with your question first.<\/P>\n<P>As you know, the FileSystemObject (the object used for reading and writing text files) is designed to work locally; in fact, pretty much anything and everything you read about the FileSystemObject will take great pains to point out that you can\u2019t use the object against remote machines. As it turns out, though, that\u2019s not entirely true: that\u2019s because the FileSystemObject <I>is<\/I> capable of using UNC paths. Suppose the file you want to read is located on a file share (\\\\atl-fs-01\\public\\myfile.txt). In that case, opening and reading the text file is as easy as this:<\/P><PRE class=\"codeSample\">Const ForReading = 1<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>Set objTextFile = objFSO.OpenTextFile _\n    (&#8220;\\\\atl-fs-01\\public\\myfile.txt&#8221;, ForReading)\nstrContents = objTextFile.ReadAll\nobjTextFile.Close<\/p>\n<p>Wscript.Echo strContents\n<\/PRE>\n<P>As you can see, we begin by defining a constant named ForReading and setting the value to 1. We then create the FileSystemObject reference and call the <B>OpenTextFile<\/B> method, passing two parameters: the UNC path to the file in question, and the constant ForReading. At this point we can do anything we want with the file: Just to give you a quick example of working with a text file we call the <B>ReadAll<\/B> method to read the entire contents of the file into a variable named strContents. We then close the file, echo the value of strContents, and we\u2019re done.<\/P>\n<P>Now, this works great as long as the file in question is in a shared folder. But what if the file is <I>not<\/I> in a shared folder? In that case, your only recourse is to use the Administrative shares (like C$). This script will read the file MyFile.txt even if the folder C:\\Public has not been shared:<\/P><PRE class=\"codeSample\">Const ForReading = 1<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>Set objTextFile = objFSO.OpenTextFile _\n    (&#8220;\\\\atl-fs-01\\C$\\public\\myfile.txt&#8221;, ForReading)\nstrContents = objTextFile.ReadAll\nobjTextFile.Close<\/p>\n<p>Wscript.Echo strContents\n<\/PRE>\n<P>If you don\u2019t use Administrative shares, well, then you\u2019re probably out of luck. (Unless you do something <I>really<\/I> crazy, like use the WSHController object. But that\u2019s a story for another time.)<\/P>\n<P>And that brings us to our ulterior motive: how can we cycle through all the files in a remote folder and use the FileSystemObject to open and read each of those files? Well, here\u2019s one way:<\/P><PRE class=\"codeSample\">Const ForReading = 1<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>strComputer = &#8220;atl-fs-01&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Logs&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>For Each objFile In colFileList\n    strFilePath = &#8220;\\\\&#8221; &amp; strComputer &amp; &#8220;\\C$\\Logs\\&#8221; &amp; _\n        objFile.FileName &amp; &#8220;.&#8221; &amp; objFile.Extension\n    Set objTextFile = objFSO.OpenTextFile(strFilePath, ForReading)\n    strContents = objTextFile.ReadAll\n    Wscript.Echo strContents\n    objTextFile.Close\nNext\n<\/PRE>\n<P>What we\u2019re doing here is connecting to the remote computer atl-fs-01 and retrieving a collection of all the files in the folder C:\\Logs. The tricky part lies in constructing a path to each file; that\u2019s because we need to use an Administrative share path similar to this:<\/P><PRE class=\"codeSample\">\\\\atl-fs-01\\C$\\Logs\\MyFile.log\n<\/PRE>\n<P>To construct that path we use a little bit of WMI and a little bit of hard-coding:<\/P><PRE class=\"codeSample\">strFilePath = &#8220;\\\\&#8221; &amp; strComputer &amp; &#8220;\\C$\\Logs\\&#8221; &amp; _\n    objFile.FileName &amp; &#8220;.&#8221; &amp; objFile.Extension\n<\/PRE>\n<P>What we\u2019re doing is this:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Starting with a pair of \\\u2019s: <B>\\\\<\/B><\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Adding the name of the computer: <B>\\\\atl-fs-01<\/B><\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Adding a \\ and the Administrative share path C$\\Logs\\: <B>\\\\atl-fs-01\\C$\\Logs\\<\/B><\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Adding the WMI <B>FileName<\/B> property (which is just the name, minus the file extension):<B> \\\\atl-fs-01\\C$\\Logs\\MyFile<\/B><\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Adding the period between the file name and the file extension (because the period is not part of WMI\u2019s <B>Extension<\/B> property): <B>\\\\atl-fs-01\\C$\\Logs\\MyFile.<\/B><\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Adding the WMI property <B>Extension<\/B>: <B>\\\\atl-fs-01\\C$\\Logs\\MyFile.log<\/B><\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>It\u2019s a tad bit complicated, but it builds the UNC path we need. Furthermore, each time through the loop it will substitute a new file name and new file extension (the computer name and the folder path never change). Thus we\u2019ll eventually be able to open &#8211; and read &#8211; each file in the remote folder.<\/P>\n<P>By the way, thanks for letting us take advantage of your question, BM. We owe you one!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I read a text file on a remote computer?&#8212; BM Hey, BM. We have a confession to make: we\u2019re using you and your question for our own nefarious purposes. Earlier this week we answered a question about reading the last line in a group of text files; in our answer [&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-68693","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 read a text file on a remote computer?&#8212; BM Hey, BM. We have a confession to make: we\u2019re using you and your question for our own nefarious purposes. Earlier this week we answered a question about reading the last line in a group of text files; in our answer [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68693","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=68693"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68693\/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=68693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}