Solved

Concatenate Files

Posted on 2000-05-11
6
1,074 Views
Last Modified: 2011-04-14
I would like to concatenate two files like in the DOS command:

copy a.txt+b.txt,c.txt

How can this be done in VB without parsing the files?

Best regards,
Sorin
0
Comment
Question by:axasb
6 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2801412
Private Sub JoinFile(File1, File2, NFile)
    Dim Buffer() As Byte
    Open File1 For Binary Access Read As #1
    Open File2 For Binary Access Read As #2
    Open NFile For Binary Access Write As #3
    ReDim Buffer(1 To LOF(1))
    Get #1, , Buffer
    Put #3, , Buffer
    ReDim Buffer(1 To LOF(2))
    Get #2, , Buffer
    Put #3, , Buffer
    Close #1, #2, #3
End Sub

Private Sub Command1_Click()
    Call JoinFile("c:\files\1.txt", "c:\files\2.txt", _
        "c:\files\3.txt")
End Sub
0
 

Author Comment

by:axasb
ID: 2801442
Thank you AzraSound.
You are very quick.
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 50 total points
ID: 2801479
- submitting answer -

Youre welcome  =)
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 12

Expert Comment

by:mark2150
ID: 2801529
you could also do it the DOS way with SHELL...

M
0
 
LVL 27

Expert Comment

by:Ark
ID: 2802592
Hi
To join ANY number of files (like in DOS):
Private Function ConCatFiles(sDest As String, ParamArray _
   sInputFiles()) As Boolean
   

  Dim bTemp() As Byte, nDestFile As Integer
  Dim nSrcFile As Integer, i As Integer
  On Error GoTo ErrHandler
  ReDim sTemp(UBound(sInputFiles()))
  nDestFile = FreeFile
  Open sDest For Binary Access Write As nDestFile
  For i = 0 To UBound(sInputFiles)
      nSrcFile = FreeFile
      Open sInputFiles(i) For Binary Access Read As nSrcFile
      ReDim bTemp(LOF(nSrcFile) - 1)
      Get nSrcFile, , bTemp
      Put nDestFile, , bTemp
      Close nSrcFile
  Next i
  Close nDestFile
  On Error GoTo 0
  ConCatFiles = True
 
  Exit Function
ErrHandler:
  'OPTIONAL, OR CALLER CAN JUST CHECK RETURN VALUE
  'MsgBox "Unable to concatenate files.", vbCritical
  On Error GoTo 0
  Close
End Function

'Using
'Private Sub Command1_Click()
'ConCatFiles "c:\temp.txt", "c:\autoexec.bat", "c:\config.sys", "c:\msdos.sys"
'End Sub
'Result - output file "c:\temp.txt" is a sum of all three source files

Cheers
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2804138
Shell "C:\command.com /b copy a.txt+b.txt,c.txt"

M
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now