Recently I had to find all the files of a directory, and it’s sub directories. I thought there would be an out of the box way to do this, but discovered there was no way. After googling I discovered a few different sources and put together something of my own. Here I am sharing how you can achieve this as well:
The first step is to call our namespace to easy coding:
C#:
VB.NET
Next we will call create a function which passes in our base/root path. Using this path we will check if there are any child folders, or if it is a file itself. If it is a file, we will print out the file path. If it is a directory, we will check if it has sub directories or any files within it, and print them out as well. This is done through a recursive call.
C#
|
public void GetFiles(string path)
{
if (File.Exists(path))
{
// This path is a file
ProcessFile(path);
}
else if (Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path);
}
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries)
ProcessFile(fileName);
// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}
// Insert logic for processing found files here.
public void ProcessFile(string path)
{
FileInfo fi = new FileInfo(path);
Response.Write("File Number " + position.ToString() + ". Path: " + path + " <br />");
position++;
} |
VB.NET
|
Public Sub GetFiles(ByVal path As String)
If File.Exists(path) Then
' This path is a file
ProcessFile(path)
ElseIf Directory.Exists(path) Then
' This path is a directory
ProcessDirectory(path)
End If
End Sub
' Process all files in the directory passed in, recurse on any directories
' that are found, and process the files they contain.
Public Sub ProcessDirectory(ByVal targetDirectory As String)
' Process the list of files found in the directory.
Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
For Each fileName As String In fileEntries
ProcessFile(fileName)
Next
' Recurse into subdirectories of this directory.
Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
For Each subdirectory As String In subdirectoryEntries
ProcessDirectory(subdirectory)
Next
End Sub
' Insert logic for processing found files here.
Public Sub ProcessFile(ByVal path As String)
Dim fi As New FileInfo(path)
Response.Write("File Number " + position.ToString() + ". Path: " + path + " <br />")
position += 1
End Sub |
To use the methods just call the following method with our base/root directory:
C#
VB.NET
This helped solve some problems on my side. Hope this helps you as well.
Was this article helpful? Don't forget to rate it. Ratings helps community members identify top & useful articles.