splowe
splowe

Reputation: 11

Directory.Enumerate Files - need to filter by strict file extension

I have the following code:

Dim File_Names_In_Path = Directory.EnumerateFiles(DropBox_Path, "*.csv")

which works fine on newer machines, however on a few older machines it's finding some files that the users have renamed to "*.csvo".

I'm aware of why this happens - with a wildcard and a three letter extension as per Microsoft's documentation.

I don't really have the option of making the users rename their old files.

How can I make the Directory.EnumerateFiles filter results to only pick .csv files?

I think it might be connected to using .where and the file extension but can't just figure how to add this to my code.

Upvotes: 1

Views: 193

Answers (1)

Andrew Morton
Andrew Morton

Reputation: 25023

If you want it to use the exact case of "csv" then

Dim File_Names_In_Path = Directory.EnumerateFiles(DropBox_Path, "*.csv").
                         Where(Function(f) Path.GetExtension(f) = ".csv")

or to make it case-insensitive:

Dim File_Names_In_Path = Directory.EnumerateFiles(DropBox_Path, "*.csv").
                         Where(Function(f) Path.GetExtension(f).Equals(".csv", StringComparison.InvariantCultureIgnoreCase))

Note that the Path.GetExtension Method includes the dot at the start of the extension.

Upvotes: 3

Related Questions