Help - Search - Members - Calendar
Full Version: Copying A File
BleepingComputer.com > Software > Programming
   
Billy O'Neal
Alright.... before you all rip my head off with "Just use My.Computer.Filesystem.CopyFile(ByVal Source as String, ByVal Destination as String)", let me explain:
Here is my method:
CODE
    Private Sub CopyFile(ByVal OldFile As String, ByVal NewFile As String)
        Const bufferLength As Integer = 1000 'Number of bytes to do at once
        log(OldFile + " -> " + NewFile) 'Log the fact that we are copying these files
        Try
            'Create the directory if it does not exist
            Directory.CreateDirectory(NewFile.Substring(0, NewFile.LastIndexOf("\")))
            'Open the files
            Dim FS As New FileStream(OldFile, FileMode.Open)
            Dim FW As New FileStream(NewFile, FileMode.Create)
            'Initialize a buffer to hold the data in ram temporarily
            Dim Buffer(bufferLength) As Byte
            'Initialize "Pointers" of sorts to deal with the buffer
            'Copy the file
            Dim cur As Long
            For cur = 0 To (FS.Length - 1) Step bufferLength
                'if whats left in the file cannot fill our buffer, than we want it so the buffer does not have garbage for the
                'of the file. So, if the amount of distance left is less than the bufferlength, jump to the else
                If (FS.Length - FS.Position) > bufferLength Then
                    FS.Read(Buffer, 0, bufferLength)
                    FW.Write(Buffer, 0, bufferLength)
                    RaiseEvent fileProgressUpdate(CInt(cur / FS.Position * 100))
                Else
                    Dim tmp As Integer = CInt((FS.Length - FS.Position))
                    RaiseEvent fileProgressUpdate(100)
                    FS.Read(Buffer, 0, bufferLength)
                    FW.Write(Buffer, 0, tmp)
                End If
            Next
            FS.Close()
            FW.Close()
            RaiseEvent fileProgressUpdate(0)
        Catch e As Exception
            RaiseEvent fileProgressUpdate(0)
            log("ERROR: " + e.Message)
        End Try
    End Sub

The only reason I am going through something so complex for something so simple is to get a progressbar on the screen (which is what the FileProgressUpdate event is for).

My problem is that the else block in the for loop:
CODE
                    Dim tmp As Integer = CInt((FS.Length - FS.Position))
                    RaiseEvent fileProgressUpdate(100)
                    FS.Read(Buffer, 0, bufferLength)
                    FW.Write(Buffer, 0, tmp)


does not seem to be executing. Every file I run this thing on has the last bufferLength bytes cut off the end.

One more thing, I tried just removing that if...else block alltogether and having it read/write bufferlength every time, but that writes a ton of garbage at the end of the file.

Because this is going into a backup program, its kinda important that it can copy a file.

Any ideas?
groovicus
IF you are not sure what it is doing, you should be printing out statements to the console like "I am in the else loop", or "Wscript.Echo FS.Length - FS.Position". You don't really have a question just yet. you have a suspicion. Prove to yourself that it doesn't work.
Billy O'Neal
No, I know its getting into the else block
If I set breakpoints they stop execution of the program inside of the the else block and let me step through the else block line by line. Its some sort of logic error on my part.


This is really odd.... Im thinking there was something wrong with another function.... because it seems to be working....

NVM then.
groovicus
You are trying to use a high level language to do some low level stuff, which is not what it was really designed to do. My suspicion is that you are not flushing the buffers to make sure that everything is out of them before closing the stream though, so whatever is left in the buffer gets stuck there when the program terminates.
Billy O'Neal
Its odd.... it seems to have fixed itself. I think I was using the compiler incorrectly.
Oh, and the Close() call flushes the buffers.
I'd use C++ for this, I like that language better. But I have one problem:
I dont know Win32API.
Do you know of any good books / reference materials that deal with that?
groovicus
QUOTE
Close() call flushes the buffers.

In many languages, it has to be done manually.

I have never done GUI stuff in C++, so I am afraid that i can not recommend anything.
Keithuk
QUOTE(Billy O @ Feb 22 2008, 02:52 AM) *
I dont know Win32API.
Do you know of any good books / reference materials that deal with that?

Well I'm guessing your using VB.Net. So I would recommend API-Guide its free. Its mainly for VB6 but there are a few .Net clips in there. thumbup2.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.