Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

merge .txt in one named big.txt 1

sal21

Programmer
Apr 26, 2004
428
IT
I c:\mydir\ have 35 files .txt, how to merge all .txt files in one named bigfile.txt

Note
in mydir i can have also .pdf, .csv ecc i need olnly for .txt
 
Use adir() to get an array of text files
then filetostr() to read each one and add it to a string
then strtofile() to write it back out as one file
 
VB6 (for that is what this forum is about) does not have adir, filetostr or strtofile functions
 
I might be tempted to just use robocopy for this rather than write any VB code.
 
if we are wedded to a VB solution, here's an example that uses the filesystemobject:

Rich (BB code):
' Requires reference to Microsoft Scripting Runtime
Public Sub CombineTextFilesFSO()
    Dim fso As FileSystemObject
    Dim srcFile As File
    Dim dstFile As TextStream
    Dim sourceFolder As String
    Dim destinationFile As String

    ' Define the source folder and the destination file
    sourceFolder = "D:\Downloads\DeleteMe\Source\"  ' Change to your folder
    destinationFile = "D:\Downloads\DeleteMe\Source\Combined.txt"  ' Change to your output file

    With New FileSystemObject
    Set dstFile = .OpenTextFile(destinationFile, ForAppending, True)
        For Each srcFile In .GetFolder(sourceFolder).Files
            If .GetExtensionName(srcFile) = "txt" And srcFile.Path <> destinationFile Then
                dstFile.Write srcFile.OpenAsTextStream.ReadAll
            End If
        Next
    End With
End Sub
 
It's off-topic - sorry - but I try to use PowerShell instead of VBS/VB these days. Its easy to filter by filetype and use Get-Content with its Add-Content operator for appending. For example, I used the following to merge several dozen .TXT files in a folder of mixed content recently:

Rich (BB code):
# Merge all .TXT files in a directory into one file
# Define the directory to search for TXT files
$directory = "C:\Mixed content"

# Define the output file
$outputFile = Join-Path -Path $directory -ChildPath "combined.txt"

# Get all TXT files in the directory (excluding subdirectories)
$txtFiles = Get-ChildItem -Path $directory -Filter *.txt -File

# Initialize the output file by removing any existing file with the same name
if (Test-Path -Path $outputFile) {
    Remove-Item -Path $outputFile
}

foreach ($file in $txtFiles) {
    # Append the content of each file to the combined.txt
    Get-Content -Path $file.FullName | Add-Content -Path $outputFile
}

I just like KISS. :)
 
I prefer binary access.

Code:
Private Sub MergeIt(ByVal sPath As String)
Dim sFile As String
Dim bText() As Byte
    sFile = Dir(sPath & "*.txt")
    Open "textFile.txt" For Binary Access Write As #1
    Do While Len(sFile) > 0         '
        bText = sLoadFile(sPath & sFile)    'encoding is not matter
        Put #1, , bText
        sFile = Dir                 'find next
        DoEvents
    Loop
    Close #1
End Sub

Private Function sLoadFile(sFile As String) As String
Dim iFree As Integer
Dim bRead() As Byte
    iFree = FreeFile()
    Open sFile For Binary As iFree
        ReDim bRead(LOF(iFree))
        Get iFree, , bRead
    Close iFree
    sLoadFile = bRead
End Function
 

Part and Inventory Search

Sponsor

Back
Top