Solved

splitting and merging a text file

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB error "Type mismatch" 2 51
A macro to Count the number of rows across all worksheets 3 84
Updates not working for MS Windows 7 12 154
Spell Check in VB6 13 99
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 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

19 Experts available now in Live!

Get 1:1 Help Now