Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, 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.

Photo

Copying A File


  • Please log in to reply
6 replies to this topic

#1 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:09:59 AM

Posted 21 February 2008 - 01:12 AM

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:
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:
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?
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:10:59 AM

Posted 21 February 2008 - 07:32 AM

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.

#3 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer

  • Topic Starter

  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:09:59 AM

Posted 21 February 2008 - 01:25 PM

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.
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:10:59 AM

Posted 21 February 2008 - 09:40 PM

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.

#5 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer

  • Topic Starter

  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:09:59 AM

Posted 21 February 2008 - 09:52 PM

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?
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:10:59 AM

Posted 21 February 2008 - 10:34 PM

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.

#7 Keithuk

Keithuk

  • Members
  • 960 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:05:59 PM

Posted 22 February 2008 - 10:49 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. :thumbsup:

Keith

Windows ME (spare computer)
Windows XP 2002 Professional SP3 (desktop computer)
Windows 7 Professional SP1 32bit (laptop computer)

Windows 8 64bit spare drive for laptop computer





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users