combine files

S

Scott

I have a group of .csv files with identical fields. I want to combine them
into a single file. How to do that? Appending?
 
J

Jonathan West

Scott said:
I have a group of .csv files with identical fields. I want to combine them
into a single file. How to do that? Appending?

Appending should be fine, but be careful to check whether there is a valid
CR/LF at the end of each file, and insert one if there isn't, otherwise the
append may be something of a mess.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
S

Scott

Thanks.
Could you be more specific?

Jonathan West said:
Appending should be fine, but be careful to check whether there is a valid
CR/LF at the end of each file, and insert one if there isn't, otherwise the
append may be something of a mess.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
L

Luke Alcatel

Scott,
I have no doubt that there are more elegant ways to concatenate files in VBA
than the solution I offer. I am only a very occasional VBA programmer so I
tend to use constructs and idioms closer to what I would do in "C" as
opposed to what an accomplished VBA programmer might do. This subroutine is
agnostic to the fact that your files are CSV text - it just copies and
concatenates byte for byte. The code was taken from a VBA app I did last
month. One thing you may want to do is to delete the destination file first
if it already exists.
Luke

Rem Concatenate files named in "sources" array to file named by "output"
Private Sub catFiles(ByRef sources As Variant, ByVal output As String)
Dim fds As Variant, fdo As Variant, data As Byte, ndx As Integer

fdo = FreeFile(0)
Open output For Binary Access Write As fdo Len = 1024
For ndx = 0 To UBound(sources)
fds = FreeFile(0)
Open sources(ndx) For Binary Access Read As fds Len = 1024
Get #fds, , data
While Not EOF(fds)
Put #fdo, , data
Get #fds, , data
Wend
Close #fds
Next ndx
Close #fdo
End Sub
 
S

Steve Rindsberg

Thanks.
Could you be more specific?


If there isn't a CR/LF pair at the end of the first file, then after appending
your list of MS Biggies to your list of nursery rhyme characters, you'd have:

"Peter","Piper"
"Jack","Horner""Bill","Gates"
"Steve","Ballmer"

where what you want is this:

"Peter","Piper"
"Jack","Horner"
"Bill","Gates"
"Steve","Ballmer"
 
L

Luke Alcatel

I'm pre-emptively posting a 2nd solution that deals with the possible issue
mentioned by Jonathan. I didn't have this issue so it's not accounted for
in my original code. I suspect that since there isn't even a standard
definition of what a .txt file is in M$ this solution may not work in every
case but it worked with files created via NotePad.
Luke

Rem Concatenate files named in "sources" array to file named by "output"
Private Sub catFiles(ByRef sources As Variant, ByVal output As String)
Dim fds As Variant, fdo As Variant, data As Byte, ndx As Integer
Dim cr As Variant, lf As Variant

fdo = FreeFile(0)
Open output For Binary Access Write As fdo Len = 1024
For ndx = 0 To UBound(sources)
fds = FreeFile(0)
Open sources(ndx) For Binary Access Read As fds Len = 1024
Get #fds, , data
While Not EOF(fds)
Put #fdo, , data
cr = lf
lf = data
Get #fds, , data
Wend
If cr <> 13 Or lf <> 10 Then Put #fdo, , vbCrLf
Close #fds
Next ndx
Close #fdo
End Sub
 

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

Top