Can anyone tell me how to use the percentage bar while copying files. The files i am copying within VB a large, so i would like to have some feedback to the copy process and how far it is going.
Regards
Printable View
Can anyone tell me how to use the percentage bar while copying files. The files i am copying within VB a large, so i would like to have some feedback to the copy process and how far it is going.
Regards
If you are using the FileCopy command or sometinhg similar, execution will stop at that line until the command is completed. So you will not be able to update a progress bar or anything.
Maybe someone out there knows how to do this, i don't.
All i can suggest is that you write your own copy function. As an example. You could set the progress bars Max property to the size of the file being copied. You then copy the file in chunks. Every time you copy a chunk, you update the progress bars value by the size of the chunk being copied. I know this is a pain in the arse, but i don't know a better way.
Maybe someone else does?
It's pain in your arse indeed. But you can make those chunks to 100 for every file and it will give you an update for each %. Open the files in binary, and dump the chunks in variable length string by using Get and Put statement. Also you may search for an activeX
Do you have any sample code for that..? this is quite new too me.
This is a binary chunk copying program that i wrote a while ago, when i was helping someone else on this forum. It had a flaw in it earlier, but it works perfectly now.
I know for a fact that other people do it slightly differently to me, but give ten programmers the same problem and you will get 10 different solutions.
For this to work you need:
textBox called "txtInput"
textBox called "txtOutput"
textBox called "txtChunkSize"
commandButton called "cmdCopy"
progressBar called "ProgressBar1"
Then when the program runs enter the input file and the output file. Also enter the size of the chunks in bytes.
There is no error checking, so if a file doesn't exist it will crash, but you can sort that out.
Hope this helps.;)Code:Option Explicit
Dim CHUNK_SIZE As Long
Dim blCopying As Boolean
Private Sub cmdCopy_Click()
Dim i As Long
Dim iLength As Long, iMod As Long, iRemain As Long
Dim myFile As String, myOutFile As String
Dim myByte() As Byte
blCopying = True
myFile = txtInput.Text
myOutFile = txtOutput.Text
CHUNK_SIZE = txtChunkSize.Text
'open the files
Open myFile For Binary As #1
Open myOutFile For Binary As #2
'get the length
iLength = FileLen(myFile)
'find out how many time 1024 goes into iLength
iMod = iLength \ CHUNK_SIZE
'finds the remainder
iRemain = iLength Mod CHUNK_SIZE
ReDim myByte(CHUNK_SIZE - 1) As Byte
ProgressBar1.Max = iMod
ProgressBar1.Value = 0
'loop for however many times 1024 goes into the filelength
For i = 1 To iMod
Get #1, , myByte
Put #2, , myByte
Me.Caption = "Progress = " & Int(((i * CHUNK_SIZE) / iLength) * 100) & "%"
ProgressBar1.Value = i
DoEvents
If blCopying = False Then
'in case of close
Close #1
Close #2
Unload Me
Exit Sub
End If
Next i
'get the remaining charcters
If iRemain > 0 Then
ReDim myByte(iRemain - 1) As Byte
Get #1, , myByte
Put #2, , myByte
End If
ProgressBar1.Value = ProgressBar1.Max
Me.Caption = "Finished!"
Close #1
Close #2
blCopying = False
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'if the user tries to quit the program then we need
'to set a boolean to stop the loop
If blCopying = True Then
blCopying = False
End If
End Sub
What component or reference in the percentage bar, i do not seem to have it on my components list?
Regards
Go to the Project menu in VB. Select Components. Now tick the "Microsoft Windows Common Controls 6.0". Now click ok. The progress bar should be on the toolbox now.
I've found the percentage bar, but what about the path for the readable files.?
I dont' know exactly what you mean.
if you only type in a file name, and no directory, the program will look for th file in the directory it is runnig in. e.g. if you type "myInputFile.txt" and your project is in "c:\myProject" then the path is c:\myProject\myInputFile.txt
Or you can specify the complete path. e.g. c:\someFolder\myOutputFile.txt