954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

For loop error

i have the following code for getting the number of files on a drive:

Dim num As Integer = 0
        Dim drive As String = "C:"

For Each File In System.IO.Directory.GetFiles(drive,"*",System.IO.SearchOption.AllDirectories)
            Try
                num += 1
                Label5.Text = String.Format("Files found: {0}", num)
                If num > 50 Then
                    RaiseEvent start()
                End If
                ProgressBar1.Maximum = num
            Catch ex As Exception
                Continue For              //Continue Loop
            End Try
        Next


I want to skip the files which i dont have access to. But my error is occuring before the try hence i am not able to continue the loop.
Please advice on how should i continue the for loop disregarding the error.

Regards
BattleX

battlex2010
Newbie Poster
15 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 


Use this recursive method. It allows you to trap the exception and continue.

I put together another version that tested the access rights first but took 60% longer than just catching the exeception.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     
    Dim path As String = "C:\"
    Dim TotalFiles As Integer = GetDirectoryFileCount(path)

End Sub

Private Function GetDirectoryFileCount(ByVal directoryPath As String) As Integer

    Dim TotalFiles As Integer

    Try
        TotalFiles += System.IO.Directory.GetFiles(directoryPath).Count

        For Each Directory As String In System.IO.Directory.GetDirectories(directoryPath)
            TotalFiles += GetDirectoryFileCount(Directory)
        Next

    Catch ex As Exception

    End Try

    Return TotalFiles

End Function
Unhnd_Exception
Posting Pro
570 posts since Nov 2010
Reputation Points: 249
Solved Threads: 201
 

Thank you for your prompt response

When i tried to display the current status of the files searched by using a label it keeps going to zero and flicks to another number and again zero. But however in the end it is giving me the correct value.

Dim TotalFiles As Integer
        Label5.Text = 0
        Try
            TotalFiles += System.IO.Directory.GetFiles(directoryPath).Count


            For Each Directory As String In System.IO.Directory.GetDirectories(directoryPath)
                TotalFiles += GetDirectoryFileCount(Directory)
                Label5.Text = TotalFiles                              //Is this correct??
            Next

        Catch ex As Exception

        End Try

        Return TotalFiles

    End Function


Is the label5.text = TotalFiles at correct position? I need to get the progress of the files counted..

Regards
BattleX

battlex2010
Newbie Poster
15 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Sorry i got my error :

i have put label5.text = 0 in the start of the function ooopsss

Thanx again!!

battlex2010
Newbie Poster
15 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

I also want to get the names of the files..

battlex2010
Newbie Poster
15 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Check out this thread .

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

The method in the link skips a complete top director if access is denied.
For example if my C drive contains the following folders:

Program Files
Users
etc
if access is denied to any file in Program files it completely skips program files and moves on to Users

Thanks any way learned about the UnauthorizedAccessException from it :-)

battlex2010
Newbie Poster
15 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 


Heres the same function in another thread that displays the file names. http://www.daniweb.com/software-development/vbnet/threads/352686

Unhnd_Exception
Posting Pro
570 posts since Nov 2010
Reputation Points: 249
Solved Threads: 201
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: