Someone on my favorite Visual Basic forums (
www.vbforums.com) gave me this alternative to the file copy command.
Code:
const BlockSize = 16384
Dim sInput As String
Open SourceFile$ For Binary Lock Read As #1
Open DestFile$ For Binary Lock Write As #2
While Not Eof(#1)
sInput = Input$(BlockSize, #1)
Put #2,,sInput
Debug.Print cStr((Seek(#1)/Lof(#1)) * 100) & "% completed."
Wend
Close
OK, the problem with VBForums is that they dont comment or explain it so I have come back and show you how it works.
This just a standard block size, the algorithm will copy 16384 bytes at a time, this break up allows for the progressbar to increment.
The next two lines abviously open the files in binary format. They must be opened in binary format since we are copying by bytes. You will need to insert the filenames/variables.
Code:
Open SourceFile$ For Binary Lock Read As #1
Open DestFile$ For Binary Lock Write As #2
Performs a loop while not End Of File for file #1
Basically this means it will continue to copy till there is nothing left to copy. Duh.
This is used to input from a file in binary format using the blocksize.
Code:
sInput = Input$(BlockSize, #1)
This is used to put the data you read into the new file.
This is the confusing part, but it doesnt have to be. The seek funtion returns the current position of a file being read in. Basically there is a marker as long as the file is open that marks a position just after the last read byte, this is done automatically. The LOF function returns the total length (in bytes) of a file. The current amount read divided by the total amount multiplied by the constant 100 (100 percent) gives you the percentage complete. This is printed to the debug window. (This isnt what you want, I'll explain how to modifiy it but I want you to understand all the funtions listed here for future reference)
Code:
Debug.Print cStr((Seek(#1)/Lof(#1)) * 100) & "% completed."
Wend marks the end the loop if you didnt know that, Close closes a file, if the number is omitted, it closes all active files. Therefore both files are closed here.
Now how do we get what we want out of this? Well, its easier than the code we just looked over actually. Lets say we are using the better VBOverdrive progressbar I attached above. (The only important difference here in code is that it uses the name "Position" instead of "Value")
Here we set the min value to 0 and the max value to the total size of the file. You can set the Min value at design time, but the Max value must be set at runtime after the file is opened.
Next we replace the Debug.Print statement with an increment for the vboProgressbar using the Seek function to find the current position. And thats it! Hope this helps. You can also change the block size to 1 if you want a more smooth progressbar increase, however, the smaller the block size the less efficient the copy is and the more time it takes to do it.
Code:
const BlockSize = 16384
Dim sInput As String
Open SourceFile$ For Binary Lock Read As #1
Open DestFile$ For Binary Lock Write As #2
vboProgressbar.Max = Lof(#1)
vboProgressbar.Position = 0
While Not Eof(#1)
sInput = Input$(BlockSize, #1)
Put #2,,sInput
vboProgressbar.Position = Seek(#1)
Wend
Close
Theoretically this should work. I havent tested it though. If you have any problems let me know. Also this should reset with every file, a running total of all files being copied in this manner (if you are copying multiple files) would be much more difficut.