VB icon

Get a chunk from a file, but specify start and finish (usefull for splitting files / P2P program)

Email
Submitted on: 1/25/2015 9:24:00 PM
By: Jon Barker (from psc cd)  
Level: Intermediate
User Rating: By 6 Users
Compatibility: VB 5.0, VB 6.0
Views: 623
 
     This code can be made to take a certain chunk of your choice from a file. Mainly for making resumable downloads etc, or working on a patch for a program... I made this cos im working on a file sharing program that downloads from multiple sources. SERVERAL UPDATES HAVE TAKEN PLACE (thanks to guys who posted comments improving the code)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Get a chunk from a file, but specify start and finish (usefull for splitting files/ P2P program)
' Description:This code can be made to take a certain chunk of your choice from a file. Mainly for making resumable downloads etc, or working on a patch for a program... I made this cos im working on a file sharing program that downloads from multiple sources. SERVERAL UPDATES HAVE TAKEN PLACE (thanks to guys who posted comments improving the code)
' By: Jon Barker (from psc cd)
'**************************************

Option Explicit
Sub GetFilePart(FilenameFROM As String, FilenameTO As String, StartPos As Long, EndPos As Long)
 Dim bData() As Byte
 Dim sBuf As String
 Dim i As Long
 Dim l As Long
 Dim File1 as Integer
 Dim File2 as Integer
 
 File2 = Freefile
 Open FilenameTO For Binary As #File2
 File1 = Freefile
 Open FilenameFROM For Binary As #File1
 Get #File1, StartPos, bData
 
 ReDim bData(1 To 2048) As Byte
 
 For l = 1 To (EndPos - StartPos) \ 2048
 Get #File1, 2048 * (l - 1) + StartPos, bData
 Put #File2, , bData
 Next
 
 i = (EndPos - StartPos) Mod 2048
 If i <> 0 Then
 ReDim bData(1 To i) As Byte
 Get #File1, , bData
 Put #File2, , bData
 
 End If
 Close #File1
 
 Close #File2
End Sub
Private Sub cmdStart_Click()
 Call GetFilePart("c:\filename.exe", "C:\filename2.exe", 100, 3000)
 
 'this example above would get bytes 100 to 3000 in the program 'c:\filename.exe', and put them into
 'the file 'C:\filename2.exe'. The size of 'C:\filename2.exe' is now 2900 byes.
End Sub


Other 10 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 Intermediate 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


 There are no comments on this submission.
 

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.