Computer Help and Spyware Removal Computer Help and Spyware Removal Computer Help and Spyware Removal Computer Help Forums Windows Startup Programs Database Spyware and Malware Removal Guides Computer Tutorials Uninstall Database File Database Computer Glossary Computer Resources
 

Welcome Guest ( Log In | Click here to Register a free account now! )



Register a free account to unlock additional features at BleepingComputer.com
Welcome to Bleeping Computer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.
Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

 
Reply to this topicStart new topic
> Copying A File
Billy O'Neal
post Feb 21 2008, 01:12 AM
Post #1


Big Brother Bill is Watching You
******

Group: HJT Team
Posts: 2,842
Joined: 17-January 08
From: Northfield, Ohio
Member No.: 184,215



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?


--------------------
In the event I fail to reply within twenty-four hours, feel free to send me a PM (By clicking this link). Sometimes things get overlooked... I don't want to overlook ya!
Have I helped you? If so, please sign My Guestbook to help me get into college!
Join BC.com's Folding Team (#38444)! Help Stanford University find a cure for diseases!
Go to the top of the page
 
+Quote Post
groovicus
post Feb 21 2008, 07:32 AM
Post #2


Hail Groovicus!
******

Group: Site Admin
Posts: 5,849
Joined: 5-June 04
From: Vermillion, SD
Member No.: 689



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.


--------------------
Go to the top of the page
 
+Quote Post
Billy O'Neal
post Feb 21 2008, 01:25 PM
Post #3


Big Brother Bill is Watching You
******

Group: HJT Team
Posts: 2,842
Joined: 17-January 08
From: Northfield, Ohio
Member No.: 184,215



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.


--------------------
In the event I fail to reply within twenty-four hours, feel free to send me a PM (By clicking this link). Sometimes things get overlooked... I don't want to overlook ya!
Have I helped you? If so, please sign My Guestbook to help me get into college!
Join BC.com's Folding Team (#38444)! Help Stanford University find a cure for diseases!
Go to the top of the page
 
+Quote Post
groovicus
post Feb 21 2008, 09:40 PM
Post #4


Hail Groovicus!
******

Group: Site Admin
Posts: 5,849
Joined: 5-June 04
From: Vermillion, SD
Member No.: 689



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.


--------------------
Go to the top of the page
 
+Quote Post
Billy O'Neal
post Feb 21 2008, 09:52 PM
Post #5


Big Brother Bill is Watching You
******

Group: HJT Team
Posts: 2,842
Joined: 17-January 08
From: Northfield, Ohio
Member No.: 184,215



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?


--------------------
In the event I fail to reply within twenty-four hours, feel free to send me a PM (By clicking this link). Sometimes things get overlooked... I don't want to overlook ya!
Have I helped you? If so, please sign My Guestbook to help me get into college!
Join BC.com's Folding Team (#38444)! Help Stanford University find a cure for diseases!
Go to the top of the page
 
+Quote Post
groovicus
post Feb 21 2008, 10:34 PM
Post #6


Hail Groovicus!
******

Group: Site Admin
Posts: 5,849
Joined: 5-June 04
From: Vermillion, SD
Member No.: 689



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.


--------------------
Go to the top of the page
 
+Quote Post
Keithuk
post Feb 22 2008, 10:49 AM
Post #7


Forum Regular
***

Group: Members
Posts: 248
Joined: 25-January 07
Member No.: 108,272



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


--------------------
Keith

http://www.martin2k.co.uk/forums

I've been programming with VB for 12 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 7th September 2008 - 06:20 PM


Advertise   |   About Us   |   Terms of Use   |   Privacy Policy   |   Contact Us   |   Site Map   |   Chat   |   Tutorials   |   Uninstall List
Discussion Forums   |   The Computer Glossary   |   Resources   |   RSS Feeds   |   Startups   |   The File Database   |   Malware Removal Guides

© 2003-2008 All Rights Reserved Bleeping Computer LLC.