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

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)

If you have better solution, just tell me !

0 comments: