user1447679
user1447679

Reputation: 3240

In vb.net, how do I use directory info with specific files name in the query

Lets say I have a list of files separated by a comma.

Dim listOfFiles As String() = filesPosted.Split(",")

And I use DirectoryInfo to grab that list of files and send it to another array.

        Dim files = New DirectoryInfo(StorageRoot) _
                    .GetFiles("*", SearchOption.TopDirectoryOnly) _
                    .Where(Function(f) Not f.Attributes.HasFlag(FileAttributes.Hidden)) _
                    .Where(Function(f) filesPosted.Contains(f.Name)) _
                    .[Select](Function(f) New FilesStatus(f)).ToArray()

The problem I'm facing is, I need my condition to be more strict. I'll explain:

If my listOfFiles contains ( abc.txt, xyz.txt ) and there's a filename of aabc.txt in the directory that is being searched, it'll return both abc.txt and aabc.txt. I know this is because of this part of the clause:

.Where(Function(f) filesPosted.Contains(f.Name))

As the contains attribute is finding this other file... But I don't want it. I want the files to match exactly based on the string().

Is there a better way to do this without cycling through each file? A tighter way to make it a strict condition on "Contains" ?

Thank you for your help!

Upvotes: 1

Views: 157

Answers (1)

reckface
reckface

Reputation: 5858

Try:

Dim listOfFiles As String() = filesPosted.Split(",").Select(function(f) f.ToLower())
' then
Dim files = New DirectoryInfo(StorageRoot) _
                .GetFiles("*", SearchOption.TopDirectoryOnly) _
                .Where(Function(f) Not f.Attributes.HasFlag(FileAttributes.Hidden)) _
                .Where(Function(f) listOfFiles.Any(function(l) l = f.Name.ToLower())) _
                .[Select](Function(f) New FilesStatus(f)).ToArray()

Sorry, poor C# to VB.Net conversion

Upvotes: 1

Related Questions