Concatenate Text File

  • Thread starter Thread starter RickKennedy
  • Start date Start date
R

RickKennedy

Are you saying there is not a command to contatenate two text files i
VBA
 
No, there's no single command to do that. However, if the 2nd file is less
than the maximum allowed for a single string (2^31 bytes), you can do this:

File1 = "FirstFile.Txt"
File2 = "SecondFile.Txt"

Open File1 For Binary As #1
Seek #1, LOF(1) + 1
Open File2 For Binary AS #2
X = Space$(LOF(2))
Get #2, 1, X
Put #1, , X
Close #1, #2

If it's binary data and too big to read into a single string, you would have
to read in blocks, most probably adjusting the size of the the string variable
when reading the last block. IOW, you would have to keep track of the number
of bytes left, and if it's less than the length of X, redefine X to the number
of bytes left before the last Get statement.

If the 2nd file is too large, and it consists of lines ending with CR/LF, you
can do it the old fashioned way:

File1 = "FirstFile.Txt"
File2 = "SecondFile.Txt"
Open File1 For Append As #1
Open File2 For Input AS #2
Do While Not EOF(2)
Line Input #2, X
Print #1, X
Loop
Close #1, #2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top