SplitFile()

Function SplitFile(SourceFile As String, Optional ByVal DestDir As String = ".\", _
Optional ChunkSize As Long = 1048576, Optional Assume8Dot3 As Boolean = False) As Long

'//SplitFile function - Copyright (C)1997-1999 michiel de bruijn <mdb@x42.net>
'//Redistribution permitted, provided this comment block is left intact
'//For full licensing terms, see
http://www.x42.net/Code/license.html

Dim hSourceFile As Long, hDestFile As Long, Chunks As Long, LastChunkSize As Long
Dim CurChunkSize As Long, Blocks As Long, CurBlockSize As Long, BaseFile As String
Dim x As Long, i As Long, i2 As Long, DestFile As String, LastBlockSize As Long
Dim Buffer As String
Const BLOCKSIZE = 32768

On Local Error Resume Next

'//Determine number of output files, as well as the exact size of the last file
Chunks = FileLen(SourceFile) \ ChunkSize
LastChunkSize = FileLen(SourceFile) Mod ChunkSize
CurChunkSize = ChunkSize

'//Determine base output file name, and do special handling in case of an 8.3 file system
BaseFile = SourceFile
If Assume8Dot3 Then
x = InStrRev(BaseFile, ".")
If x <> 0 Then BaseFile = Left(BaseFile, x - 1)
BaseFile = Left(BaseFile, 8)
End If

'//Remove source directory from base filename and prepend target directory
x = InStrRev(BaseFile, "\")
If x <> 0 Then BaseFile = Mid(BaseFile, x + 1)
If Right(DestDir, 1) <> "\" Then DestDir = DestDir & "\"
BaseFile = DestDir & BaseFile

'//Open source file, and start writing output files
hSourceFile = FreeFile
Err = 0
Open SourceFile For Binary Shared As #hSourceFile

If Err <> 0 Then
SplitFile = SF_SOURCEFILE_UNREADABLE
Exit Function
End If

For i = 1 To Chunks + 1
If i = Chunks + 1 Then CurChunkSize = LastChunkSize
DestFile = BaseFile & "." & Right("000" & Hex(i), 3)
If Not Assume8Dot3 Then DestFile = DestFile & "~"
'//Delete destination file if it already exists...
If Len(Dir(DestFile)) Then
Err = 0
Kill DestFile
If Err Then
SplitFile = SF_DESTFILE_UNWRITABLE
Exit Function
End If
End If

'//Determine # of blocks in this chunk, and do the copy thing
hDestFile = FreeFile
Open DestFile For Binary As #hDestFile
Blocks = CurChunkSize \ BLOCKSIZE
CurBlockSize = BLOCKSIZE
LastBlockSize = CurChunkSize Mod BLOCKSIZE
For i2 = 1 To Blocks + 1
If i2 = Blocks + 1 Then CurBlockSize = LastBlockSize
Buffer = Space(CurBlockSize)
Get #hSourceFile, , Buffer
Put #hDestFile, , Buffer
Next
Close #hDestFile

Next

Close #hSourceFile

SplitFile = SF_OK

End Function

 

[Home] [Publications] [Code] [Contact Us] [Legal]