I'm looking for a VBA code (macro) to merge text files.

grid

Board Regular
Joined
Dec 4, 2009
Messages
56
Hello,

I'm looking for a VBA code (macro). I want it a whole folder of text files to merge into 1 text file. So far have found nothing. Maybe someone can help me?

I want the folder in the VBA code included.


Best regards,

grid

:)
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Here's one way:

Code:
Dim strPath As String

strPath = "C:\Some folder\Some other folder\"   'don't forget the closing \!

Shell "cmd.exe copy /c """ & strPath & "*.txt"" """ & strPath & "Consolidated.txt""",0

It will merge all text files in the given folder into an output consolidated file in the same folder named Consolidated.txt.
 
Upvote 0
By merge you mean append?

If you're just looking to jam all text files, back to back, in one text file creating a little batch file may be an easier option than excel.

I don't have code handy, but it can't be more than a few lines. Just get code off google, paste into Notepad, save, and change extension to *.bat and run inside the folder containing the text (a copy to be safe).
 
Upvote 0
Hello RichardSchollar,

Thank you. I run it as a procedure. It is necessary to complement my other VBA code.

The files that make Consolidated.txt change every so often. I will merge a few more times in a while. I have not run your script. But the dos screen you can also run in the background? So that you do not see the taskbar.

I want the script to run automatically every 10 seconds via:

Sub MySub ()
Application.OnTime Now + TimeValue ( "00:00:10"), 'Merge'
End Sub

Can you also make calls through the procedure Merge?


Thank you,

grid

:)
 
Upvote 0
Hello RichardSchollar,

It has been resolved. I'll figure it out.


Best regards,

grid

:)
 
Upvote 0
I found these on another site in case anyone is still searching for a solution.

Code:
Sub CombineFiles()    Shell Environ$("COMSPEC") & " /c Copy C:\temp\*.tmp C:\temp\CombinedFile.tmp"
End Sub

Code:
Sub CombineFiles2()    c00 = "C:\temp\" ' the path
    c01 = Dir(c00 & "*.tmp")
     
    Do Until c01 = ""
        c02 = c02 & vbCrLf & CreateObject("scripting.filesystemobject").opentextfile(c00 & c01).readall
        c01 = Dir
    Loop
     
    CreateObject("scripting.filesystemobject").createtextfile(c00 & "_new.tmp").write c02
End Sub
 
Upvote 0
Hi there. I'm trying to do the same thing. I used
Code:
Shell Environ$("COMSPEC") & " /c Copy C:\temp\*.tmp C:\temp\CombinedFile.tmp"

and


Code:
Shell "cmd.exe copy /c """ & strPath & "*.txt"" """ & strPath & "Consolidated.txt""",0

As long and I use C:\test folder, everything works out just fine. But when I use this:

Code:
folder = ActiveWorkbook.Path & "\output"
Shell Environ$("COMSPEC") & " /c Copy " & folder & "\*.scr " & folder & "\CombinedFile.scr"

nothing happens. I'm not sure what I'm doing wrong here.
 
Upvote 0

Forum statistics

Threads
1,215,752
Messages
6,126,672
Members
449,327
Latest member
John4520

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top