Question Splitting Large Files

s1ckOh

Well-known member
Joined
Aug 1, 2011
Messages
68
Location
San Diego, Ca
Programming Experience
Beginner
Hello,

I'm working on trying to split a large file from say 3gb to multiple 500mb files with a .1, .2, .3 etc... extension.

My output is one 500mb file and one 28kb file. I can't figure out what I'm missing to continue reading the filestream into the 2nd split file.

here is my code so far... Thanks in advance for any ideas.

Sub SplitFiles(ByVal inFile As String, ByVal newFileSizeBytes As Integer)

Dim i As Short = 1
Dim fsInput As New System.IO.FileStream(inFile, FileMode.Open, FileAccess.Read)
Dim fsOutput As System.IO.FileStream
Dim bytBuffer(4096) As Byte 'holds a block of bytes for processing
Dim lngBytesProcessed As Long = 0 'running count of bytes processed
Dim lngFileLength As Long = fsInput.Length 'the input file's length
Dim intBytesInCurrentBlock As Integer 'current bytes being processed


Try
While lngBytesProcessed < lngFileLength
fsOutput = New FileStream(inFile & "." & i, FileMode.OpenOrCreate, FileAccess.Write)


'Make sure fsOutput is empty
fsOutput.SetLength(0)


'Set progress bar min/max
pbStatus.Value = 0
pbStatus.Maximum = 10000


While fsOutput.Length < newFileSizeBytes


'Read file with the input filestream.
intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096)


'Write output file
fsOutput.Write(bytBuffer, 0, intBytesInCurrentBlock)


'Update lngBytesProcessed
lngBytesProcessed = lngBytesProcessed + CLng(intBytesInCurrentBlock)


'Update Progress Bar
pbStatus.Value = CInt((lngBytesProcessed / newFileSizeBytes) * 10000)


End While


'Close FileStream
fsOutput.Close()


i += 1
End While
fsInput.Close()


Catch
End Try
End Sub
 
This is the problem:
pbStatus.Value = CInt((lngBytesProcessed / newFileSizeBytes) * 10000)
and this is why you didn't already know:
Don't catch exceptions without handling them.

The inner loop has a logical error that prevents it from exiting when it reaches last of the splitted files. That last file will be newFileSizeBytes will probably never happen.

I suggest that you implement progress reporting by percentage. That means you only report the numbers 1 through 100, and one report for each percentage is sufficient.

The current code also indicates you are doing this in UI thread, so have a look at multithreading also, the BackgroundWorker component is a good start.

Hungarian notation is generally a bad idea and is not recommended.
 
It was an easy fix after you pointed out the lack of error handling. Thank you for your help.

I have only been coding for about 4 months now (for fun) so I'm not sure what threading is but I will

definitely look into it. I looked up Hungarian notation, what is a more appropriate style? I've

just been using what I learned through examples and threads.

Thank you again for your help.
 
Back
Top