Results 1 to 5 of 5

Thread: Folder full of .txt's to 1 .txt

  1. #1

    Thread Starter
    Lively Member THCfog's Avatar
    Join Date
    Oct 2002
    Location
    Michigan
    Posts
    86

    Folder full of .txt's to 1 .txt

    I need to be able to join all text files within a folder, to 1 "Master.txt". Anyone here have a soultion for me?

    There will be between 20 and 100 text files, most with different titles.


    Thanks ahead of time

  2. #2
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Something like this, but you may to tune it up:
    VB Code:
    1. Public Sub CombineFiles(strPath As String, strNewFile As String)
    2. '================================================================
    3. ' to simplify processing the following must be in order:
    4. ' - strNewFile must havefull file name including extension
    5. ' - strPath must represent full path to your folder
    6. '================================================================
    7. Dim strFile As String
    8. Dim strLine As String
    9. Dim strExtension As String
    10. Dim FileNumber
    11.  
    12. On Error GoTo ErrClear
    13.  
    14.     If Not Right(strPath, 1) = "\" Then strPath = strPath & "\"
    15.     strExtension = LCase(Right(strNewFile, 3))
    16.     Open strPath & strNewFile For Output As #1
    17.         strFile = Dir(strPath, vbNormal)
    18.         Do While strFile <> ""
    19.             If strFile <> "." And strFile <> ".." Then
    20.                 If (GetAttr(strPath & strFile) And vbNormal) = vbNormal Then
    21.                     If Not strFile = strNewFile And LCase(Right(strFile, 3)) = strExtension Then
    22.                         FileNumber = FreeFile
    23.                         Open strPath & strFile For Input As #FileNumber
    24.                             Do
    25.                                 Line Input #FileNumber, strLine
    26.                                 Print #1, strLine
    27.                             Loop Until EOF(FileNumber)
    28.                         Close #FileNumber
    29.                     End If
    30.                 End If
    31.             End If
    32.             strFile = Dir
    33.         Loop
    34.     Close #1
    35.     Exit Sub
    36.    
    37. ErrClear:
    38. '-----------
    39.     Err.Clear
    40.     Resume Next
    41.  
    42. End Sub
    Roy

  3. #3
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Usage:
    VB Code:
    1. Private Sub Command1_Click()
    2.     CombineFiles "c:\temp", "bigfile.txt"
    3. End Sub
    Roy

  4. #4
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    You may use MS Word application to do that.

    VB Code:
    1. Dim wa As Object
    2. Set wa = CreateObject("Word.Application")
    3.    
    4.     With wa
    5.         .Documents.Open "C:\t1.txt"
    6.         .ActiveDocument.Select
    7.         .Selection.EndOf
    8.        
    9.         For j = 2 To 4
    10.             .Selection.InsertFile "C:\t" & j & ".txt", , False, , False
    11.             .Selection.EndOf
    12.         Next j
    13.         .Quit wdSaveChanges
    14.     End With

  5. #5
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    As far as memory concern, it will be an overkill using Word, but it will get job done.
    Roy

Posting Permissions

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



Click Here to Expand Forum to Full Width