Solved

splitting and merging a text file

Posted on 2004-08-21
4
141 Views
Last Modified: 2010-05-02
Thanks to JR2003 I have got a program that splits text files into splits of 1000. Now I need to take each of those splits and merge them together - so for example instead of the file being:

1
2
3
4
etc upto 1000

it will be

1
5001
2
5002
3
5003
etc upto
5000
10000


sometimes the file won't have 10000 rercords in becasue there may have been some records left over at the end of the splits, so the last file may only have a few hundred records in it.

below is the code JR2003 gave me for splitting the files, I would prefer to have the merging part seperate if possible. Can anyone get me started? don't really have much of an idea how to go about this.


Thanks
0
Comment
Question by:arcdogl
  • 2
  • 2
4 Comments
 

Author Comment

by:arcdogl
ID: 11858359
Option Explicit

Private Sub Command1_Click()

    Dim fso As Scripting.FileSystemObject
    Dim ReadStream As Scripting.TextStream
    Dim WriteStream As Scripting.TextStream
    Dim i As Long
    Dim iFileCount As Long
    Dim sLine As String
   
   
    Set fso = New Scripting.FileSystemObject
    Set ReadStream = fso.OpenTextFile("C:\MyBigTextFile.txt", ForReading)
    With ReadStream
        While Not .AtEndOfLine
            If i Mod 1000 = 0 Then
                iFileCount = iFileCount + 1
                If Not WriteStream Is Nothing Then
                    WriteStream.Close
                End If
                Set WriteStream = fso.OpenTextFile("C:\MyLittleTextFile" & iFileCount & ".txt", ForWriting, True)
            End If
            sLine = .ReadLine
            WriteStream.WriteLine sLine
            i = i + 1
        Wend
        WriteStream.Close
    End With
   

End Sub
0
 
LVL 18

Expert Comment

by:JR2003
ID: 11858406
Private Sub Command2_Click()

    Dim fso As Scripting.FileSystemObject
    Dim ReadStream As Scripting.TextStream
    Dim WriteStream As Scripting.TextStream
    Dim iFileCount As Long
   
    Set fso = New Scripting.FileSystemObject
    Set WriteStream = fso.OpenTextFile("C:\MyNewBigTextFile.txt", ForWriting, True)
   
    iFileCount = 1
    While fso.FileExists("C:\MyLittleTextFile" & iFileCount & ".txt")
        Set ReadStream = fso.OpenTextFile("C:\MyLittleTextFile" & iFileCount & ".txt", ForReading)
        WriteStream.WriteLine ReadStream.ReadAll
        ReadStream.Close
        iFileCount = iFileCount + 1
    Wend

End Sub
0
 

Author Comment

by:arcdogl
ID: 11858527
Hi JR2003,

I tried the above and it put all of the files that were split into one file with a new line between each of them. Sorry if this is what you thought I meant from my explanation - i wasn t very clear.

I need the records in the new files to be mixed up (sort of like shuffleing cards in half).

so using the new files with the 10000 records I need the file split in half and merged back together so that what ever was the record halfway is now the second record then what ever was the second record becomes the third record, and whatever was the second record after halfway becomes the fourth record. Sorry - i realsise this still isnt very clear. see below

original file of 10 (example)
1
2
3
4
5
6
7
8
9
10

new merged file
1
6
2
7
3
8
4
9
5
10
0
 
LVL 18

Accepted Solution

by:
JR2003 earned 125 total points
ID: 11859750
This will take a text file and shuffle the lines to the output file.
I think this is what you want

Private Sub Command3_Click()

    Dim fso As Scripting.FileSystemObject
    Dim ReadStream1 As Scripting.TextStream
    Dim ReadStream2 As Scripting.TextStream
    Dim WriteStream As Scripting.TextStream
    Dim iLineCount As Long
    Dim iMidPoint As Long
    Dim i As Long
    Dim sLine As String
    Set fso = New Scripting.FileSystemObject
    Set ReadStream1 = fso.OpenTextFile("C:\MyBigTextFile.txt", ForReading)
    'Count the lines in the input file
   
    iLineCount = 0
    With ReadStream1
        While Not .AtEndOfStream
            .ReadLine
            iLineCount = iLineCount + 1
        Wend
        .Close
    End With
   
    iMidPoint = iLineCount - (iLineCount \ 2) 'Get the middle line
    Set ReadStream1 = fso.OpenTextFile("C:\MyBigTextFile.txt", ForReading)
    Set ReadStream2 = fso.OpenTextFile("C:\MyBigTextFile.txt", ForReading)
    Set WriteStream = fso.OpenTextFile("C:\MyShuffledTextFile.txt", ForWriting, True)
    With ReadStream2
        For i = 1 To iMidPoint 'Get stream 2 to the mid point
            .ReadLine
        Next i
    End With
   
    iLineCount = 0
    With ReadStream1
        While iLineCount < iMidPoint
            sLine = .ReadLine
            WriteStream.WriteLine sLine
            If Not ReadStream2.AtEndOfStream Then
                sLine = ReadStream2.ReadLine
                WriteStream.WriteLine sLine
            End If
            iLineCount = iLineCount + 1
        Wend
    End With
    ReadStream1.Close
    ReadStream2.Close
    WriteStream.Close
End Sub
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb6 programatically bring window to front 8 72
passing a value with stream reader AFTER a ";" 3 67
Spell Check in VB6 13 112
VB 6 error 5 in windows 10 but not in XP 7 56
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question