mystack
mystack

Reputation: 5532

What is the best way of searching a files with specific extension in another group of files

I want to write a search algoritham for searching files having extension .txt(the occurance of all .txt file) in another group of files.ie i have a directory named d:\myfolder.I have a files of extension .txt in this folderin as well as subfolders. There are also files of extension .proj,.dat,.cs etc in this folder as well as subfolders.I want to search the occurance of .txt files in these files I thought to create a list(LISTA) that contain the names of all .txt files and also create a seperate list(ListB) containg the fullpath of all other files.Then iterate throgh the lists to get the result.Is that a better method.Else is there any other better method

Upvotes: 0

Views: 73

Answers (2)

Habib
Habib

Reputation: 223392

If you want to search for a particular file in folder and sub folder then easiest would be to use Directory.GetFiles like

string path = @"C:\YourFolder";
string[] txtFiles = System.IO.Directory.GetFiles(path, 
                                                 "*.txt", 
                                                SearchOption.AllDirectories);

You don't have to populate two different lists, one with txt extensions and one without. Directory.GetFiles would let you find all the txt files in a folder and sub folder.

Upvotes: 1

donstack
donstack

Reputation: 2715

if you use LINQ:

 List<FileInfo> list = new List<FileInfo>();
            list= list.Where(a => a.Extension == Path.GetExtension(a.FullName)).ToList();

Upvotes: 0

Related Questions