{"id":70903,"date":"2004-12-01T19:09:00","date_gmt":"2004-12-01T19:09:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/12\/01\/can-i-combine-multiple-text-files-using-a-script\/"},"modified":"2004-12-01T19:09:00","modified_gmt":"2004-12-01T19:09:00","slug":"can-i-combine-multiple-text-files-using-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/can-i-combine-multiple-text-files-using-a-script\/","title":{"rendered":"Can I Combine Multiple Text Files Using a Script?"},"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! From the command prompt the command <B>copy a.txt+b.txt ab.txt<\/B> will take the contents of a.txt and the contents of b.txt and combine them into a new file named ab.txt. Can I do the same thing with a script?<BR><BR>&#8212; DL<\/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, DL. In yesterday\u2019s column we dealt with text files; more specifically, we talked about how you can use a script to modify .INI files. We told you that the solution wasn\u2019t very elegant, but that it would do the job. Well, we have the same situation here. Can we use a script to combine text files? Yes we can. We have to do it in a slightly cumbersome way, but it\u2019ll work just fine.<\/P>\n<P>The problem we face is that neither WSH nor VBScript has a way to combine text files with a single command (e.g., objFile.AddTextFiles(\u201cfile1.log\u201d,\u201dfile2.log\u201d). That\u2019s a shame, but it won\u2019t stop us from combining text files; we\u2019ll just have to go through a few extra steps in order to do this. For example, to combine File1.log and File2.log into a single file (which we\u2019ll call Output.txt) we\u2019ll have to first read File1.log and append the contents of that file to output.txt, then we\u2019ll have to read File2.log and append the contents of <I>that<\/I> file to Output.txt. In fact, we\u2019ll have to use a script that looks a lot like this:<\/P><PRE class=\"codeSample\">Const ForReading = 1<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objOutputFile = objFSO.CreateTextFile(&#8220;output.txt&#8221;)<\/p>\n<p>Set objTextFile = objFSO.OpenTextFile(&#8220;c:\\logs\\file1.log&#8221;, ForReading)<\/p>\n<p>strText = objTextFile.ReadAll\nobjTextFile.Close\nobjOutputFile.WriteLine strText<\/p>\n<p>Set objTextFile = objFSO.OpenTextFile(&#8220;c:\\logs\\file2.log &#8220;, ForReading)<\/p>\n<p>strText = objTextFile.ReadAll\nobjTextFile.Close\nobjOutputFile.WriteLine strText<\/p>\n<p>objOutputFile.Close\n<\/PRE>\n<P>As you can see, this isn\u2019t exactly rocket science. We start out by defining a constant (ForReading) that we\u2019ll use to open each log file. We then create an instance of the FileSystemObject (the scripting technology used to manipulate text files) and use the CreateTextFile method to create a new file named Output.txt.<\/P>\n<P>After that, we open our first file (C:\\Logs\\File1.log) for reading. We use the ReadAll method to read in the entire text file and store that information in the variable strText. We close File1.log, then use the WriteLine method to append the information we just read in to our new file, Output.txt. We then repeat the process for the next file (C:\\Logs\\File2.log). After we\u2019ve read in this second file, Output.txt will consist of all the information found in the first file <I>plus<\/I> all the information found in the second file. Hey, we did it!<\/P>\n<P>Ok, ok, we know what you\u2019re thinking: sure, the preceding script works, but it also requires you to know &#8211; in advance &#8211; the names of all the files in the folder C:\\Logs. Wouldn\u2019t it be better to have a script that could grab all the files in C:\\Logs and then combine them all for us? Hmmm, we never thought about that. You mean something like this:<\/P><PRE class=\"codeSample\">Const ForReading = 1<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objOutputFile = objFSO.CreateTextFile(&#8220;output.txt&#8221;)<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set FileList = 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 FileList\n    Set objTextFile = objFSO.OpenTextFile(objFile.Name, ForReading) \n    strText = objTextFile.ReadAll\n    objTextFile.Close\n    objOutputFile.WriteLine strText\nNext<\/p>\n<p>objOutputFile.Close\n<\/PRE>\n<P>All we\u2019re really doing here is getting a collection of all the files in the C:\\Logs folder; that\u2019s what this WMI Associators of query does:<\/P><PRE class=\"codeSample\">Set FileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Logs&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)\n<\/PRE>\n<P>As soon as we have this collection, we use a For-Each loop to open each file and then read in the text (using the ReadAll method, just like we did before). We close the file, and append the text to our output file. We then loop around and repeat this process for the next file in the collection. In no time at all, we\u2019ll have taken all the text from all the files in C:\\Logs and combined into a new file named output.txt. It\u2019s that easy.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! From the command prompt the command copy a.txt+b.txt ab.txt will take the contents of a.txt and the contents of b.txt and combine them into a new file named ab.txt. Can I do the same thing with a script?&#8212; DL Hey, DL. In yesterday\u2019s column we dealt with text files; more specifically, we [&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-70903","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! From the command prompt the command copy a.txt+b.txt ab.txt will take the contents of a.txt and the contents of b.txt and combine them into a new file named ab.txt. Can I do the same thing with a script?&#8212; DL Hey, DL. In yesterday\u2019s column we dealt with text files; more specifically, we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70903","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=70903"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70903\/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=70903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}