Get All Files from Directories and Sub-Directories - Vb.NET

Posted by Venkat | Labels: , ,

Here i have to discuss about how to get all the files of Directories and SubDirectories.

Generally we know how to get the files from From directory

Vb.NET


Imports System.IO

Dim position as integer = 1

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 + "
")

position += 1

End Sub

Give the path like this

GetFiles("C:\Test\")

And there is a simple way to get all files like this

Dim di as new IO.DirectoryInfo("C:\uploadfiles")
Dim finfo as IO.FileInfo() = di.GetFiles("*.*",IO.SearchOption.AllDirectories)


Get All files of Directories and SubDirectories - C#

Posted by Venkat | Labels: , ,

Here i have to discuss about how to get all the files of Directories and SubDirectories.

Generally we know how to get the files from From directory

ex: Directory.GetFiles("path");

C# Code:


using System.IO;

private int position = 1;

public void GetFiles(string path)

{

if (File.Exists(path))

{

// if file Exists

ProcessFile(path);

}

else if (Directory.Exists(path))

{

// if Directory exists

ProcessDirectory(path);

}

}

public void ProcessDirectory(string targetDirectory)

{

string[] fileEntries = Directory.GetFiles(targetDirectory);

foreach (string fileName in fileEntries)

ProcessFile(fileName);

string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);

foreach (string subdirectory in subdirectoryEntries)

ProcessDirectory(subdirectory);

}

public void ProcessFile(string path)

{

FileInfo fi = new FileInfo(path);

Response.Write("File Number " + position.ToString() + ". Path: " + path + "
"
);

position++;

}


Give File Path like this


GetFiles("C:\\Test\\");

PayOffers.in