Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for file in directories recursively

Tags:

c#

recursion

I have the following code to recursively search for files through a directory, which returns a list of all xml files to me. All works well, except that xml files in the root directory are not included in the list.

I understand why, since the first thing it does is get the directories in the root, then get files, thus missing the GetFiles() call on the root. I tried including the GetFiles() call prior to the foreach, but the results are not as I expect.

public static ArrayList DirSearch(string sDir) {     try     {         foreach (string d in Directory.GetDirectories(sDir))         {             foreach (string f in Directory.GetFiles(d, "*.xml"))             {                 string extension = Path.GetExtension(f);                 if (extension != null && (extension.Equals(".xml")))                 {                 fileList.Add(f);                 }             }             DirSearch(d);         }     }     catch (Exception ex)     {         Console.WriteLine(ex.Message);     }     return fileList; } 

My directory structure is something like:

RootDirectory         test1.0.xml             test1.1.xml             test1.2.xml   2ndLevDir             test2.0.xml             test2.1.xml   3rdLevDir                test3.0.xml                test3.1.xml 

Code returns:

test2.0.xml test2.1.xml test3.0.xml test3.1.xml 

I would like to return every file including:

test1.0.xml test1.1.xml test1.2.xml 

Not very well verse with recursion. Any pointers would be greatly appreciated.

like image 861
Paul Avatar asked Mar 22 '12 20:03

Paul


People also ask

How do I search for a recursive file in Linux?

The grep command is used to search text or scans the given record for lines containing a match to the given strings or words. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines.

What is a recursive file search?

Alternatively referred to as recursive, recurse is a term used to describe the procedure capable of being repeated. For example, when listing files in a Windows command prompt, you can use the dir /s command to recursively list all files in the current directory and any subdirectories.


2 Answers

You could use this overload of Directory.GetFiles which searches subdirectories for you, for example:

string[] files = Directory.GetFiles(sDir, "*.xml", SearchOption.AllDirectories); 

Only one extension can be searched for like that, but you could use something like:

var extensions = new List<string> { ".txt", ".xml" }; string[] files = Directory.GetFiles(sDir, "*.*", SearchOption.AllDirectories)                     .Where(f => extensions.IndexOf(Path.GetExtension(f)) >= 0).ToArray(); 

to select files with the required extensions (N.B. that is case-sensitive for the extension).


In some cases it can be desirable to enumerate over the files with the Directory.EnumerateFiles Method:

foreach(string f in Directory.EnumerateFiles(sDir, "*.xml", SearchOption.AllDirectories)) {     // do something } 

Consult the documentation for exceptions which can be thrown, such as UnauthorizedAccessException if the code is running under an account which does not have appropriate access permissions.

If the UnauthorizedAccessException is a problem, then please see the fine answers at Directory.EnumerateFiles => UnauthorizedAccessException.

like image 153
Andrew Morton Avatar answered Oct 07 '22 11:10

Andrew Morton


This returns all xml-files recursively :

var allFiles = Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories); 
  • http://msdn.microsoft.com/en-us/library/ms143316%28v=vs.100%29.aspx
  • http://msdn.microsoft.com/en-us/library/ms143448.aspx#Y252
like image 25
Tim Schmelter Avatar answered Oct 07 '22 10:10

Tim Schmelter