Important alert: (current site time 7/16/2013 4:11:33 AM EDT)
 

VB icon

Split any file into smaller files

Email
Submitted on: 4/26/1999
By: Riaan Aspeling  
Level: Not Given
User Rating: By 101 Users
Compatibility: VB 5.0, VB 6.0
Views: 33847
 
     This code will read any large file and split it into smaller chuncks so you can copy to stiffy,e-mail or ftp it. This code is for you out there playing with file management etc. This code is very basic but it does some cool things. It will leave the source file and will create a bunch of smaller files in the same directory.. This code can be modified to output directly to the stiffy drive if you want.
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Split any file into smaller files
'**************************************
'*************************************
'*** PASTE THIS CODE INTO A MODULE ***
'*************************************
Option Explicit
Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Type OPENFILENAME
 lStructSize As Long
 hwndOwner As Long
 hInstance As Long
 lpstrFilter As String
 lpstrCustomFilter As String
 nMaxCustFilter As Long
 nFilterIndex As Long
 lpstrFile As String
 nMaxFile As Long
 lpstrFileTitle As String
 nMaxFileTitle As Long
 lpstrInitialDir As String
 lpstrTitle As String
 flags As Long
 nFileOffset As Integer
 nFileExtension As Integer
 lpstrDefExt As String
 lCustData As Long
 lpfnHook As Long
 lpTemplateName As String
End Type
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Public Type SHITEMID
 mkidcb As Long
 abID As Byte
End Type
Public Type ITEMIDLIST
 idlmkid As SHITEMID
End Type
Public Type BROWSEINFO
 hOwner As Long
 pidlRoot As Long
 pszDisplayName As String
 lpszTitle As String
 ulFlags As Long
 lpfn As Long
 lParam As Long
 iImage As Long
End Type
Public Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Public Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Public Const BIF_RETURNONLYFSDIRS = &H1
Function GetOpenFileNameDLG(Filter As String, Title As String, DefaultExt As String, WindowHnd As Long) As String
On Error GoTo handelopenfile
 Dim OpenFile As OPENFILENAME, Tempstr As String
 Dim Success As Long, FileTitleLength%
 Filter = Find_And_Replace(Filter, "|", Chr(0))
 If Right$(Filter, 1) <> Chr(0) Then Filter = Filter & Chr(0)
 
 OpenFile.lStructSize = Len(OpenFile)
 OpenFile.hwndOwner = WindowHnd
 OpenFile.hInstance = App.hInstance
 OpenFile.lpstrFilter = Filter
 OpenFile.nFilterIndex = 1
 OpenFile.lpstrFile = String(257, 0)
 OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
 OpenFile.lpstrFileTitle = OpenFile.lpstrFile
 OpenFile.nMaxFileTitle = OpenFile.nMaxFile
 OpenFile.lpstrTitle = Title
 OpenFile.lpstrDefExt = DefaultExt
 OpenFile.flags = 0
 Success = GetOpenFileName(OpenFile)
 If Success = 0 Then
 GetOpenFileNameDLG = ""
 Else
 Tempstr = OpenFile.lpstrFile
 GetOpenFileNameDLG = Trim(Tempstr)
 End If
 Exit Function
handelopenfile:
 MsgBox Err.Description, 16, "Error " & Err.Number
 Exit Function
End Function
Function Find_And_Replace(ByRef TextLine As String, ByRef SourceStr As String, ByRef ReplaceStr As String) As String
On Error GoTo handelfindandreplace
 Dim DoAnother As Boolean, PosFound As Integer, ReturnStr As String
 DoAnother = True
 ReturnStr = TextLine
 While DoAnother
 PosFound = InStr(1, ReturnStr, SourceStr)
 If PosFound > 0 Then
ReturnStr = Mid$(ReturnStr, 1, PosFound - 1) & ReplaceStr & Mid$(ReturnStr, PosFound + Len(SourceStr))
Else
DoAnother = False
 End If
 Wend
 Find_And_Replace = ReturnStr
handelfindandreplace:
 Exit Function
End Function
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Split any file into smaller files
' Description:This code will read any large file and split it into smaller chuncks so you can copy to stiffy,e-mail or ftp it. This code is for you out there playing with file management etc. This code is very basic but it does some cool things. It will leave the source file and will create a bunch of smaller files in the same directory.. This code can be modified to output directly to the stiffy drive if you want.
' By: Riaan Aspeling
'
' Inputs:Create a new form and drop four Command Buttons on it (Command1 to Command4). Also drop a Textbox on it (Text1) and a Combobox (Combo1). You can (if you want) place a label above the textbox and change it's caption to "Source File" and a label above the combobox and change it's caption to "Split File size".
Now copy the source into the form and the module. Run and have fun ;).
If you make a nice util with the code please send me a copy : riaana@hotmail.com
'
' Returns:Split files with extensions from Myfile.000 to MyFile.999
'
' Assumes:If checked the split files after I've Assembled them again with FC (FileCompare) in binary mode and it didn't find any differences. But you should know that you are playing with files so don't delete the origanal after you've checked that you can re-assemble it ok.
'
' Side Effects:None that I know of... This code can be a basis for a cool util (You have to e-mail me that cool util .. riaana@hotmail.com)
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1662&lngWId=1'for details.'**************************************

'***********************************
'*** PASTE THIS CODE INTO A FORM ***
'***********************************
Option Explicit
Private Sub Command1_Click()
 Dim Ans As String
 Ans = GetOpenFileNameDLG("File to split *.*|*.*|File to combine *.000|*.000|", "Please select a file", "", Me.hwnd)
 If Ans <> "" Then
 Text1.Text = Ans
 End If
End Sub
Private Sub Command2_Click()
 
 'Check that somting is selected
 If Not CheckForFile Then Exit Sub
 
 'Ok split the file in the current directory
 
 If SplitFile(Text1.Text, Combo1.ItemData(Combo1.ListIndex)) Then
 MsgBox "File was split!"
 Else
 MsgBox "Error splitting file..."
 End If
 
 
End Sub
Private Sub Command3_Click()
 'Check that somting is selected
 If Not CheckForFile Then Exit Sub
 'Check to see if it is a Split file with extension "MYFILE.SP(x)"
 
 If (Right$(Text1.Text, 3)) <> "000" Then
 MsgBox "That's not the proper extension for a split file. It should be somthing like Myfile.000, the first file of the split files.", 16, "No go !"
 Exit Sub
 End If
 
 'Ok assemble the files in the current directory
 
 If AssembleFile(Text1.Text) Then
 MsgBox "File assembled!"
 Else
 MsgBox "Error assembeling file..."
 End If
End Sub
Private Sub Command4_Click()
 Unload Me
 End
End Sub
Private Sub Form_Load()
 Text1.Text = ""
 Combo1.AddItem "16 Kb"
 Combo1.ItemData(Combo1.NewIndex) = 16
 Combo1.AddItem "32 Kb"
 Combo1.ItemData(Combo1.NewIndex) = 32
 Combo1.AddItem "64 Kb"
 Combo1.ItemData(Combo1.NewIndex) = 64
 Combo1.AddItem "128 Kb"
 Combo1.ItemData(Combo1.NewIndex) = 128
 Combo1.AddItem "256 Kb"
 Combo1.ItemData(Combo1.NewIndex) = 256
 Combo1.AddItem "512 Kb"
 Combo1.ItemData(Combo1.NewIndex) = 512
 Combo1.AddItem "720 Kb"
 Combo1.ItemData(Combo1.NewIndex) = 720
 Combo1.AddItem "1200 Kb"
 Combo1.ItemData(Combo1.NewIndex) = 1200
 Combo1.AddItem "1440 Kb"
 Combo1.ItemData(Combo1.NewIndex) = 1440
 Combo1.ListIndex = Combo1.ListCount - 1
 Command1.Caption = "Browse"
 Command2.Caption = "Split File"
 Command3.Caption = "Assemble Files"
 Command4.Caption = "Cancel"
End Sub
Function CheckForFile() As Boolean
 'We don't want nasty spaces in the end
 Text1.Text = Trim(Text1.Text)
 CheckForFile = False
 'Check for text in textbox
 If Text1.Text = "" Then
 'Stop !! no text entered
 MsgBox "Please select a file first!", 16, "No file selected"
 Exit Function
 End If
 'Check if the file excists
 If Dir$(Text1.Text, vbNormal) = "" Then
 'Stop !! no file
 MsgBox "The file '" & Text1.Text & "' was not found!", 16, "File non excistend?!"
 Exit Function
 End If
 CheckForFile = True
End Function
Function SplitFile(Filename As String, Filesize As Long) As Boolean
On Error GoTo handelsplit
 
 Dim lSizeOfFile As Long, iCountFiles As Integer
 Dim iNumberOfFiles As Integer, lSizeOfCurrentFile As Long
 Dim sBuffer As String '10Kb buffer
 Dim sRemainBuffer As String, lEndPart As Long
 Dim lSizeToSplit As Long, sHeader As String * 16
 Dim iFileCounter As Integer, sNewFilename As String
 Dim lWhereInFileCounter As Long
 
 If MsgBox("Continue to split file?", 4 + 32 + 256, "Split?") = vbNo Then
 SplitFile = False
 Exit Function
 End If
 
 Open Filename For Binary As #1
 lSizeOfFile = LOF(1)
 lSizeToSplit = Filesize * 1024
 
 'Check if the file is actualy larger than the selected split size
 If lSizeOfFile <= lSizeToSplit Then
 Close #1
 SplitFile = False
 MsgBox "This file is smaller than the selected split size! Why split it ?", 16, "Duh!"
 Exit Function
 End If
 
 'Check if file isn't alread split
 sHeader = Input(16, #1)
 Close #1
 If Mid$(sHeader, 1, 7) = "SPLITIT" Then
 MsgBox "This file is alread split!"
 SplitFile = False
 Exit Function
 End If
 
 Open Filename For Binary As #1
 lSizeOfFile = LOF(1)
 lSizeToSplit = Filesize * 1024
 
 'Write the header of the split file
 ' Signature = "SPLITIT" = Size 7
 ' Split Number= "xxx" = Size 3
 ' Total Number of Split Files = "xxx" = Size 3
 ' Origanal file extension = "aaa" = Size 3
 'Total of 16 for header
 
 iCountFiles = 0
 iNumberOfFiles = (lSizeOfFile \ lSizeToSplit) + 1
 
 sHeader = "SPLITIT" & Format$(iFileCounter, "000") & Format$(iNumberOfFiles, "000") & Right$(Filename, 3)
 sNewFilename = Left$(Filename, Len(Filename) - 3) & Format$(iFileCounter, "000")
 Open sNewFilename For Binary As #2
 Put #2, , sHeader 'Write the header
 lSizeOfCurrentFile = Len(sHeader)
 
 While Not EOF(1)
 Me.Caption = "File Split : " & iFileCounter & " (" & Int(lSizeOfCurrentFile / 1024) & " Kb)"
 Me.Refresh
 sBuffer = Input(10240, #1)
 lSizeOfCurrentFile = lSizeOfCurrentFile + Len(sBuffer)
 If lSizeOfCurrentFile > lSizeToSplit Then
'Write last bit
lEndPart = Len(sBuffer) - (lSizeOfCurrentFile - lSizeToSplit) + Len(sHeader)
Put #2, , Mid$(sBuffer, 1, lEndPart)
Close #2
'Make new file
iFileCounter = iFileCounter + 1
sHeader = "SPLITIT" & Format$(iFileCounter, "000") & Format$(iNumberOfFiles, "000") & Right$(Filename, 3)
sNewFilename = Left$(Filename, Len(Filename) - 3) & Format$(iFileCounter, "000")
Open sNewFilename For Binary As #2
Put #2, , sHeader 'Write the header
'Put Rest of buffer read
Put #2, , Mid$(sBuffer, lEndPart + 1)
lSizeOfCurrentFile = Len(sHeader) + (Len(sBuffer) - lEndPart)
Else
Put #2, , sBuffer
 End If
 Wend
 
 Me.Caption = "Finished"
 
 Close #2
 Close #1
 SplitFile = True
 Exit Function
handelsplit:
 SplitFile = False
 MsgBox Err.Description, 16, "Error #" & Err.Number
 Exit Function
End Function
Function AssembleFile(Filename As String) As Boolean
On Error GoTo handelassemble
 Dim sHeader As String * 16
 Dim sBuffer As String '10Kb buffer
 Dim sFileExt As String, iNumberOfFiles As Integer
 Dim iCurrentFileNumber As Integer
 Dim iCounter As Integer, sTempFilename As String
 Dim sNewFilename As String
 If MsgBox("Continue to assemble file?", 4 + 256 + 32, "Assemble?") = vbNo Then
 AssembleFile = False
 Exit Function
 End If
 
 Open Filename For Binary As #1
 sHeader = Input(Len(sHeader), #1)
 
 'Check if it's a split file !!!
 If Mid$(sHeader, 1, 7) <> "SPLITIT" Then
 MsgBox "This is not a split file ;) nice try!"
 AssembleFile = False
 Exit Function
 Else
 'The first file is a split file ok
 'Read the header values
 iCurrentFileNumber = Val(Mid$(sHeader, 8, 3))
 iNumberOfFiles = Val(Mid$(sHeader, 11, 3))
 sFileExt = Mid$(sHeader, 14, 3)
 If iCurrentFileNumber <> 0 Then
MsgBox "This is not the first file in the sequence!!! AAAGGHH!"
AssembleFile = False
Exit Function
 End If
 End If
 
 Close #1
 
 sNewFilename = Left$(Filename, Len(Filename) - 3) & sFileExt
 'Create the assembled file
 Open sNewFilename For Binary As #2
 
 'Assemble files
 For iCounter = 0 To iNumberOfFiles - 1
 sTempFilename = Left$(Filename, Len(Filename) - 3) & Format$(iCounter, "000")
 
 Me.Caption = "File Assemble : " & sTempFilename
 Me.Refresh
 
 Open sTempFilename For Binary As #1
 sHeader = Input(Len(sHeader), #1)
 If Mid$(sHeader, 1, 7) <> "SPLITIT" Then
MsgBox "This is not a split file ;) nice try! " & sTempFilename
AssembleFile = False
Exit Function
 End If
 iCurrentFileNumber = Val(Mid$(sHeader, 8, 3))
 If iCurrentFileNumber <> iCounter Then
MsgBox "The file '" & sTempFilename & "' is out of sequence!! AARRGHH!"
AssembleFile = False
Close #2
Close #1
Exit Function
 End If
 While Not EOF(1)
sBuffer = Input(10240, #1)
Put #2, , sBuffer
 Wend
 Close #1
 Next iCounter
 Close #2
 
 Me.Caption = "Finished"
 
 AssembleFile = True
 Exit Function
handelassemble:
 AssembleFile = False
 MsgBox Err.Description, 16, "Error #" & Err.Number
 Exit Function
End Function


Other 23 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

5/12/1999 3:19:00 AMDan Messenger

I tried this program and it seemed to work fine. I told it to split an 5mb file into 720k files. It was working away and creating the files fine untill it got into the 8th file. At this point an error message occured saying 'Input past end of file'.

Any solutions?

Thanks in advance
(If this comment was disrespectful, please report it.)

 
5/13/1999 1:30:00 AMRiaan Aspeling

Hi Dan
The Form_Load event had a type-o with one of the ItemData fields. It read:

Combo1.ItemData(Combo1.NewIndex) = 702

I've changed it to

Combo1.ItemData(Combo1.NewIndex) = 720

I hope that fixes it ;)

(If this comment was disrespectful, please report it.)

 
5/15/1999 7:42:00 AMAdam

It is nice code, and it is work fine, I did some changes in it, I add progressbar,
GREAT JOB..
Thanks for Riaan Aspeling.


(If this comment was disrespectful, please report it.)

 
5/15/1999 7:46:00 AMAdam

It is nice code, and it is work fine, I did some changes in it, I added progressbar,
GREAT JOB..
Thanks for Riaan Aspeling.


(If this comment was disrespectful, please report it.)

 
5/15/1999 2:25:00 PMDaniel

Adam, can you please mail me your source code with the progressbar?

Thanks
(If this comment was disrespectful, please report it.)

 
5/27/1999 10:19:00 AMAfdah Wahid

Cool proggy. Keep up the good work!
By the way any of you guys know how to make it works faster?
Because it takes ages to split a 20MB file.
(If this comment was disrespectful, please report it.)

 
5/29/1999 10:12:00 AMBill

good program I got it to work great and i changed it around a little but I have two problems -> one, when you split a file on the desktop and then try to assemble it it causes and error, why is this? two, how would i change it so the user can change the destination file of the split up files? If someone could please help me with these problems that would be great
(If this comment was disrespectful, please report it.)

 
5/29/1999 12:47:00 PMJustin Uy

Great concept. I'm having a constant problem though. Every time the splitter gets to the last file to split it says "Input past end of file" "Error #62" Is there a way to take care of this? Thanks.
(If this comment was disrespectful, please report it.)

 
5/29/1999 12:53:00 PMJustin Uy

Oh yeah. When I removed the error handling code this is the text it wanted me to debug.

sBuffer = Input(10240, #1)
(If this comment was disrespectful, please report it.)

 
5/30/1999 6:35:00 AMRiaan Aspeling

Hi Justin.
Are you using VB5 Service Pack 3. I've had another coder ask me the same question , he was using VB4.. I believe VB5 handels the eof senario by just dumping the last bit of data into the sBuffer string. eg. :
The file is 18kb... It will read the first 10kb ok .. second time round the sbuffer will be 8kb. (with vb4 it will give you and error : Past End Of File, or something... this can be modified by checking the size of the remaining file constantly.. eg:
Make a variable with the initial size of the file x = lof(FileNumber) decrement the x variable by 10240 (10kb) for every read, then check the x variable before you read from the file to see if it is smaller than 10kb (10240) and adjust the read like :
sBuffer = input(x,FileNumber)
Where x will be smaller then 10240 and only read the phisical amount of bytes left in the file.
Kind Regards
(If this comment was disrespectful, please report it.)

 
5/30/1999 6:43:00 AMRiaan Aspeling

Hi Afdah.

Thanks for the compliment !

To speed this program up I believe you can just make the read buffer bigger. (Don't exceed 64Kb though.. it will crash your system or give totally unpredictable results.

Check where you find 10240... this is the buffer size 10kb. Change this to .... let's say 20480 (20kb) this should make a speed difference...

Keep Coding

Kind Regards
Riaan Aspeling
(If this comment was disrespectful, please report it.)

 
5/30/1999 5:56:00 PMErik

How do you have it split the files up with the extensions for exaple: .nfs?
(If this comment was disrespectful, please report it.)

 
5/31/1999 12:01:00 PMJustin DuJardin

thats some nice code there riaan it seems to work fine for me but i removed the 10k read buffer and did a 32k read buffer to speed it up and i also added a progress bar and some other small changes but other than that it is great code it inspired me to create the opposite so i am creating a file packer it takes many file kinda like winzip and packs them into one file but they arent compressed so its kinda pointless but its a challenge to do it without messing up any file headers especially on graphic files.
(If this comment was disrespectful, please report it.)

 
6/10/1999 8:01:00 AMAniruddha Ray

Hi Justin:

Is it possible to get a copy of the code with the progress bar? If so, could you pl. e-mail me one?

Thanks.

Ray.
(If this comment was disrespectful, please report it.)

 
8/15/1999 8:11:00 AMDMess

Hi, i have been lookin through this code and presume that what it does is open the file in binary mode or something and then puts it into loads of little files. Does anybody know how to put this info into a text file or something so that an other program can read it a reconstruct the file??

Any help would be appriecited

DMess
(If this comment was disrespectful, please report it.)

 
9/12/1999 5:58:00 PMAlexandre Gosselin

Can anyone tell me how to add a progress bar to this program?
(If this comment was disrespectful, please report it.)

 
9/19/1999 4:28:00 PMAnonymous

This code is garbage. It would take minutes to split a 10 megabyte file! What's wrong with using the windows APIs to do it the right way? I'm glad you've taken up VB as a hobby... But let's try to put some more thought into these submissions.
(If this comment was disrespectful, please report it.)

 
9/27/1999 2:51:00 PMJake

hey riaan man this code works great at dsaccembling but i cant get it to reassemble. Could you or someone else gimmie some help?
(If this comment was disrespectful, please report it.)

 
9/30/1999 12:36:00 PMPaul Mather

Yes, download my code "Large File Splitter"
(If this comment was disrespectful, please report it.)

 
10/6/1999 2:46:00 PMWoooooooodard

Ne1 got the code to recombine the files after they are split? Like in order to split an exe, and send it through multiple connections, and then recombine it.
(If this comment was disrespectful, please report it.)

 
10/29/1999 2:33:00 PMPaul Mather

Here's an efficient way of splitting files: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3582
(If this comment was disrespectful, please report it.)

 
11/30/1999 5:42:00 AMAndrew Timberlake

This program is really usefull for getting roms to abd from college on disk since most are more than 1 meg

Thanks
(If this comment was disrespectful, please report it.)

 
4/10/2004 5:55:53 AM

i'm working for copying single file with a progressbar in it well the bottom line is.. i want to include a cancel button so i can terminate the operation intermittently and so the code should let me do it! hope somebody can help. MABUHAY!
(If this comment was disrespectful, please report it.)

 
1/9/2006 5:54:04 PMMatt DeKok

I didn't know it was possible to have more than 5 globes, cool!
(If this comment was disrespectful, please report it.)

 
12/29/2009 8:34:01 PMChris

Wonder why all these splitters say large files?
Anything over 2 gig does not work.

(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.