{"id":66723,"date":"2006-08-09T13:38:00","date_gmt":"2006-08-09T13:38:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/09\/how-can-i-concatenate-files-of-the-same-name\/"},"modified":"2006-08-09T13:38:00","modified_gmt":"2006-08-09T13:38:00","slug":"how-can-i-concatenate-files-of-the-same-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-concatenate-files-of-the-same-name\/","title":{"rendered":"How Can I Concatenate Files of the Same Name?"},"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 concatenate all the files (taken from various folders) that have the same file name?<BR><BR>&#8212; AS<\/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, AS. You know, the hardest part about selecting questions to be used in the <I>Hey, Scripting Guy!<\/I> column isn\u2019t writing the scripts; instead, it\u2019s figuring out how to go about <I>testing<\/I> the scripts. After all, many people write to us with questions we find both interesting and useful; unfortunately, in order to test them we\u2019d have to set up two separate Active Directory forests, install both a Unix server and a Novell server, go back in time to the year 1542, and temporarily swap the orbits of Mars and Jupiter. Even for the Scripting Guys, that\u2019s asking a lot. <\/P>\n<P>At first glance, you might think that this question would fall into the same category; after all, how many people already have files with the same file name scattered all over their hard disk? Fortunately, though, the Scripting Guy who writes this column is lacking in both ambition and creativity; as it turns out, he had 49 different files named test.vbs located on his C: drive alone. That made it very easy for him to write\u2014and then test\u2014a script that grabbed all 49 of these files (regardless of location) and concatenated the contents into a file named C:\\Scripts\\Main_file.txt.<\/P>\n<P>Interestingly enough, that was a script that looked an awful lot like this:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForAppending = 8<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * from CIM_Datafile Where FileName = &#8216;test&#8217; AND Extension = &#8216;vbs'&#8221;)<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>Set objMainFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Main_file.txt&#8221;, ForAppending, True)<\/p>\n<p>For Each objFile in colFiles\n    Set objFile = objFSO.OpenTextFile(objFile.Name, ForReading)\n    strContents = objFile.ReadAll\n    objFile.Close\n    objMainFile.WriteLine strContents\nNext<\/p>\n<p>objMainFile.Close\n<\/PRE>\n<P>To begin with, we define a pair of constants: ForReading, which we\u2019ll use each time we open an instance of Test.vbs; and ForAppending, which we\u2019ll use each time we add the contents of a Test.vbs file to Main_file.txt. Following that, we bind to the WMI service on the local computer, then use this line of code to retrieve a collection of all the files on the computer that have a <B>FileName<\/B> equal to <I>test<\/I> and a file <B>Extension<\/B> equal to <I>vbs<\/I> (note that it\u2019s just the letters <I>vbs<\/I> and not <I>.vbs<\/I>, with a dot in front of it):<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * from CIM_Datafile Where FileName = &#8216;test&#8217; AND Extension = &#8216;vbs'&#8221;)\n<\/PRE>\n<P>This is the point where we usually say, \u201cOf course, seeing as how we\u2019re using WMI, you could just as easily run this script against a remote computer.\u201d However, we aren\u2019t going to say that this time around. Granted, we <I>could<\/I> use WMI to retrieve the equivalent collection of files from a remote computer. However, we\u2019ll run into problems trying to <I>open<\/I> those files; that\u2019s because the FileSystemObject isn\u2019t designed to work with files stored on remote computers. Consequently, this particular script is for the local machine only. <\/P>\n<TABLE id=\"ECE\" 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>. Of course, you could always try modifying the script so that it <I>does<\/I> work with files stored on remote computers. For a few hints on how to do that take a look at this <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/oct05\/hey1021.mspx\"><B>Hey, Scripting Guy! column<\/B><\/A>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>With our collection in hand we create an instance of the <B>Scripting.FileSystemObject<\/B>, then use this line of code to open the file C:\\Scripts\\Main_file.txt:<\/P><PRE class=\"codeSample\">Set objMainFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Main_file.txt&#8221;, ForAppending, True)\n<\/PRE>\n<TABLE id=\"E3E\" 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>. What\u2019s the second parameter \u2013 <B>True<\/B> \u2013 that we added to the <B>OpenTextFile<\/B> method? As it turns out, that\u2019s an optional second parameter that tells the FileSystemObject to create the file Main_file.txt if that file doesn\u2019t already exist.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Once the file is open we set up a For Each loop to walk through the previously-retrieved collection of files that have the name Test.vbs. Inside that loop we start off by opening the file in question (passing OpenTextFile the <B>Name<\/B> of the file, which is equivalent to the file path) and then reading the entire contents into a variable named strContents. That\u2019s what we do with these two lines of code:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.OpenTextFile(objFile.Name, ForReading)\nstrContents = objFile.ReadAll\n<\/PRE>\n<P>After closing the first instance of Test.vbs, we then use the <B>WriteLine<\/B> method to append the value of strContents to the file Main_file.txt:<\/P><PRE class=\"codeSample\">objMainFile.WriteLine strContents\n<\/PRE>\n<P>And then we loop around and repeat the process with the next instance of Test.vbs in the collection. After we\u2019ve looped through the entire collection we close the file Main_file.txt and call it a day.<\/P>\n<P>This should answer your question, AS. Oh, and by the way: thanks for asking a question that didn\u2019t require us to go back in time. The last time we did that we went back to the year 1742 and \u2013 much to our horror \u2013 the first person we encountered was Scripting Guy Peter Costantini as a child. Hopefully that meeting won\u2019t do anything to change the future. (Although, based on the direction the Scripting Guys are headed, maybe changing the future wouldn\u2019t be such a bad thing.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I concatenate all the files (taken from various folders) that have the same file name?&#8212; AS Hey, AS. You know, the hardest part about selecting questions to be used in the Hey, Scripting Guy! column isn\u2019t writing the scripts; instead, it\u2019s figuring out how to go about testing the scripts. [&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-66723","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 concatenate all the files (taken from various folders) that have the same file name?&#8212; AS Hey, AS. You know, the hardest part about selecting questions to be used in the Hey, Scripting Guy! column isn\u2019t writing the scripts; instead, it\u2019s figuring out how to go about testing the scripts. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66723","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=66723"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66723\/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=66723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}