Filter GetFiles() by file name and select one random file.

Posted on 2010-08-25
Last Modified: 2012-05-10
I am reading the contents of a Directory using and the FileInfo() and GetFiles commmands.

Dim DirectoryLocation As New IO.DirectoryInfo("c:\somedir")
Dim FileArray As IO.FileInfo() = DirectoryLocation.GetFiles("*.jpg")
Dim FilesInFolder As IO.FileInfo

This code will return several results:

I want to eliminate the files starting with "thumb_" from the list  and then select one random file from those results.

I am trying to select a random background image for a webpage from a folder on the server.

I have spent all day on this so far. this code will select a random item from the GetFiles() Array
FileArray(RandString.[Next](0, FileArray.Length)) but before i do that i need to remove the files that contain "thumb" in the file name.

I have tried the FILTER command on the GetFiles() array but i throws an error due to the array type.

any help you can throw my way would be greatly appreciated!

Question by:ronayers
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33525502
If you're using .NET 3.x or above, you could throw in some Linq:
Dim DirectoryLocation As New IO.DirectoryInfo("c:\somedir")
Dim FileArray As IO.FileInfo() = DirectoryLocation.GetFiles("*.jpg")
Dim FilesInFolder As IO.FileInfo

FileArray = (From item As IO.FileInfo In FileArray _
            Where Not item.Name.StartsWith("thumb_") _
            Select item).ToArray()

Open in new window

LVL 11

Accepted Solution

ladarling earned 500 total points
ID: 33525516
Try something like...

For Each f As IO.FileInfo In FileArray

            If f.Name.StartsWith("thumb") Then


            End If


Open in new window

LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33525530

I think he mean "eliminate" the files from the list, not delete the image file entirely.

Expert Comment

ID: 33525549

Try something like this ...

Dim RandomFileName as String = ""


     RandomFileName = FileArray(RandString.[Next](0, FileArray.Length))

Loop While(RandomFileName.ToLower().StartsWith("thumb_"))

If RandomFileName <> "" Then
     ' use the random file name here
End If

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

LVL 17

Expert Comment

ID: 33525570
Try this:

        Dim DirectoryLocation As New IO.DirectoryInfo("c:\somedir")
        Dim FileArray As IO.FileInfo() = DirectoryLocation.GetFiles("*.jpg")
        Dim newFileArray As IO.FileInfo() = Nothing
        Dim count As Integer
        For Each oInfo As FileInfo In FileArray
            If oInfo.Name.StartsWith("thumb_", System.StringComparison.CurrentCultureIgnoreCase) = False Then
                count += 1
                Array.Resize(newFileArray, count)
                newFileArray(count - 1) = oInfo
            End If

Now newFileArray will hold the files without "thumb_".

LVL 11

Expert Comment

ID: 33525580
@kaufmed..... Oh, ok... gotcha...
Then, a non-Linq way would be:

    Dim FileArray As IO.FileInfo()

        Dim GoodFiles As New List(Of IO.FileInfo)

        For Each f As IO.FileInfo In FileArray

            If Not f.Name.StartsWith("thumb") Then


            End If


 FileArray = GoodFiles.ToArray

Open in new window

LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33525821
Love all the ways to do's another one:
        Dim Files As New List(Of IO.FileInfo)

        Files.AddRange(New IO.DirectoryInfo("c:\somedir").GetFiles("*.jpg"))

        Files.RemoveAll(Function(fi As IO.FileInfo) fi.Name.ToLower.StartsWith("thumb_"))

        Dim RandomFile As String = Files(New Random().Next(0, Files.Count)).FullName

Open in new window


Author Closing Comment

ID: 33537198

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now