Solved

Concatenate Files

Posted on 2000-05-11
6
1,075 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Expert Comment

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

M
0
 
LVL 28

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 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