Solved

Progress Bar

Posted on 2008-06-12
3
270 Views
Last Modified: 2011-09-20
Hello all,
I was create a program that copy files to a destination folder. I need to add a progress bar in form that can see when copy files complete and one label that can show the % of the progress done.

From a commandbutton I call the attach code (sub)
Call FindFiles(txtSourceFolder.Text, "_a.txt", txtDestFolder.Text, "_b.txt", CDate(DtpFrom.Value), CDate(DtpTo.Value), txtCustomer.Text)

Can be help me anyone. Thanks
Sub FindFiles(strFolder As String, strFilePatternA As String, strDestFolder As String, _
           strFilePatternB As String, dtStart As Date, dtEnd As Date, strSearchText As String)
    Dim strSourceFile As String
    Dim strDestFile As String
    Dim strSubFolder As String
    Dim strFolders() As String
    Dim strText As String
    Dim f As Integer
    Dim i As Integer
    Dim strFile As String
    
    strSourceFile = Dir$(strFolder & "\*" & strFilePatternA)
    Do Until strSourceFile = ""
            DoEvents
            If FileDateTime(strFolder & "\" & strSourceFile) >= dtStart And _
                FileDateTime(strFolder & "\" & strSourceFile) <= dtEnd Then
                f = FreeFile
                Open strFolder & "\" & strSourceFile For Binary As #f
                strText = String(LOF(f), " ")
                Get #f, , strText
                If InStr(strText, strSearchText) > 0 Then
                    Close #f
                    FileCopy strFolder & "\" & strSourceFile, strDestFolder & "\" & strSourceFile
                    strDestFile = Left$(strSourceFile, Len(strSourceFile) - Len(strFilePatternA)) _
                                                 & strFilePatternB
                    FileCopy strFolder & "\" & strDestFile, strDestFolder & "\" & strDestFile
                End If
            End If
        strSourceFile = Dir$()
    Loop
    
    i = -1
    strSubFolder = Dir$(strFolder & "\*", vbDirectory)
    Do Until strSubFolder = ""
        If Left$(strSubFolder, 1) <> "." Then
            If (GetAttr(strFolder & "\" & strSubFolder) And vbDirectory) = vbDirectory Then
                i = i + 1
                ReDim Preserve strFolders(i)
                strFolders(i) = strFolder & "\" & strSubFolder
            End If
        End If
        strSubFolder = Dir$()
    Loop
    
    If i > -1 Then
    For i = 0 To UBound(strFolders)
    FindFiles strFolders(i), strFilePatternA, strDestFolder, strFilePatternB, dtStart, _
                       dtEnd, strSearchText
    Next i
    End If
End Sub

Open in new window

0
Comment
Question by:fkourou
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 21771991
You can use the windows API to setup a callback routine that can be used to setup a progress

FileCopyEx
http://msdn.microsoft.com/en-us/library/aa363852(VS.85).aspx
CopyProgressRoutine Callback Function
http://msdn.microsoft.com/en-us/library/aa363854(VS.85).aspx
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 21772551
You need to Find how much work there is to be done so that the progress bar shows the what proportion has been done at any moment.

There is a possibility that finding that information will take as much time as the actual work, so this doesn't open the possible target files, so the count will include files which aren't actually copied.
You need to include a line in the original code:

            If FileDateTime(strFolder & "\" & strSourceFile) >= dtStart And _
                FileDateTime(strFolder & "\" & strSourceFile) <= dtEnd Then
                ProgressBar1.Value = ProgressBar1.Value + 1 '<--extra line
                f = FreeFile
 


The Function in the snippet is a modification of the FindFiles Sub. It counts all the candidate files without opening them.

Call the two procedures like this:


ProgressBar1.Max = CountFiles( "C:\MyFolder", "_a.txt", "C:\Backup", "_b.text", CDate("20/04/2008"), CDate("26/04/2008"), "3000367648")

ProgressBar1.Value = 0
  FindFiles "C:\MyFolder", "_a.txt", "C:\Backup", "_b.text", CDate("20/04/2008"), CDate("26/04/2008"), "3000367648"
Function CountFiles(strFolder As String, strFilePatternA As String, strDestFolder As String, _
        strFilePatternB As String, dtStart As Date, dtEnd As Date, strSearchText As String) As Integer
    Dim strSourceFile As String
    Dim strDestFile As String
    Dim strSubFolder As String
    Dim strFolders() As String
    Dim strText As String
    Dim f As Integer
    Dim i As Integer
    Dim strFile As String
    
    strSourceFile = Dir$(strFolder & "\*" & strFilePatternA)
    Do Until strSourceFile = ""
            DoEvents
            If FileDateTime(strFolder & "\" & strSourceFile) >= dtStart And _
                FileDateTime(strFolder & "\" & strSourceFile) <= dtEnd Then
                CountFiles = CountFiles + 1
            End If
        strSourceFile = Dir$()
    Loop
    
    i = -1
    strSubFolder = Dir$(strFolder & "\*", vbDirectory)
    Do Until strSubFolder = ""
        If Left$(strSubFolder, 1) <> "." Then
            If (GetAttr(strFolder & "\" & strSubFolder) And vbDirectory) = vbDirectory Then
                i = i + 1
                ReDim Preserve strFolders(i)
                strFolders(i) = strFolder & "\" & strSubFolder
            End If
        End If
        strSubFolder = Dir$()
    Loop
    
    If i > -1 Then
        For i = 0 To UBound(strFolders)
            CountFiles = CountFiles + CountFiles(strFolders(i), strFilePatternA, strDestFolder, strFilePatternB, dtStart, _
                           dtEnd, strSearchText)
        Next i
    End If
End Function

Open in new window

0
 

Author Closing Comment

by:fkourou
ID: 31466627
I dont khow why It is not running.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month4 days, 2 hours left to enroll

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question