ronayers
asked on
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:\somed ir")
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!
Dim DirectoryLocation As New IO.DirectoryInfo("c:\somed
Dim FileArray As IO.FileInfo() = DirectoryLocation.GetFiles
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
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@ladarling
I think he mean "eliminate" the files from the list, not delete the image file entirely.
I think he mean "eliminate" the files from the list, not delete the image file entirely.
Try something like this ...
Dim RandomFileName as String = ""
Do
RandomFileName = FileArray(RandString.[Next
Loop While(RandomFileName.ToLow
If RandomFileName <> "" Then
' use the random file name here
End If
Try this:
Dim DirectoryLocation As New IO.DirectoryInfo("c:\somed ir")
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("thu mb_", System.StringComparison.Cu rrentCultu reIgnoreCa se) = False Then
count += 1
Array.Resize(newFileArray, count)
newFileArray(count - 1) = oInfo
End If
Next
Now newFileArray will hold the files without "thumb_".
Dim DirectoryLocation As New IO.DirectoryInfo("c:\somed
Dim FileArray As IO.FileInfo() = DirectoryLocation.GetFiles
Dim newFileArray As IO.FileInfo() = Nothing
Dim count As Integer
For Each oInfo As FileInfo In FileArray
If oInfo.Name.StartsWith("thu
count += 1
Array.Resize(newFileArray,
newFileArray(count - 1) = oInfo
End If
Next
Now newFileArray will hold the files without "thumb_".
@kaufmed..... Oh, ok... gotcha...
Then, a non-Linq way would be:
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
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
ASKER
THANK YOU!
Open in new window