Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleSplit a long text file
Description
Keywordsfile split, long file
CategoriesFiles and Directories, Utilities
 
Search for vbCrLf near where we want the split to occur. Write the file into smaller files in pieces.
 
Private Sub cmdSplit_Click()
Dim base_name As String
Dim max_file_size As Long
Dim fnum As Integer
Dim txt As String
Dim new_txt As String
Dim file_num As Integer
Dim txt_len As Long
Dim pos As Long

    MousePointer = vbHourglass
    
    max_file_size = CLng(txtMaxSize.Text)
    
    ' Read the input file.
    fnum = FreeFile
    Open txtInputFileName.Text For Input As fnum
    txt = Input$(LOF(fnum), fnum)
    Close fnum
    
    ' Start breaking up the file.
    base_name = txtBaseName.Text
    file_num = 0
    txt_len = Len(txt)
    Do While txt_len > 0
        If txt_len <= max_file_size Then
            new_txt = txt
            txt = ""
            txt_len = 0
        Else
            ' Find the next earlier carriage return.
            For pos = max_file_size To 1 Step -1
                If Mid$(txt, pos, 2) = vbCrLf Then Exit For
            Next pos
            If pos < 1 Then pos = max_file_size
            new_txt = Left$(txt, pos + 1)
            txt_len = txt_len - pos - 1
            txt = Right(txt, txt_len)
        End If
        
        ' Create the next file.
        file_num = file_num + 1
        Open base_name & Format$(file_num) & _
            ".txt" For Output As fnum
        Print #fnum, new_txt
        Close fnum
    Loop

    MousePointer = vbDefault
    MsgBox "Created" & Str$(file_num) & " files."
End Sub
 
For non-text files, you'll need to break the file at some character other than vbCrLf.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated