Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET File I/O and Registry (http://www.xtremevbtalk.com/-net-file-i-o-and-registry/)
-   -   Finding and listing all images on a computer / folder structure (http://www.xtremevbtalk.com/-net-file-i-o-and-registry/327279-finding-listing-images-computer-folder-structure.html)

dmratcliffe 09-20-2014 11:24 AM

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.

hDC_0 09-21-2014 02:01 PM

HD searching..
 
Quote:

Originally Posted by dmratcliffe
Is there a way to implement the second part into the first? Check it before adding it to the array?

Did you possibly mean (hint) "Check it while adding it to the array"?
..and yes, I definitely commend you for learning about working with arrays;
however another topic to explore might be that of recursion.
It is a very powerful programming technique designed to do a lot of work while only using a few lines of code.

I did an advanced search of XVBT's VB.Net File I/O sub forum, using the terms "recursion" and "recursive"
--it found dozens of threads, including a few that use background threading (1, 2),
which allows stopping the search if it starts to run too long on full 8/10 terabyte hard drives.

dmratcliffe 09-26-2014 03:53 PM

Quote:

Originally Posted by hDC_0 (Post 1406927)
Did you possibly mean (hint) "Check it while adding it to the array"?

Oh. Derp. Why didn't I think of that?
So instead of just adding the .getFiles I should check the list of 'em first.
I really like that idea of 2 threads for the directory and for the files though.

Also, I had already placed the getfiles inside of a new thread to run it (such as the "1" you directed me to. However I have a lot of trouble stopping the thing while running, but I will look into that later.


All times are GMT -6. The time now is 05:49 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.