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

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!

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

Try something like...

1:
2:
3:
4:
5:
For Each f As IO.FileInfo In FileArray
            If f.Name.StartsWith("thumb") Then
                f.Delete()
            End If
 Next
Random Solutions  
 
programming4us programming4us