View Single Post
 
Old 09-20-2014, 11:24 AM
dmratcliffe dmratcliffe is offline
Newcomer
 
Join Date: Sep 2014
Posts: 2
Default Finding and listing all images on a computer / folder structure

Hey, so I whipped up to code snippets here,
both are meant to go through every folder and find every image,
I will go into more detail for each of them:

This is my original finder before I knew much about arrays:
Code:
 For Each foundFile As String In My.Computer.FileSystem.GetFiles(Application.StartupPath)
            Try
                Dim tryImage As System.Drawing.Image = System.Drawing.Image.FromFile(foundFile)
                pictureListBox.Items.Add(foundFile)
            Catch ex As Exception

            End Try
        Next

        For Each Dir As String In My.Computer.FileSystem.GetDirectories(Application.StartupPath, FileIO.SearchOption.SearchAllSubDirectories)
            For Each foundFile As String In My.Computer.FileSystem.GetFiles(Dir)
                Try
                    Dim tryImage As System.Drawing.Image = System.Drawing.Image.FromFile(foundFile)
                    pictureListBox.Items.Add(foundFile)
                Catch ex As Exception

                End Try
            Next
        Next
The problem is, 1. its slow as balls, 2. It hates protected folders because of those fors. It looks at each file and tests if it is an image then adds it or moves on.

Then I went off and learned about arrays and ranges and came up with this:
Code:
 Private Function listFiles(ByVal appStart As String) As List(Of String)
        Dim fList As New List(Of String)
        Dim fldrLis As New List(Of String)
        Dim t As New List(Of String)

        Dim d As New IO.DirectoryInfo(appStart)
        If Not d.Exists Then
            MessageBox.Show("Not a real folder errr")
            Return Nothing
        End If
        Try
            fList.AddRange(Directory.GetFiles(appStart))
            fldrLis.AddRange(Directory.GetDirectories(appStart))
        Catch ex As Exception
            MessageBox.Show("Re run in admin mode")
            Return Nothing
        End Try
        'If there are directories in this folder process them
        For Each f As String In fldrLis
            t = listFiles(f)
            If t IsNot Nothing Then fList.AddRange(tmp)
        Next
        Return fList
    End Function
then after that finishes, I go through that and check each item for picture (I wrote this code in a ****ed off mode and haven't fixed it but it works):
Code:
 Try
            For n = 0 To ListBox1.Items.Count - 1
                ListBox1.SelectedIndex = n
                Dim var As String = ListBox1.SelectedItem.ToString
                Try
                    Dim tryImage As System.Drawing.Image = System.Drawing.Image.FromFile(var)
                    amountLabel.Text = pictureListBox.Items.Count
                    pictureListBox.Items.Add(var)
                Catch ex As Exception
                End Try
            Next
        Catch ex As Exception
            MsgBox("whoopse!")
        End Try
All this is still better then the original.
Is there a way to implement the second part into the first? Check it before adding it to the array?
I am still new to arrays but that code does work fine...
Thanks in advance to anyone who can help.
Reply With Quote