VB: Search files in a folder and show only Word and PDF files

637 Views Asked by At

I have problem searching folder and find all files with specific name. With this method I am able to browse my folder and find all files. But only Word files.

Dim fi As FileInfo() = path.GetFiles(fileName & "*.doc*", SearchOption.AllDirectories)

Now I would like to search Word and PDF. Is there a way how I can find it? If I just write a dot instead of .doc it searches the folder but finds all files. Excel, txt etc.

Dim fi As FileInfo() = path.GetFiles(fileName & "*.*", SearchOption.AllDirectories)

The goal would be to find only certain types of files.

1

There are 1 best solutions below

3
Alex Essilfie On

It is not possible to use the GetFiles method to enumerate files with multiple extensions. To do that, you'll have to write your own code to do the filtering. I also recommend you use the EnumerateFiles method as it provides better performance over the GetFiles method in most cases.

Here is sample code to do the filtering as you require

Dim filterExtensions = {".docx", ".pdf"}

Dim files = From f In System.IO.Directory.EnumerateFiles("C:\Path", "*.*", _
                                                         SearchOption.AllDirectories)
            Let fi = New FileInfo(f)
            Where filterExtensions.Contains(fi.Extension.ToLower())
            Select fi