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

Recommended Answers

All 7 Replies

Member Avatar for Unhnd_Exception

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

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

Sorry i got my error :

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

Thanx again!!

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

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 :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.