views:

687

answers:

3

Using VB 6

In my Project, when I copy the file from one folder to another folder, at the time I want to show the progress bar like copying…., Once the file was copied the Progress bar show’s 100 % Completed.

Code.

'File Copying

Private Sub Copy_Click()
Timer1.Enabled = True
Dim abc As Integer
Dim line As String
abc = FreeFile
Open App.Path & "\DatabasePath.TXT" For Input As #abc
Input #abc, line
databasetext = line
Dim fs As New FileSystemObject, f As File
Set f = fs.GetFile(databasetext)
f.Copy App.Path & "\"
Set fs = Nothing
Close #abc
End Sub

Private Sub Timer1_Timer()
ProgressBar1.Min = 0
ProgressBar1.Max = 100
ProgressBar1.Value = ProgressBar1.Value + 1
If ProgressBar1.Value = ProgressBar1.Max Then
Timer1.Enabled = False
End If
End Sub

Above code Is working, But when I click copy button, Progressbar1 is not displaying, once the file was copied to another folder. Then only progressbar1 is stating.

Both will not working simultaneously.

And Also Once the file was copied, then progress bar should display 100 %. Now it is not displaying correctly, Still the file is copying, Progress bar is showing 100 %

Can any one help to solve the problem.

Need VB 6 Code Help.

A: 

For a progress bar to function, it either has to be updated inline with a periodic loop, or run in a separate thread.

Autocracy
@Autocracy. New to VB 6, How to use the Periodic loop?
Gopal
Autocracy
+4  A: 

If the standard copy function is blocking the timer from firing then the best thing you can do is write your own copy which reads the source file a few thousand bytes at a time and writes it to the destination file.

Between each read and write operation you need to update your progress bar and (possibly) call DoEvents to make sure it redraws.

Also your timer code makes no sense. It just arbitrarily increases progress every time if fires without reference to how much progress has actually been made. You would be better off passing the progress bar to your copy function so that it can updated as you go.

Something like this would do it:

Private Sub Copy_Click()
  Dim abc As Integer
  Dim line As String
  abc = FreeFile
  Open App.Path & "\DatabasePath.TXT" For Input As #abc
  Input #abc, line
  copyFile line, App.Path & "\" & line, ProgressBar1
  Close #abc
End Sub



Sub copyFile(inFile As String, outFile As String, ByRef pg As ProgressBar)

  Close

  Const chunkSize = 1024
  Dim b() As Byte

  fhIn = FreeFile

  Open inFile For Binary Access Read As #fhIn

  fhOut = FreeFile

  Open outFile For Binary Access Write As #fhOut

  toCopy = LOF(fhIn) 'gets the size of the file
  fileSize = toCopy

  pb.Min = 0
  pb.Max = toCopy




  While toCopy > 0
      If toCopy > chunkSize Then
          ReDim b(1 To chunkSize)
          toCopy = toCopy - chunkSize
      Else
          ReDim b(1 To toCopy)
          toCopy = 0
      End If

      Get #fhIn, , b
      Put #fhOut, , b

      pg.Value = fileSize - toCopy
      DoEvents
  Wend
  Close #fhIn
  Close #fhOut
End Sub
Gareth Simpson
In production don't use #1 and #2, use FreeFile as you are in your original function :)
Gareth Simpson
@Gareth. It showing error on this line like Bad filename - Open inFile For Binary Access Read As #1, Open outFile For Binary Access Write As #2, I changed to #abc, It showing the same error.
Gopal
@Gareth. It showing File already open error.
Gopal
That's the #1/#2 thing biting you.Try it with the code as edited now.
Gareth Simpson
@Gareth. It showing error like "Bad Filename or Number" on this line "Open outFile For Binary Access Write As #fhOut"
Gopal
What are you passing to inFile and outFile ?
Gareth Simpson
Looking at your original code, I assume that the text file you are opening has the full path to the source file you want to copy. You therefore need to trim off the filename part, when you copy it to the local directory. something like line = mid$(line,instrrev(line,"\")+1)
Gareth Simpson
@Gareth, Just paste your code in my project, it showing error like Bad filename.
Gopal
add debug.print inFile and debug.print outFile to the top of the copy function and show me the results?
Gareth Simpson
@Gareth, I modified, that but it again showing error in pb.Max = toCopy, Is not accepting tocopy Value. My progessBar name is pb.
Gopal
I used line = mid$(line,instrrev(line,"\")+1), it showing error in this line "pb.max = tocopy". And also i added debug.print inFile and debug.print outFile in the code, it showing "bad filename or number" error.
Gopal
"Write your own copy function" should be an absolute last resort. Standard wisdom: Don't rewrite things in a language that are doing their job.
Autocracy
@Autocracy - that doesn't match up with the answer you gave to the question :)
Gareth Simpson
@Gopal I can't help any more without knowing what the two debug.prints actually printed. You need to look in the output console. Also it looks like your files are large and overflowing the size that pb.max can take. You'll need to do something like set pb.Max to 100 and calculate the %age of the file copied each time you update the progress bar.
Gareth Simpson
A: 

The copy in old school VB6 is a blocking command. So even DoEvents will give the same result (the file will copy, then the progress bar will show up). If you are copying large files over a slow medium and you need to be able to show progress, then you should create the target file and move over bytes in chunks in a loop, in that loop you could update your progress bar. Sadly for the example given in the OP you won't get what you are looking for since every operation is synchronous.

EDIT: Beaten by the guy above me :)

TheHurt