File copy progress
File copy progress
File copy progress
File copy progress
File copy progress
File copy progress File copy progress File copy progress File copy progress File copy progress File copy progress File copy progress File copy progress
File copy progress File copy progress
File copy progress
Go Back  Xtreme Visual Basic Talk > > > File copy progress


Reply
 
Thread Tools Display Modes
  #1  
Old 03-27-2009, 04:17 AM
stokeentertainm's Avatar
stokeentertainm stokeentertainm is offline
Contributor
 
Join Date: Jan 2005
Location: Stoke-on-Trent, UK
Posts: 439
Default File copy progress


Hey all,

I've got an app which uses a background worker to copy files. It works fine but some files are quite large and it "appears" as though the app isn't doing anything. Is there an alternative to the File.Copy method of copying files so I can report on the progress of individual file copies, like the percentage complete?
Reply With Quote
  #2  
Old 03-27-2009, 04:50 AM
wildfire1982 wildfire1982 is offline
Senior Contributor
 
Join Date: Jul 2003
Location: Southampton UK
Posts: 801
Default

Hello,

Yes there are a few different ways. I have seen API's to do it, however, I think it would be easier to do it a slightly different way.

As if you were downloading a file using a socket, you could read a file to a byte array and write that byte array to another file (essentially copying).

Now to get the progress, you could simply read and write chunks at a time, the chunk size, the number of chunks copied and the total file size could then be used to work out the current percentage complete.

Add some threading capabilities and delegates and you have it.

Does all of that make sense? Any parts you missed?

There may be more efficient ways to do this but it will be an entertaining excercise and because of the way it works, its customisable.
__________________
Chris
Reply With Quote
  #3  
Old 03-27-2009, 04:55 AM
stokeentertainm's Avatar
stokeentertainm stokeentertainm is offline
Contributor
 
Join Date: Jan 2005
Location: Stoke-on-Trent, UK
Posts: 439
Default

Thanks, yes, I understand what I need to do, but how? Does the File.Copy method support reading/writing partial files?
Reply With Quote
  #4  
Old 03-27-2009, 05:21 AM
wildfire1982 wildfire1982 is offline
Senior Contributor
 
Join Date: Jul 2003
Location: Southampton UK
Posts: 801
Default

It doesnt, however, you can scrap the file.copy and use:
Code:
Dim fW As New IO.StreamWriter(copyToFileLocation)
and
Code:
Dim fR As New IO.StreamReader(copyFromFileLocation)
to read and write to a file. Then use a buffer (byte array) to read into. Keep reading into the buffer and writing the buffer to the new file while you have have not read all of the file, incrementing progress as you read and write.

Im just writing a little bit of code to help.
__________________
Chris
Reply With Quote
  #5  
Old 03-27-2009, 05:27 AM
stokeentertainm's Avatar
stokeentertainm stokeentertainm is offline
Contributor
 
Join Date: Jan 2005
Location: Stoke-on-Trent, UK
Posts: 439
Default

Oh yeah, of course. That method reminds me of my old Amiga programming days

Do you think it would just be best to use a buffer which is 1/100th the size of the file as I am needing a percentage, or could that cause memory trouble on very large files?
Reply With Quote
  #6  
Old 03-27-2009, 05:38 AM
wildfire1982 wildfire1982 is offline
Senior Contributor
 
Join Date: Jul 2003
Location: Southampton UK
Posts: 801
Default

Im not sure to be honest, I think that might cause some problems. Have a go and see how it works.

Here is the kind of thing I mean (not tested)
Code:
Dim fR As New IO.FileStream(copyFromFileLocation, IO.FileMode.Open) Dim fW As New IO.FileStream(copyToFileLocation, IO.FileMode.CreateNew) Const bufferSize As Integer = 1024 Dim theBuffer(bufferSize - 1) As Byte Dim count As Integer = bufferSize Dim copiedData As Integer = 0 While count > 0 count = fR.Read(theBuffer, 0, count) fW.Write(theBuffer, copiedData, bufferSize) copiedData += count RaiseEvent onProgress(CInt(100 / filesize * copiedData)) End While fW.Close() fR.Close()

Obviously error handling is needed as is some tweaking but this should give you the general idea.

You can create your own event if you like to do progress however dont forget you will need to work with threads and delegates to get this working nicely otherwise your progressbar won't update.

Any use?
__________________
Chris
Reply With Quote
  #7  
Old 03-27-2009, 05:42 AM
stokeentertainm's Avatar
stokeentertainm stokeentertainm is offline
Contributor
 
Join Date: Jan 2005
Location: Stoke-on-Trent, UK
Posts: 439
Default

That's great thanks

Obviously, the file probably won't be an exact multiple of the buffer size. How do you think is the best way to handle the remaining bytes when it's less than the buffersize, or will your code do that already?
Reply With Quote
  #8  
Old 03-27-2009, 05:48 AM
wildfire1982 wildfire1982 is offline
Senior Contributor
 
Join Date: Jul 2003
Location: Southampton UK
Posts: 801
Default

Change the buffersize to count in the fW.Write line.

The count variable is given the value of the number of bytes read and so will write only the bytes which have been written to.
__________________
Chris
Reply With Quote
  #9  
Old 03-27-2009, 05:57 AM
darkforcesjedi's Avatar
darkforcesjediFile copy progress darkforcesjedi is offline
Trust me, I'm an

* Expert *
 
Join Date: Apr 2001
Location: In ur base, pwnin d00dz
Posts: 1,964
Default

This is not an ideal solution as it does not preserve file attributes, metadata, and alternate data streams. Depending on what you're copying, this may or may not be acceptable.
__________________
To err is human; to debug, divine.
Reply With Quote
  #10  
Old 03-27-2009, 06:03 AM
wildfire1982 wildfire1982 is offline
Senior Contributor
 
Join Date: Jul 2003
Location: Southampton UK
Posts: 801
Default

Very true. There is always the API route, however personally, I would prefer to make this work nicely, get it to work with your above details and add it to a library for future use.

Do you have another solution?
__________________
Chris
Reply With Quote
  #11  
Old 03-27-2009, 06:07 AM
stokeentertainm's Avatar
stokeentertainm stokeentertainm is offline
Contributor
 
Join Date: Jan 2005
Location: Stoke-on-Trent, UK
Posts: 439
Default

The solution seems okay. Couldn't I copy the file attributes after the copy has completed?

Is there an API example or am I way out of my league there?
Reply With Quote
  #12  
Old 03-27-2009, 06:15 AM
wildfire1982 wildfire1982 is offline
Senior Contributor
 
Join Date: Jul 2003
Location: Southampton UK
Posts: 801
Default

That depends how much you like API programming

Below is a link to the msdn for a suitable API
SHFileOperation

You are in luck!
A solution
__________________
Chris
Reply With Quote
  #13  
Old 03-27-2009, 06:28 AM
stokeentertainm's Avatar
stokeentertainm stokeentertainm is offline
Contributor
 
Join Date: Jan 2005
Location: Stoke-on-Trent, UK
Posts: 439
Default

Well I didn't really know what it was before, but now I don't like it

Thanks for that, I will give it a go!
Reply With Quote
  #14  
Old 03-27-2009, 07:04 AM
stokeentertainm's Avatar
stokeentertainm stokeentertainm is offline
Contributor
 
Join Date: Jan 2005
Location: Stoke-on-Trent, UK
Posts: 439
Default

Ok, so I can see how the code works, but I can't see how it solves my original problem. How do I get the copy progress value?
Reply With Quote
  #15  
Old 03-27-2009, 07:15 AM
wildfire1982 wildfire1982 is offline
Senior Contributor
 
Join Date: Jul 2003
Location: Southampton UK
Posts: 801
Default

Mine or the API?
__________________
Chris
Reply With Quote
  #16  
Old 03-27-2009, 07:41 AM
stokeentertainm's Avatar
stokeentertainm stokeentertainm is offline
Contributor
 
Join Date: Jan 2005
Location: Stoke-on-Trent, UK
Posts: 439
Default

Sorry, the API code.
Reply With Quote
  #17  
Old 03-27-2009, 07:59 AM
stokeentertainm's Avatar
stokeentertainm stokeentertainm is offline
Contributor
 
Join Date: Jan 2005
Location: Stoke-on-Trent, UK
Posts: 439
Default

Actually I can't get your original code to work either. I've tried to amend a few things but it always says that the index is out of bounds after the first write.
Reply With Quote
  #18  
Old 03-27-2009, 08:05 AM
stokeentertainm's Avatar
stokeentertainm stokeentertainm is offline
Contributor
 
Join Date: Jan 2005
Location: Stoke-on-Trent, UK
Posts: 439
Default

Ah, got it, just amended ...

Code:
count = fR.Read(theBuffer, 0, bufferSize)
fW.Write(theBuffer, 0, count)
copiedData += count
Cheers
Reply With Quote
  #19  
Old 03-27-2009, 08:12 AM
wildfire1982 wildfire1982 is offline
Senior Contributor
 
Join Date: Jul 2003
Location: Southampton UK
Posts: 801
Default

I believe the API is designed to use the standard windows file copy which means that it can show the standard dialog box.

In the lpszProgressTitle you can specify the title of the progress dialog which is to represent the progress of your copy.
__________________
Chris
Reply With Quote
  #20  
Old 03-27-2009, 08:14 AM
wildfire1982 wildfire1982 is offline
Senior Contributor
 
Join Date: Jul 2003
Location: Southampton UK
Posts: 801
Default

Sorry, I have a habit of doing that with this sort of thing.

Didnt have chance to test my code but thought it would be close
__________________
Chris
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
File copy progress
File copy progress
File copy progress File copy progress
File copy progress
File copy progress
File copy progress File copy progress File copy progress File copy progress File copy progress File copy progress File copy progress
File copy progress
File copy progress
 
File copy progress
File copy progress
 
-->