Results 1 to 7 of 7

Thread: How do you join two text files together ?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    UK
    Posts
    300
    could anyone tell me how to join two text files in to one file.

    My files are c:\test\testfile1.txt
    and c:\test\testfile2.txt

    I want one file at the end with the data from both files in called c:\test\testfile1.txt
    (basically I am trying to append the data from testfile2 to testfile1. How do I go about this ? code would be great with a short explaination.

    I use VB5 so don't have the filesystem object

    Thanks
    Locutus

    [Edited by locutus on 07-27-2000 at 09:38 AM]
    Resistance is futile

  2. #2
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833
    dim B() as byte
    dim c() as byte

    open file1 for input as #1
    b() = input(lof(1),1)
    close #1

    open file2 for input as #1
    b() = input(lof(1),1)
    close #1

    open file3 for output as #1
    print #1, b()
    print #1, c()
    close #1





    Kurt Simons
    [I know I'm a hack but my clients don't!]

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>same dog different sled

    Code:
    Private Sub Command1_Click()
      Dim myVar1 As String, myVar2 As String, myVar3 As String
    
      Open "c:\my documents\text1.Txt" For Input As #1
      myVar1 = Input(LOF(1), 1)
      Close #1
    
      Open "c:\my documents\text2.Txt" For Input As #1
      myVar2 = Input(LOF(1), 1)
      Close #1
    
      myVar3 = myVar1 & vbCrLf & myVar2
    
      Open "c:\my documents\text1.Txt" For Output As #1
      Print #1, myVar3
      Close
      
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184
    Same thing as above, but mabye a little clearer (as requested in your Question)

    Code:
     Dim iFile1 As Integer
        Dim iFile2 As Integer
        Dim iFile3 As Integer
        Dim strFileLine As String
        Dim PathToFile1 As String
        Dim PathToFile2 As String
        Dim PathToFile3 As String
        
        PathToFile1 = "c:\WINDOWS\Desktop\Testfile1.txt"
        PathToFile2 = "c:\WINDOWS\Desktop\Testfile2.txt"
        PathToFile3 = "c:\WINDOWS\Desktop\Testfile3.txt"
        
        iFile1 = FreeFile   'Get unused file number.
        Open PathToFile1 For Input As #iFile1   'Create file name.
        
        iFile2 = FreeFile
        Open PathToFile2 For Input As #iFile2
        
        iFile3 = FreeFile   'Note if this file exists it will be wiped clean (made & destroyed).
        Open PathToFile3 For Output As #iFile3   'Create file name.
        
        Do While Not EOF(iFile1)
          Line Input #iFile1, strFileLine
          Print #iFile3, strFileLine
        Loop
        
        Do While Not EOF(iFile2)
          Line Input #iFile2, strFileLine
          Print #iFile3, strFileLine
        Loop
        
        Close #iFile1
        Close #iFile2
        Close #iFile3

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    UK
    Posts
    300

    Thanks guys that worked , but !!!!!!!

    That worked a treat , thanks guys. Just one small detail I need to sort. When I copy contents of the second file I would like to lose the header row.so its one big block of data in one file.

    ie:

    "Name", "address", "age"
    "Joe","10 the road","24"
    "Joe","20 the road","27"
    "Joe","55 the road","67"

    "Name", "address", "age"
    "mary","10 the road","24"
    "bill","20 the road","27"
    "ian","55 the road","67"

    So to summarise when I read in the second file, I want to ignore the first row ( which is headings)

    Please help again guys !

    Locutus
    Resistance is futile

  6. #6
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184
    Just skip the lines before entering the loop:

    Code:
     Line Input #iFile1, strFileLine
        Do While Not EOF(iFile1)
          Line Input #iFile1, strFileLine
          Print #iFile3, strFileLine
        Loop
        
        Line Input #iFile2, strFileLine
        Do While Not EOF(iFile2)
          Line Input #iFile2, strFileLine
          Print #iFile3, strFileLine
        Loop

  7. #7
    Member
    Join Date
    Jul 2000
    Location
    Eastern Universe
    Posts
    32

    Wink Re: Thanks guys that worked , but !!!!!!!

    if u used Do while Not Eof(#)

    have a Line Input #?, xVAR command before the loop statement
    -----------------------------------------

    oooO Knock! Knock! Ooooo

    -----------------------------------------

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