BleepingComputer.com: Copying A File

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Copying A File

#1 User is offline   Billy O'Neal 

  • Bleepin Engineer GRADUATE
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,391
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

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?

#2 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,604
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

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.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#3 User is offline   Billy O'Neal 

  • Bleepin Engineer GRADUATE
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,391
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

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.

#4 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,604
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

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.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#5 User is offline   Billy O'Neal 

  • Bleepin Engineer GRADUATE
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,391
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

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?

#6 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,604
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 21 February 2008 - 10:34 PM

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.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#7 User is offline   Keithuk 

  • Distinguished Member
  • PipPipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 800
  • Joined: 25-January 07
  • Gender:Male

Posted 22 February 2008 - 10:49 AM

View PostBilly O, on Feb 22 2008, 02:52 AM, said:

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

Martin2k

Windows ME (spare computer)
Windows XP 2002 Professional SP3 (desktop computer)
Windows Vista Home Premium SP2 (laptop computer)

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users