Solved

splitting and merging a text file

Posted on 2004-08-21
4
139 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now