Solved

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

Posted on 2010-08-25
8
504 Views
Last Modified: 2012-05-10
I am reading the contents of a Directory using VB.net 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:
thumb_somefile.jpg
somefile.jpg
thumb_anotherfile.jpg
anotherfile.jpg

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!

thanks!
0
Comment
Question by:ronayers
8 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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

0
 
LVL 11

Accepted Solution

by:
ladarling earned 500 total points
Comment Utility
Try something like...
 

For Each f As IO.FileInfo In FileArray

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

                f.Delete()

            End If

 Next

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
@ladarling

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

Expert Comment

by:larkvale
Comment Utility

Try something like this ...

Dim RandomFileName as String = ""

Do

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

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

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


0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
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
        Next

Now newFileArray will hold the files without "thumb_".

0
 
LVL 11

Expert Comment

by:ladarling
Comment Utility
@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

                GoodFiles.Add(f)

            End If

        Next

 FileArray = GoodFiles.ToArray

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Love all the ways to do it...here'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

0
 

Author Closing Comment

by:ronayers
Comment Utility
THANK YOU!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

7 Experts available now in Live!

Get 1:1 Help Now