CSharp: How to get a recursive list of files with a particular extension from a directory and its subdirectories

From /etc/src/
Jump to: navigation, search

Description

This C# code sample demonstrates how to get a recursive list of files with a particular extension from a directory and its sub-directories.

Prerequisites

  • C# compiler with any standard ASCII editor like Notepad, or
  • Visual Studio Express with C# compiler, or
  • Visual Studio.Net or better with C# compiler.

Code

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4  
 5 class Program {
 6     static void Main () {
 7         string rootDirectory = @"C:\Libs\Selenium";
 8         string extension = "xml";
 9  
10         List<string> files = GetFilesRecursively (rootDirectory, extension);
11  
12         Console.WriteLine ("Got {0} files of extension {1} in directory {2}", files.Count, extension, rootDirectory);
13         foreach (string file in files) {
14             Console.WriteLine (file);
15         }
16     }
17  
18     /// <summary>
19     /// This function calls itself recursively for each of the subdirectories that it finds
20     /// in the root directory passed to it. It returns files of the extension as specified
21     /// by the caller.
22     /// </summary>
23     /// <param name="rootDirectory">The directory from which the file list is sought.</param>
24     /// <param name="extension">The particular extension for which the file list is sought.</param>
25     /// <returns>A list of all the files with extension as specified by the caller. This list
26     /// includes the files in the current directory as well its sub-directories.</returns>
27     static List<string> GetFilesRecursively (string rootDirectory, string extension) {
28         // Uncomment this line only if you want to trace the control as it goes from
29         // sub-directory to sub-directory. Be ready for long scrolls of text output:
30         //Console.WriteLine ("Currently in directory {0}", rootDirectory);
31  
32         // Create an output list:
33         List<string> opList = new List<string> ();
34  
35         // Get all files in the current directory:
36         string[] allFiles = Directory.GetFiles (rootDirectory, "*." + extension);
37  
38         // Add these files to the output list:
39         opList.AddRange (allFiles);
40  
41         // Get all sub-directories in current directory:
42         string[] subDirectories = Directory.GetDirectories (rootDirectory);
43  
44         // And iterate through them:
45         foreach (string subDir in subDirectories) {
46             // Get all the files from the sub-directory:
47             List<string> subDirFileList = GetFilesRecursively (subDir, extension);
48             // And add it to this list:
49             opList.AddRange (subDirFileList);
50         }
51  
52         // Finally return the output list:
53         return opList;
54     }
55 }

Output

Got 8 files of extension xml in directory C:\Libs\Selenium
C:\Libs\Selenium\net35\Selenium.WebDriverBackedSelenium.xml
C:\Libs\Selenium\net35\ThoughtWorks.Selenium.Core.xml
C:\Libs\Selenium\net35\WebDriver.Support.xml
C:\Libs\Selenium\net35\WebDriver.xml
C:\Libs\Selenium\net40\Selenium.WebDriverBackedSelenium.xml
C:\Libs\Selenium\net40\ThoughtWorks.Selenium.Core.xml
C:\Libs\Selenium\net40\WebDriver.Support.xml
C:\Libs\Selenium\net40\WebDriver.xml

Explanation

A list of files with a particular extension type is often sought by a user. This program demonstrates a kind of "search" utility, in that it retrieves a list of files of a particular extension type from a directory, including its sub-directories. In our case in the example above, we search for all files with the extension xml that may be present within the directory C:\Libs\Selenium, including its sub-directories. The output can be seen in the Output section above.

The actual action happens within the GetFilesRecursively() function. Here we create an output list that we first populate with the files in the current directory of the extension sought by the caller. We next get a list of all the sub-directories in the current sub-directory and we call the function again for each of these sub-directories. We add the lists returned by each of these sub-directories to the current directories list of files. This we return to the Main() function which subsequently prints out the list of all the files thus retrieved.

Additional notes

None

Known issues

In case the directory name passed by the caller does not exist, the code will throw an exception. To counter this, use the check as shown on lines 3-6 in the code as below.

 1 static List<string> GetFilesRecursively (string rootDirectory, string extension) {
 2     // Check if the directory exists:
 3     if (!Directory.Exists (rootDirectory)) {
 4         // Return an empty list if directory does not exist:
 5         return new List<string> ();
 6     }
 7  
 8     List<string> opList = new List<string> ();
 9  
10     // As before
11  
12     return opList;
13 }

References

See also

Further reading

External links

Author link