VB App won't populate ListBox with files found with GetFiles

Jason Paradis
Jason Paradis used Ask the Experts™
on
Private Sub findPSTButton_Click(sender As Object, e As EventArgs) Handles findPSTButton.Click
        BackupperProgress.Value = 0
        MasterTimer.Start()
        Try
            For Each foundPST As String In My.Computer.FileSystem.GetFiles(
                Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.pst")
                PSTList.Items.Add(foundPST)
            Next
        Catch ex As Exception

        End Try
        BackupperProgress.Value = 99
    End Sub

Open in new window


According to several sources, this should do the following:

a) Start the progress bar
b) Search the current logged in User Profile folder for PST files.
c) Ignore Junction Points (i.e. My Pictures under Documents, etc) and all exceptions
d) Add all of the PST's found to the ListBox

Unfortunately, when the button is clicked nothing appears in the list box. Can anyone tell me what I've done wrong here?

EDIT: By adding a MsgBox(ex.Message) I found out that I'm still getting access denied errors. I've tried putting a Catch line after the For Each one but it blocks the foundPST variable from being used elsewhere in the code. It also says that a Next line needs to be before the Catch. But if I do that I'm back to square one.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi Jason;

Try changing this line of code
PSTList.Items.Add(foundPST)

Open in new window

To this
 PSTList.Items.AddRange(foundPST.ToArray())

Open in new window

NorieAnalyst Assistant
Commented:
Jason

I think you might need to separate things out a bit.

For example, first find all the subdirectories you want to search and then try and search them individually for PST files.

Doing it that way means you'll be able to deal with folders where there are problems with access.

Something like this, which I'm sure can be written more elegantly.
    Private Sub findPSTButton_Click(sender As Object, e As EventArgs) Handles findPSTButton.Click

        Dim dirs As Object

        dirs = My.Computer.FileSystem.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile))

        For Each dir As String In dirs
            Try
                For Each foundPST As String In My.Computer.FileSystem.GetFiles(dir, FileIO.SearchOption.SearchAllSubDirectories, "*.pst")
                    PSTList.Items.Add(foundPST)
                Next foundPST
            Catch ex As Exception

            End Try

        Next dir

    End Sub

Open in new window

Jason ParadisIT Operations Specialist

Author

Commented:
Norie,

Thank you! That worked.

However, it only shows one PST file in the ListBox when I know for a fact there are more in the User Profile folder that's being searched.

Is there a reason only one line appears? It's a pretty big box.

It found the one located in %userprofile%\Downloads\ but not the one located in %userprofile%\My Documents\ unless there is a limitation on how many rows can be in a ListBox.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Jason ParadisIT Operations Specialist

Author

Commented:
Fernando,

I tried your suggestion, however it produced an error:

Overload resolution failed because no accessible AddRange can be called with these arguments.
NorieAnalyst Assistant

Commented:
Jason

Is the missing PST file definitely in the My Documents (or Documents) folder and not a subfolder of that document?

As it is the code I posted is only going to search the first level of subfolders of the user profile folder.

Do you want to search recursively? i.e. search the main folder, then it's subfolders, then the subfolders of the subfolders and so on.
Jason ParadisIT Operations Specialist

Author

Commented:
Norie,

Yes, I wish to search all subdirectories as well. I thought SearchAllSubDirectories did just that.
Analyst Assistant
Commented:
jason

Try this, it worked for me but I'm sure there's a more elegant/better solution.
Imports System.IO
Imports System.Environment

Public Class Form1

    Private Sub findPSTButton_Click(sender As Object, e As EventArgs) Handles findPSTButton.Click

        Dim dirstack As New Stack(Of String)
        Dim res As New List(Of String)

        dirstack.Push(GetFolderPath(SpecialFolder.UserProfile))

        Do While (dirstack.Count > 0)

            Dim dir As String = dirstack.Pop

            Try
                res.AddRange(Directory.GetFiles(dir, "*.pst"))

                For Each subdir As String In Directory.GetDirectories(dir)
                    dirstack.Push(subdir)
                Next subdir

            Catch ex As UnauthorizedAccessException

            End Try

        Loop

        For Each fil As String In res
            PSTList.Items.Add(fil)
        Next fil

        MsgBox(PSTList.Items.Count & " PST files found.")

    End Sub

End Class

Open in new window

Jason ParadisIT Operations Specialist

Author

Commented:
Norie,

That is absolutely fantastic! Thank you very much, works like a charm!

And on top of that, I learned a bunch of new stuff about programming in VB. Win win!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial