Filesystem.GetFiles using more than one wildcard criteria?

thebatfink

Well-known member
Joined
Mar 29, 2012
Messages
47
Programming Experience
Beginner
Ok, so here's what I am trying to achieve -

1) Get a root folder using a browse prompt
2) Recursively scan the folder and find all excel files ("*.xl*")
3) Count the number of files so I can update my forms text box to display number files found to user
4) Create a listbox containing the filenames to display to the user
5) Put the filenames into an array for processing later in the application

I'm achieving that using the code below. What I want to do now is change requirement number 2 above to.. Include filenames where filename like "*.xl*" = TRUE AND filename like "*.xlt*" = FALSE.
I don't think this is possible with the GetFiles method? But is there a logical workaround to achieve this that any one can think of as its frying my brain!

VB.NET:
'Get root folder
Dim SelectedFolderName as string = FolderBrowserDialog1.SelectedPath

'Count number of files which match wildcard *.xl*
Dim counter = My.Computer.FileSystem.GetFiles(SelectedFolderName, FileIO.SearchOption.SearchAllSubDirectories, "*.xl*")

'Display number of found files on form
Form1.TextBox1.Text = CStr(counter.Count)

Dim ArrayPosition As Integer = 0

'Set array size to total number of files found
ReDim SelectedFileList(CStr(counter.Count) - 1)

For Each FoundFile As String In My.Computer.FileSystem.GetFiles(SelectedFolderName, FileIO.SearchOption.SearchAllSubDirectories, "*.xl*")

    'For each file add filename/path to listbox for display to user and add the filename/path to the array for use later
    Form3.ListBox1.Items.Add(FoundFile)
    SelectedFileList(ArrayPosition) = FoundFile
    ArrayPosition = ArrayPosition + 1

Next

Thanks for any help!
 
Either call GetFiles multiple times with each filter, or GetFiles once for all files and do manual filtering.
 
Hi, thanks for the reply. I'm not entirely sure what you mean by calling it multiple times with each filter. How would this help me?

I thought about manual filtering, but to initialise the array, I need to know how large it should be by using GetFiles. But then I need to use GetFiles again to loop through and add the files to the array. How could I do this with just the one call. I didn't just go this route as it felt inefficient and normally when I feel that, it's ussually because it is, and my lack of knowledge is preventing me from seeing a much simpler solution.

I guess I could do this - but it feels like more work than is nessicary just to determine how large my array needs to be.

VB.NET:
'Get root folder
Dim SelectedFolderName as string = FolderBrowserDialog1.SelectedPath
Dim TotalFileNo as Integer = 0
For Each FoundFile As String In My.Computer.FileSystem.GetFiles(SelectedFolderName, FileIO.SearchOption.SearchAllSubDirectories, "*.xl*")
    If UCase(FoundFile) IsNot Like "*.XLT*" Then
        TotalFileNo = TotalFileNo + 1
    End If
Next
Form1.TextBox1.Text = TotalFileNo
Dim ArrayPosition As Integer = 0
ReDim SelectedFileList(TotalFileNo - 1)
For Each FoundFile As String In My.Computer.FileSystem.GetFiles(SelectedFolderName, FileIO.SearchOption.SearchAllSubDirectories, "*.xl*")
    If UCase(FoundFile) IsNot Like "*.XLT*" Then
        Form3.ListBox1.Items.Add(FoundFile)
        SelectedFileList(ArrayPosition) = FoundFile
        ArrayPosition = ArrayPosition + 1
    End If
Next

One thing that sticks out is if I could fill the ListBox using GetFiles with manual filtering in the first loop, and then copy its contents to an array directly (rather than repeating the loop), that would cut out a step?

Thanks
 
Don't know why you're mixing arrays into this, but if you need a "dynamic array" use a List(Of String).
I'm not entirely sure what you mean by calling it multiple times with each filter
This is a filter: "*.xl*". If you have other file types you need to GetFiles each of those as well. Or as I said get all files and check their extension as you go.
 
The array is stored at module level to hold file paths. This is just a file selection sub, later I open each file and make changes to each using Excel Interop. I basically want all excel files except .xlt / .xlts and as there are far more extensions I want rather than what I want to exclude I thought it would be more efficient.

At the moment the current way is fast and causes no problems, I just wanted to understand the best way whilst I'm learning. The string list looks promising, I'll take a look at that, a dynamic array is exactly what I am looking for. I know I could ReDim Preserve to use an array and resize and load values in one loop but I think I remember reading thats also quite inefficient in terms of speed / resources.

I'll have a play with List and report my code later incase anyone else stumbles across the thread with similar requirements.

Thanks for the advice!

EDIT: Having listed all the file extensions it does turn out *.xls* gets every current format excluding Excel templates so all this is kind of redundant as the one filter will work. But I'd still like to pursue the best solution as its good learning experience for future stuff!
 
Back
Top