Before reading this article, I highly recommend reading the previous part:
Question: What is a realistic example of the use of recursion?
Answer: A recursive function is a function that calls itself.
Probably in an interview, an interviewer may ask you to provide an example of a use of recursion in your project.
You may answer, there is no project where the requirement is to find all the files in a folder and in all the sub-folders in the hierarchy. This is a very good example of where we could use recursion.
For example we have a folder structure and in this one folder sample it has 2 files and one folder, then the outer folder has 2 files and one folder, InnerFolder1, has again 2 files and one folder, InnerFolder2, is the innermost folder with only two files, no other folders, like in the following image.
So basically we want a method that can loop through the folder hierarchy and provide us all the file names. We can easily do that using a recursive function.
So, we will first create the method by the following procedure.
Step 1
Create a console application named InterviewQuestionPart5.
Now create the method for finding and printing all the file names from the folder structure by the following code.
- using System;
- namespace InterviewQuestionPart5
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- }
- private static void FindFiles(string path)
- {
- foreach(string filename in Directory.GetFiles(path))
- {
- Console.WriteLine(filename);
-
- }
-
- foreach (string directory in Directory.GetDirectories(path))
- {
-
- FindFiles(directory);
-
- }
-
- }
- }
- }
Code description
Now what we are doing in this method is first we get part of the folder structure by the parameter path in the method and now there are two foreach loops, the first loop invokes the GetFiles() static method and gets us all the files that are in the folder only files, not folders, and prints all the file names. After printing a filename it comes out to the first loop and goes to the next loop. What we are doing is invoking the GetDirectories() static method that gives us the directory in the folder. Now we are calling the FindFiles() method that is the same method, it is calling itself. Now it will the first foreach loop again, it is calling the GetFiles() method and prints the file name in the OuterFolder. And again, it will executer the second foreach loop and prints the inner directory in the OuterFolder and we are calling itself again, FindFiles(). So it will do like this until it is common to the innermost folders and prints the files that are in that folder from the first loop. Now when we get to the second loop it calls the GetDirectories() and it's not going to return any directory. At this point the method doe not return, so the recursion will break and control is returned to the caller.
Step 2
Now to implement this static method we need to include the namespace System.IO. And we get the path of the folder structure from the user then invoke the static method without using a class name by the following code.
- using System;
- using System.IO;
-
- namespace InterviewQuestionPart5
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- Console.WriteLine("please enter Path");
- string path = Console.ReadLine();
-
-
- FindFiles(path);
-
- }
- private static void FindFiles(string path)
- {
- foreach(string filename in Directory.GetFiles(path))
- {
- Console.WriteLine(filename);
-
- }
-
- foreach (string directory in Directory.GetDirectories(path))
- {
-
- FindFiles(directory);
-
- }
-
- }
- }
- }
Step 3
Before running this program, we need to create a folder structure in our system. First we will create the folder structure in a specific path, here I will create a folder structure in the C directory. Inside I will create a folder Sample and within this is file1, file2 and OuterFolder. Then inside it InnerFolder1 and file3 and file4, the same as the Folder Structure.
Step 4
So now we will run the program and provide the specific path and see the output. It shows all the files file1, file2 through to file8 and that is the structure. So this is a very simple example of where we can use recursion.