Hi everyone,

I would like to merge few text documents into one,
For example, i have 12 text documents in a folder,
then needed to merge their text into one. 12 = 1.
any help will be very appreciate, thanks in advance.

if is not much to ask, if i can't do this
using Visual Basic 6, will be nice if some one can
tell me how to accomplish this using batch script.
thank you again.

Dani AI

Generated

solved the core problem nicely. Two handy alternatives to consider depending on constraints: a tiny Windows batch/for loop for quick one-off merges, and a native VB6 streaming routine that avoids loading whole files into memory and preserves original bytes.

Batch (quick, simple). Runs from the folder that contains the .txt files; make sure the output file is not in that folder (or delete it first) so it is not re-read:

cd "C:\path\to\files"
if exist merged.txt del merged.txt
for %f in (*.txt) do type "%f" >> merged.txt

(inside a .bat file use %%f instead of %f). Files are processed alphabetically; rename or enumerate explicitly to control order. copy /b or type work too but treat BOMs literally.

VB6 (streaming, safe for large files). This reads and writes in 8KB binary chunks so it does not blow memory and keeps original encoding bytes intact:

Private Sub MergeFilesVB6(folder As String, outputFile As String)
    Dim outNum As Integer, inNum As Integer
    Dim fileName As String
    Dim chunk() As Byte
    Dim fileLen As Long, pos As Long, toRead As Long

    outNum = FreeFile
    Open outputFile For Binary Access Write As #outNum

    fileName = Dir(folder & "\*.txt")
    Do While fileName <> ""
        inNum = FreeFile
        Open folder & "\" & fileName For Binary Access Read As #inNum
        fileLen = LOF(inNum)
        pos = 1
        Do While pos <= fileLen
            toRead = fileLen - pos + 1
            If toRead > 8192 Then toRead = 8192
            ReDim chunk(0 To toRead - 1)
            Get #inNum, pos, chunk
            Put #outNum, , chunk
            pos = pos + toRead
        Loop
        Close #inNum
        fileName = Dir()
    Loop

    Close #outNum
End Sub

Notes: concatenating UTF‑8 files that include a BOM will leave BOM bytes in the middle of the merged file; normalize encoding beforehand if that matters. For very small jobs the batch approach is easiest; for many or very large files prefer the VB6 streaming method. accepted the FSO route from , which is great for compact code, but the above gives alternatives when order, encoding or memory are concerns.

Recommended Answers

All 2 Replies

See if this help.
This following code will merge multiple text files into one file :
Add reference : Microsoft Scripting Runtime

Private Sub ConcatenateFiles(ByVal ResultFile As String, ByVal Separator As String, ParamArray SourceFiles() As Variant)
    Dim FSO As New FileSystemObject
    Dim fsSourceStream As TextStream
    Dim fsResStream As TextStream
    Dim sSeparator As String
    Dim i As Integer
    On Error Resume Next
    ' create a new file
    Set fsResStream = FSO.OpenTextFile(ResultFile, ForWriting, True)
    ' for each source file in the input array
    For i = 0 To UBound(SourceFiles)
        ' add the separator first (replacing the special tag for the file path)
        sSeparator = Replace(Separator, "#FilePath#", SourceFiles(i))
        fsResStream.Write sSeparator & vbCrLf
        ' open the file in read mode
        Set fsSourceStream = FSO.OpenTextFile(SourceFiles(i), ForReading)
        ' add its content + a blank line to the result file
        fsResStream.Write fsSourceStream.ReadAll & vbCrLf
        ' close this source file
        fsSourceStream.Close
    Next i
    fsResStream.Close
End Sub

Private Sub Command1_Click()
' Example:
 ConcatenateFiles "D:\res.txt", "------ NEW FILE: #FilePath# ------", "D:\1.txt", "D:\2.txt", "D:\3.txt"
End Sub
commented: This is an awosome code :) +3
commented: Absolutely Help +3

Thank You So Much JX :-) Just what i need it !!!!
What i nice little piece of code, thanks a lot :-)

Private Sub Command1_Click()
ConcatenateFiles "All-In-One.txt", "=======================================", "1.txt", "2.txt", "3.txt"
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.