LINQ - GetDirectories that contain specific files

S

Scott Rymer

I'm currently loading a treeview with a directory tree which excludes user
defined paths:

Private Sub AddFolders(ByVal ParentNode As TreeNode)

'Load the Folders to exclude from the listview
Dim xFolders = From f In
XDocument.Parse(My.Resources.ExludeFolders).Descendants("FolderPath") _
Select f.Value

'Get the folders under the ParentNode
Dim folders = From folder In New
DirectoryInfo(ParentNode.Tag).GetDirectories() _
Where Not xFolders.Contains(folder.FullName) _
Select folder

'Add them to the Directory tree
For Each folder In folders
Dim N = ParentNode.Nodes.Add(folder.Name)
N.Tag = folder.FullName
Next

End Sub

What I would also now like to do is only include folders which contains
specific file types (*.dft) - or exclude ones that don't. I'm trying the
code below but I'm getting warning "Runtime errors might occur when
converting 'System.Collections.Generic.IEnumerable(Of String)' to 'String'"

Dim folders = From folder In New
DirectoryInfo(ParentNode.Tag).GetDirectories() _
Where folder.FullName.Contains(From d In
folder.GetFiles("*.dft", SearchOption.TopDirectoryOnly) Select d.FullName) _
And Not xFolders.Contains(folder.FullName) _
Select folder

Any suggestions?
 
A

Alexander Mueller

Scott said:
What I would also now like to do is only include folders which contains
specific file types (*.dft) - or exclude ones that don't. I'm trying
the code below but I'm getting warning "Runtime errors might occur when
converting 'System.Collections.Generic.IEnumerable(Of String)' to 'String'"

Dim folders = From folder In New
DirectoryInfo(ParentNode.Tag).GetDirectories() _
Where folder.FullName.Contains(From d In
folder.GetFiles("*.dft", SearchOption.TopDirectoryOnly) Select
d.FullName) _
And Not xFolders.Contains(folder.FullName) _
Select folder

Any suggestions?

"FullName.Contains()" expects a string as its argument.
Your LINQ-subquery "From d In
folder.GetFiles("*.dft", SearchOption.TopDirectoryOnly"
provides an IEnumerable Of string. So that won't work.

If you only want the directories returned that contain *.dft
Any() is a pretty good extension method to do the test:


Dim folders = From folder In _
New DirectoryInfo(ParentNode.Tag).GetDirectories() _
Where (From f In folder.GetFiles("*.dft", _
SearchOption.TopDirectoryOnly)).Any() _
And Not xFolders.Contains(folder.FullName _
Select folder



MfG,
Alex
 
A

Alexander Mueller

Scott said:
What I would also now like to do is only include folders which contains
specific file types (*.dft) - or exclude ones that don't. I'm trying
the code below but I'm getting warning "Runtime errors might occur when
converting 'System.Collections.Generic.IEnumerable(Of String)' to 'String'"

Dim folders = From folder In New
DirectoryInfo(ParentNode.Tag).GetDirectories() _
Where folder.FullName.Contains(From d In
folder.GetFiles("*.dft", SearchOption.TopDirectoryOnly) Select
d.FullName) _
And Not xFolders.Contains(folder.FullName) _
Select folder

Any suggestions?

"FullName.Contains()" expects a string as its argument.
Your LINQ-subquery "From d In
folder.GetFiles("*.dft", SearchOption.TopDirectoryOnly"
provides an IEnumerable Of string. So that won't work.

If you only want the directories returned that contain *.dft
Any() is a pretty good extension method to do the test:


Dim folders = From folder In _
New DirectoryInfo(ParentNode.Tag).GetDirectories() _
Where (From f In folder.GetFiles("*.dft", _
SearchOption.TopDirectoryOnly)).Any() _
And Not xFolders.Contains(folder.FullName _
Select folder



MfG,
Alex
 
Top