Splitting a file....

Posted on 2004-11-16
Last Modified: 2011-10-03
hi.. all

Can anyone tell me how to split a file....... and join it....
Can anyone give me some sample code for tht...

Question by:no1nincompoop
    LVL 2

    Expert Comment

    what do you mean splitting a file?
    do you want to parse the filename?
    or do you want to take a certain section of a file's contents and merge it with another?
    LVL 4

    Accepted Solution

    You need two command buttons cmdSplit and cmdJoin and a textbox txtPath

    When u click split it reads the txtpath and splits the file into parts each of size as specified in partsize. the parts are named filename.1 filename.2 etc. and saved in c:\ in this case.

    When u click join you have the specify the original name of the file, eg filename.exe (without the folder path) and the number of parts that it has been split into. the parts are read from c:\ in this case and they are joined to form the original file.

    Private Sub cmdJoin_Click()
    Dim i As Integer
    Dim Parts As Integer
    Dim FName As String
    Dim Buffer As String
    Dim Pos As Long

    Pos = 1

    FName = InputBox("Enter original name of file")
    Parts = CInt(InputBox("Enter no of source parts"))

    Open "c:\" & FName For Binary Access Write As #1

    For i = 1 To Parts
        Open "c:\" & FName & "." & i For Binary Access Read As #2
        Buffer = Space$(LOF(2))
        Get #2, , Buffer
        Put #1, Pos, Buffer
        Pos = Pos + LOF(2)
        Close #2
        Kill "c:\" & FName & "." & i
    Next i

    Close #1

    End Sub

    Private Sub cmdSplit_Click()

    Dim i As Long
    Dim c As Integer
    Dim FLen As Long
    Dim Rep As Long
    Dim Remain As Long
    Dim FName As String 'name of the file
    Dim PartSize As Long
    Dim Buffer As String 'temporary buffer

    PartSize = 1000 ' size of each portion of the split file
    FLen = FileLen(txtPath.Text) 'size of the file in bytes
    Rep = Int(FLen / PartSize) ' no of portions the file is split into
    Remain = FLen Mod PartSize ' last few bytes remaining not divisible by 1000
    FName = Right(txtPath.Text, Len(txtPath.Text) - InStrRev(txtPath.Text, "\"))
    Debug.Print FName

    c = 1
    i = 1
    Open txtPath.Text For Binary Access Read As #1

    While c <= Rep
        Buffer = Space$(PartSize)
        Get #1, i, Buffer ' read part of the source file into the temporary buffer

        Open "c:\" & FName & "." & CStr(c) For Binary Access Write As #2
            Put #2, , Buffer
        Close #2
        c = c + 1
        i = i + PartSize

    If Remain > 0 Then
        Buffer = Space$(Remain)
        Get #1, i, Buffer
        Open "c:\" & FName & "." & CStr(c) For Binary Access Write As #2
            Put #2, , Buffer
        Close #2

    End If

    Close #1

    End Sub

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    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 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…

    755 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

    16 Experts available now in Live!

    Get 1:1 Help Now