Consulting

Results 1 to 7 of 7

Thread: Merge multiple .txt files into one .txt thru Excel VBA

  1. #1
    VBAX Newbie
    Joined
    Jun 2012
    Posts
    2
    Location

    Merge multiple .txt files into one .txt thru Excel VBA

    Hello,
    I would like to merge all the .txt files in filepath C:\TEST into one .TXT file which saves into the same filepath. I'd like to do this thru XL VBA but have the files remain .TXT and append as well.

    Any help?
    Thanks

  2. #2
    You could use something like this:
    Sub CombineTextFiles()
        Dim lFile As Long
        Dim sFile As String
        Dim vNewFile As Variant
        Dim sPath As String
        Dim sTxt As String
        Dim sLine As String
        With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        If .Show Then
            sPath = .SelectedItems(1)
            If Right(sPath, 1) <> Application.PathSeparator Then
                sPath = sPath & Application.PathSeparator
            End If
        Else
            'Path cancelled, exit
            Exit Sub
        End If
        End With
        vNewFile = Application.GetSaveAsFilename("CombinedFile.txt", "Text files (*.txt), *.txt", , "Please enter the combined filename.")
        If TypeName(vNewFile) = "Boolean" Then Exit Sub
        sFile = Dir(sPath & "*.txt")
        Do While Len(sFile) > 0
        lFile = FreeFile
        Open CStr(sFile) For Input As #lFile
        Do Until EOF(lFile)
            Line Input #1, sLine
            sTxt = sTxt & vbNewLine & sLine
        Loop
        Close lFile
        sFile = Dir()
        Loop
        lFile = FreeFile
        Open CStr(vNewFile) For Output As #lFile
        Print #lFile, sTxt
        Close lFile
    End Sub
    Last edited by Aussiebear; 04-25-2023 at 07:20 PM. Reason: Adjusted the code tags
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    or

    Sub snb()
    c00 = "C:\Test\" ' the path
    c01 = Dir(c00 & "*.txt")
    Do Until c01 = ""
        c02 = c02 & vbCrLf & CreateObject("scripting.filesystemobject").opentextfile(c00 & c01).readall
        c01 = Dir
    Loop
    CreateObject("scripting.filesystemobject").createtextfile(c00 & "_new.txt").write c02
    End Sub
    Last edited by Aussiebear; 04-25-2023 at 07:21 PM. Reason: Adjusted the code tags

  4. #4
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Also here.

    David


  5. #5
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    You can employ a DOS command through VBA as well.
    Public Sub CombineFiles()
    Shell Environ$("COMSPEC") & " /c Copy C:\TEST\*.txt C:\TEST\CombinedFile.txt "
    End Sub
    Last edited by Aussiebear; 04-25-2023 at 07:21 PM. Reason: Adjusted the code tags
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  6. #6
    VBAX Newbie
    Joined
    Apr 2018
    Posts
    1
    Location
    Clarification about calling DOS, since some folks have pointed out on other forums that there are problems if path names contain spaces.

    Create the string as if it was being entered on the command line, including necessary double quotes. Then double each double quote. Then put one additional double quote at the beginning and one additional double quote at the end.

    The command below basically says "combine all text files matching the pattern into cmnt-all.txt". Because the code line could get extremely long and confusing I find it easier to put the source and target paths in variables.

    Note that the target string begins with a space, followed by the path's starting quote.

    Incidentally, this was tested using TCC.exe from JPSoft, not cmd.exe, so the command syntax might be slightly different.

    In case you're not familiar with TCC.exe, it's basically "CMD.EXE on steroids". A much older version was included in Norton Utilities under the name NDOS. If Microsoft had continued actively developing CMD.EXE for 35+ years, TCC and TCMD (full IDE, etc.) are what it would look like.


    Sub Merge_TXE_comments()
        Dim src_pat As String
        Dim target As String
    src_pattern = """F:\CLIENT STUFF\TXE\a-Temp\cmnt-*.csv"""
        target = " ""F:\CLIENT STUFF\TXE\a-Temp\cmnt-all.txt"""
    Call Shell(Environ("COMSPEC") & " /c copy " & src_pattern & target, vbMinimizedNoFocus)
    End Sub
    Last edited by Aussiebear; 04-25-2023 at 07:22 PM. Reason: Adjusted the code tags

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Thank you, Rick.

    But, now, I'm, closing this 5yo thread.

    Any other question about this will have to be in a new thread.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •