Search Tools Links Login

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


Visual Basic 6, or VB Classic

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)

Original Author: Jon Barker

Code

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

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 97 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.