Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2010-08-25
8
507 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 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

0
 
LVL 11

Accepted Solution

by:
ladarling earned 500 total points
ID: 33525516
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 75

Expert Comment

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

I think he mean "eliminate" the files from the list, not delete the image file entirely.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 2

Expert Comment

by:larkvale
ID: 33525549

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
 
LVL 17

Expert Comment

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

Now newFileArray will hold the files without "thumb_".

0
 
LVL 11

Expert Comment

by:ladarling
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
                GoodFiles.Add(f)
            End If
        Next
 FileArray = GoodFiles.ToArray

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33525821
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
ID: 33537198
THANK YOU!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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