Combining 2 txt files into 1 via VBA

  • Thread starter Thread starter Chedva
  • Start date Start date
C

Chedva

My export program generate 2 txt files out of 2 of my tables. I need to
combine the two into 1 table (I don't want to do it with the union b'c I do
several things before combining. How do I create a new one file out of the 2
old ones. (I tried to work with the FileSystemObjects but I'm very low
understanding in it and I couldn't figure out what to do.)

Thanks
 
Here is a simple subroutine that will combine two text file (called
File1.txt and File2.txt) into one text file (called CombinedText.mdb). Of
course, you will want to change the paths to these files to your actual
paths.
'------------------------
Sub CombineFiles()
Dim i As Integer

Open "c:\The\Path\You\Want\File1.txt" For Input As #1
Open "c:\The\Path\You\Want\File2.txt" For Input As #2
Open "c:\The\Path\You\Want\CombinedFile.txt" For Output As #3

'loop through first file
Do While Not EOF(1)
Line Input #1, MyString
Print #3, MyString
Loop
'loop through second file
Do While Not EOF(2)
Line Input #2, MyString
Print #3, MyString
Loop

MsgBox "Done"

Close #3
Close #2
Close #1
End Sub
'------------------------
 
In good old DOS days you could do

Copy file1 + file2 file3

So I guess from within VBA you can do

dim strCmd as string
strCmd = "Copy file1 + file2 file3"

Shell strCmd

--
Regards,

Adrian Jansen
J & K MicroSystems
Microcomputer solutions for industrial control
 
Hi:

You could do it like this --

Dim fs As Scripting.FileSystemObject
Dim t As Scripting.TextStream
Dim str1 As String

Set fs = New Scripting.FileSystemObject
Set t = fs.OpenTextFile("C:\File1.txt", ForReading)
str1 = t.ReadAll
t.Close
Set t = Nothing

Set t = fs.OpenTextFile("C:\File2.txt", ForReading)
str1 = str1 & Chr(10) & Chr(13) & t.ReadAll
t.Close
Set t = Nothing

Set fs = Nothing

Hope it makes sense.

Regards,

Naresh Nichani
Microsoft Access MVP
 
Back
Top