Progress bar Help

I have a program that I want to add a progress bar on.
The program searches through a directory strucure and if it finds a directory with anyfiles that have a  .dgn extension it outputs the data to my DataSource and then keeps on going.
What I want the Progress bar to do is to show how EACH directory is being prosessed.
Here is my main code that does all the Directory Searching

Thanks

    Public Sub ScanDir()
        Dim myPath As String = "\\Engineering\Projects\Working"
        Dim myDirectory As System.IO.Directory
        Dim mySubDirectory As String
        Dim myBridgeDirectory As String
        Dim myBridgePosition As Integer
        Dim myDesignFiles As String
        Dim FoundIt As Boolean
        Dim myDesignUnit As String
        Dim count As Integer


        ds = New dsBridgeDGNFiles
        Dim row As dsBridgeDGNFiles.tblFilesRow

        If optBridge.Checked = True Then
            myDesignUnit = "Bridge"
        ElseIf optBrgMaint.Checked = True Then
            myDesignUnit = "Bridge Maintenance"
        ElseIf optSignLight.Checked = True Then
            myDesignUnit = "SL Structures"
        ElseIf optLandScape.Checked = True Then
            myDesignUnit = "Landscape"
        End If



        Dim fileCount As Integer = 0
        Dim oDir As New System.IO.DirectoryInfo(myPath & "\" & myBridgeDirectory)
        'RecursiveCount(oDir, fileCount)


        If myDirectory.Exists(myPath) Then
            'Debug.WriteLine("Sub Directory List:")
            For Each mySubDirectory In myDirectory.GetDirectories(myPath)
                For Each myBridgeDirectory In myDirectory.GetDirectories(mySubDirectory)
                    'This routine checks to see if their are any files with a .dgn extension if so then process
                    If InStr(1, myBridgeDirectory, myDesignUnit) > 0 Then
                        myDesignFiles = Dir(myBridgeDirectory & "\*.dgn", vbDirectory)   ' Retrieve the first entry.
                        myDesignFiles = Dir()
                        'FoundIt = False

                        'If InStr(myDesignFiles, ".dgn") > 0 Then
                        '    Debug.WriteLine(myBridgeDirectory)
                        'End If

                        Do While myDesignFiles <> ""    ' Start the loop.
                            If InStr(myDesignFiles, ".dgn") > 0 Then
                                'Debug.WriteLine(myDesignFiles)
                                row = ds.tblFiles.NewtblFilesRow
                                row.DGNFiles = myDesignFiles
                                row.Directory = myBridgeDirectory
                                ds.tblFiles.Rows.Add(row)
                                'FoundIt = True
                                'Exit Do
                            End If
                            myDesignFiles = Dir()
                        Loop
                        'If FoundIt = True Then
                        'cboFiles.Items.Add(myBridgeDirectory)
                        'End If
                    End If

                Next
            Next
        Else
            MsgBox("The Projects Directory was not found Contact Stephen Herrick")
            Exit Sub
        End If

    End Sub
sherrick123Asked:
Who is Participating?
 
Bob LearnedCommented:
Borrowing from S-Twilley's code to highlight:

  Public Sub ScanDir()

    Dim myPath As String
    Dim thisFile, theseFiles() As FileInfo

    Dim myDesignUnit As String

    Dim topDir As DirectoryInfo = New DirectoryInfo("\\Engineering\Projects\Working\")

    ds = New dsBridgeDGNFiles
   
    If optBridge.Checked = True Then
      myDesignUnit = "Bridge"
    ElseIf optBrgMaint.Checked = True Then
      myDesignUnit = "Bridge Maintenance"
    ElseIf optSignLight.Checked = True Then
      myDesignUnit = "SL Structures"
    ElseIf optLandScape.Checked = True Then
      myDesignUnit = "Landscape"
    End If

    If topDir.Exists Then

      Dim listDirectories As New ArrayList

      For Each subDir  As DirectoryInfo In topDir.GetDirectories()
        For Each bottomDir As DirectoryInfo In subDir.GetDirectories
          listDirectories.Add(bottomDir)
        Next
      Next

      progDirectory.Value = 0
      progDirectory.Maximum = listDirectories.Count - 1

      For Each directoryCurrent As DirectoryInfo In listDirectories

        progDirectory.Value += 1

        theseFiles = directoryCurrent.GetFiles("*.dgn")

        progFile.Value = 0
        progFile.Maximum = theseFiles.Length - 1

        For Each thisFile In theseFiles
          progFile.Value += 1

          Dim row As dsBridgeDGNFiles.tblFilesRow = ds.tblFiles.NewtblFilesRow

          row.DGNFiles = thisFile.FullName  ' if you just want the file name use thisFile.Name
          row.Directory = bottomDir.FullName ' if you just want the folder name use bottomDir.Name

          ds.tblFiles.Rows.Add(row)

        Next

      Next

    Else
      MsgBox("The Projects Directory was not found Contact Stephen Herrick")
    End If

  End Sub


Bob
0
 
S-TwilleyCommented:
Are you intentionally only going down two levels in the directory structure, or would you prefer to go down all levels?

When you say about the process through each directory?  do you mean to say that if there were 246 directories, show the progress as it goes through each of those directories.... or for say directory X.. it has 10 dgn files, the progress through each of those files ?
0
 
sherrick123Author Commented:
Only going two down.  That is where the files are going to be
yes I want it to show on each dgn file then start over when it gets to the next directory

ALTHOUGH it would be nice to have two progress bars.
One for the directories and one for the files.

hummmm

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
S-TwilleyCommented:
okies... one option is to store the last count of directories... and update it after each scan... this can lead to slight differences.

other way is to do a quick count of directories... then use that in the file scan.


HEre's code for the 2nd implementation:

progFile and progDirectory are progress bars
this code doesn't have the code to do the dataset stuff... just the file processing

Imports System.IO

==================================

    Public Sub ScanDir()
        Dim myPath As String
        Dim topDir, subDir, bottomDir As DirectoryInfo
        Dim thisFile, theseFiles() As FileInfo
        Dim dirCount As Integer = 0

        topDir = New DirectoryInfo("\\Engineering\Projects\Working\")

        For Each subDir In topDir.GetDirectories()
            For Each bottomDir In subDir.GetDirectories
                dirCount += subDir.GetDirectories.Length
            Next
        Next

        progDirectory.Value = 0
        progDirectory.Max = dirCount

        For Each subDir In topDir.GetDirectories()
            For Each bottomDir In subDir.GetDirectories
                If progDirectory.Value < progDirectory.Max Then progDirectory.Value += 1

                theseFiles = bottomDir.GetFiles("*.dgn")

                progFile.Value = 0
                progFile.Max = theseFiles.Length

                For Each thisFile In theseFiles
                    If progFile.Value < progFile.Max Then progFile.Value += 1

                    ' YOUR DATASET CODE
                Next

            Next
        Next

    End Sub
0
 
sherrick123Author Commented:
I'm sorry but I can't figure out how to impliment that in my code.

When do I do the count of sub directories and when do I put in the file count??

        Dim myPath As String = "\\Engineering\Projects\Working"
        Dim myDirectory As System.IO.Directory
        Dim mySubDirectory As String
        Dim myBridgeDirectory As String
        Dim myBridgePosition As Integer
        Dim myDesignFiles As String
        Dim FoundIt As Boolean
        Dim myDesignUnit As String
        Dim count As Integer


        ds = New dsBridgeDGNFiles
        Dim row As dsBridgeDGNFiles.tblFilesRow

        If optBridge.Checked = True Then
            myDesignUnit = "Bridge"
        ElseIf optBrgMaint.Checked = True Then
            myDesignUnit = "Bridge Maintenance"
        ElseIf optSignLight.Checked = True Then
            myDesignUnit = "SL Structures"
        ElseIf optLandScape.Checked = True Then
            myDesignUnit = "Landscape"
        End If



        Dim fileCount As Integer = 0
        Dim oDir As New System.IO.DirectoryInfo(myPath & "\" & myBridgeDirectory)
        'RecursiveCount(oDir, fileCount)


        If myDirectory.Exists(myPath) Then
            'Debug.WriteLine("Sub Directory List:")
            For Each mySubDirectory In myDirectory.GetDirectories(myPath)
                For Each myBridgeDirectory In myDirectory.GetDirectories(mySubDirectory)
   'This routine checks to see if their are any files with a .dgn extension in a particular directory if so then process
                    If Microsoft.VisualBasic.Right(myBridgeDirectory, Len(myDesignUnit)) = myDesignUnit Then
                        myDesignFiles = Dir(myBridgeDirectory & "\*.dgn", vbDirectory)   ' Retrieve the first entry.
                        myDesignFiles = Dir()
                        Do While myDesignFiles <> ""    ' Start the loop.
                            If InStr(myDesignFiles, ".dgn") > 0 Then
                                'Debug.WriteLine(myDesignFiles)
                                row = ds.tblFiles.NewtblFilesRow
                                row.DGNFiles = myDesignFiles
                                row.Directory = myBridgeDirectory
                                ds.tblFiles.Rows.Add(row)
                            End If
                            myDesignFiles = Dir()
                        Loop
                    End If
                Next
            Next
        Else
            MsgBox("The Projects Directory was not found Contact Stephen Herrick")
            Exit Sub
        End If

    End Sub
0
 
S-TwilleyCommented:
Ok.. i'll have a go.

======================

    Public Sub ScanDir()
        Dim myPath As String
        Dim topDir, subDir, bottomDir As DirectoryInfo
        Dim thisFile, theseFiles() As FileInfo
        Dim dirCount As Integer = 0

        Dim myDesignUnit As String

        topDir = New DirectoryInfo("\\Engineering\Projects\Working\")

        ds = New dsBridgeDGNFiles
        Dim row As dsBridgeDGNFiles.tblFilesRow

        If optBridge.Checked = True Then
            myDesignUnit = "Bridge"
        ElseIf optBrgMaint.Checked = True Then
            myDesignUnit = "Bridge Maintenance"
        ElseIf optSignLight.Checked = True Then
            myDesignUnit = "SL Structures"
        ElseIf optLandScape.Checked = True Then
            myDesignUnit = "Landscape"
        End If

        If topDir.Exists Then
            For Each subDir In topDir.GetDirectories()
                For Each bottomDir In subDir.GetDirectories
                    dirCount += subDir.GetDirectories.Length
                Next
            Next

            progDirectory.Value = 0
            progDirectory.Maximum = dirCount

            For Each subDir In topDir.GetDirectories()
                For Each bottomDir In subDir.GetDirectories
                    If progDirectory.Value < progDirectory.Maximum Then progDirectory.Value += 1

                    theseFiles = bottomDir.GetFiles("*.dgn")

                    progFile.Value = 0
                    progFile.Maximum = theseFiles.Length

                    For Each thisFile In theseFiles
                        If progFile.Value < progFile.Maximum Then progFile.Value += 1

                        row = ds.tblFiles.NewtblFilesRow
                        row.DGNFiles = thisFile.FullName  ' if you just want the file name use thisFile.Name
                        row.Directory = bottomDir.FullName ' if you just want the folder name use bottomDir.Name
                        ds.tblFiles.Rows.Add(row)
                    Next

                Next
            Next
        Else
            MsgBox("The Projects Directory was not found Contact Stephen Herrick")
        End If


    End Sub
0
 
sherrick123Author Commented:
Getting close.
A couple of things,
1)
Here is an example of how the directory strucute is set up
\\Engineering\Projects\Working\
                                -->>K014701
                                     -->Bridge
                                     -->Bridge Maintenance
                                     -->Landscape
                                     -->SL Structures
So from this code
        If optBridge.Checked = True Then
            myDesignUnit = "Bridge"
        ElseIf optBrgMaint.Checked = True Then
            myDesignUnit = "Bridge Maintenance"
        ElseIf optSignLight.Checked = True Then
            myDesignUnit = "SL Structures"
        ElseIf optLandScape.Checked = True Then
            myDesignUnit = "Landscape"
        End If

Is how the program know WHAT sub directory to scan for the dgn files.  Your code scan through every sub directory that has a dgn file in it.

2)
The file progress bar works great the Directory progress bar show one click and thats all


Thanks

0
 
S-TwilleyCommented:
Ok.. i think i see what you're trying to do now...  so i changed the code slightly (hopefully it should still work)

===============================

   Public Sub ScanDir()
        Dim myPath As String
        Dim topDir, subDir, bottomDir As DirectoryInfo
        Dim thisFile, theseFiles() As FileInfo
        Dim dirCount As Integer = 0

        Dim myDesignUnit As String

        topDir = New DirectoryInfo("\\Engineering\Projects\Working\")

        ds = New dsBridgeDGNFiles
        Dim row As dsBridgeDGNFiles.tblFilesRow

        Dim dirList As New ArrayList()

        If optBridge.Checked = True Then
            myDesignUnit = "Bridge"
        ElseIf optBrgMaint.Checked = True Then
            myDesignUnit = "Bridge Maintenance"
        ElseIf optSignLight.Checked = True Then
            myDesignUnit = "SL Structures"
        ElseIf optLandScape.Checked = True Then
            myDesignUnit = "Landscape"
        End If

        Dim thisFolder As String

        If topDir.Exists Then
            For Each subDir In topDir.GetDirectories()
                thisFolder = subDir.Fullname & myDesignUnit & "\"  ' this might need to be subDir.Fullname & "\" & myDesignUnit & "\"
                If Directory.Exists(thisFolder) Then
                    dirList.Add(thisFolder)
                End If
            Next

            progDirectory.Value = 0
            progDirectory.Maximum = dirList.Count

            For Each thisFolder in dirList
                 If progDirectory.Value < progDirectory.Maximum Then progDirectory.Value += 1
                    bottomDir = New DirectoryInfo(thisFolder)
                    If bottomDir.Exists Then
                        theseFiles = bottomDir.GetFiles("*.dgn")

                        progFile.Value = 0
                        progFile.Maximum = theseFiles.Length

                        For Each thisFile In theseFiles
                            If progFile.Value < progFile.Maximum Then progFile.Value += 1

                            row = ds.tblFiles.NewtblFilesRow
                            row.DGNFiles = thisFile.FullName  ' if you just want the file name use thisFile.Name
                            row.Directory = bottomDir.FullName ' if you just want the folder name use bottomDir.Name
                            ds.tblFiles.Rows.Add(row)
                        Next
                    End If
            Next
        Else
            MsgBox("The Projects Directory was not found Contact Stephen Herrick")
        End If

    End Sub
0
 
Bob LearnedCommented:
My only question is that GetDirectories is being called twice within double nested For...Next loops.  My suggestion would be to build a flat list of directories into an ArrayList the first time, and then the progress bar would be simpler to implement.

Bob
0
 
sherrick123Author Commented:
I nested it twice because of the way I need to recurse through the directory structure?

Here is the Example of the directory structure
\\Engineering\Projects\Working\
      K021901\     <---Project Directory
      Bridge\
      Brow\
      Coordinating\
      District\
      Enviroment\
      Geology\
      Landscape\
      Road\
      SL Structures\
      Survey\
      Traffic\

I only want to search for a ONE specific sub directory down from my project directory. per my option group
And when I search that directory I test to see if their are any files with a .dgn extension.  If it finds one then
I want to add ALL of them to my Data Source to eventually create a report.

Oh yea by the way.  This only my fifth vb .net project....

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.