473,395 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Process files in directory and all subdirectories

Hello,

I'm creating a program to parse a directory and all files within that
directory (including subdirectories), but 'directoryinfo' only parses the
current directory. Is it possible to have it process all files within
subdirectories as well?

Thanks --

Alex
Sep 13 '07 #1
2 6643
just use the System.IO.Directory static class

For Each strDir as String in
System.IO.Directory.GetDirectories(SomeDirectoryHe re)

For Each strFile as String In System.IO.Directory.GetFiles(strDir)

''Do Something with the files

Next

Next

hope this helps
--
-iwdu15
Sep 13 '07 #2
On Sep 13, 12:52 pm, "Alex" <sama...@gmail.comwrote:
Hello,

I'm creating a program to parse a directory and all files within that
directory (including subdirectories), but 'directoryinfo' only parses the
current directory. Is it possible to have it process all files within
subdirectories as well?

Thanks --

Alex
Imports System.IO

Public Class EnumDir

Private _Root As String
Private _Files As List(Of String)
Private _Folders As List(Of String)
Private _TotalSize As Long
Private _TotalFiles As Long
Private _TotalFolders As Long

Public Property Root() As String
Get
Return _Root
End Get
Set(ByVal value As String)
_Root = value
End Set
End Property

Public Property Files() As List(Of String)
Get
Return _Files
End Get
Set(ByVal value As List(Of String))
_Files = value
End Set
End Property

Public Property Folders() As List(Of String)
Get
Return _Folders
End Get
Set(ByVal value As List(Of String))
_Folders = value
End Set
End Property

Public Property TotalSize() As Long
Get
Return _TotalSize
End Get
Set(ByVal value As Long)
_TotalSize = value
End Set
End Property

Public Property TotalFiles() As Long
Get
Return _TotalFiles
End Get
Set(ByVal value As Long)
_TotalFiles = value
End Set
End Property

Public Property TotalFolders() As Long
Get
Return _TotalFolders
End Get
Set(ByVal value As Long)
_TotalFolders = value
End Set
End Property

Public Sub New()

_Root = ""
_Files = New List(Of String)
_Folders = New List(Of String)
_TotalSize = 0
_TotalFiles = 0
_TotalFolders = 0

End Sub

''' <summary>
''' Enumerates all Files in a Folder Tree
''' </summary>
''' <param name="Root"></param>
''' <remarks>Root Directory to Enumerate</remarks>
Public Sub New(ByVal Root As String)

_Root = Root
_Files = New List(Of String)
_Folders = New List(Of String)
_TotalSize = 0
_TotalFiles = 0
_TotalFolders = 0
Me.GetFiles(_Root)

End Sub

Public Sub GetFiles(ByVal Path As String)

Dim myDirectoryRoot As New DirectoryInfo(Path)
Dim di As DirectoryInfo
Dim fi As FileInfo
Dim lSize As Long = 0

For Each fi In myDirectoryRoot.GetFiles
_Files.Add(fi.FullName)
_TotalFiles += 1
_TotalSize += fi.Length
Next
For Each di In myDirectoryRoot.GetDirectories()
_Folders.Add(di.FullName)
_TotalFolders += 1
GetFiles(di.FullName)
Next
myDirectoryRoot = Nothing

End Sub

End Class

Sep 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Anand K Rayudu | last post by:
Hi all, I am trying to find a way to get the files recursively in a given directory, The following code is failing, can some one please suggest what could be problem here from os import...
1
by: Thieum22 | last post by:
Hi, I try to go through a directory and it's subdirectories to reah the properties of each files. But I have a problem to set active the directory where the files are, in order to display their...
1
by: Earl Teigrob | last post by:
Does someone have or know of an algorythm (method) that will delete all files under a give directory and its subdirectories based on a wildcard mask? I can use this for one directory for each...
2
by: kamalak | last post by:
hi, can someone help me in writing code in C# for getting the no of files in a directory and all its subdirectories and their subdirectories where the no of subdirectories and their...
9
by: smoothoperator12 | last post by:
Hi all, I have spent hours trying to figure out where I have went wrong with my code for my recursive function to list all the files in a directory, and all of the files in all of its...
8
by: theCancerus | last post by:
Hi All, I am not sure if this is the right place to ask this question but i am very sure you may have faced this problem, i have already found some post related to this but not the answer i am...
2
by: dj | last post by:
Hello All, I am attempting to us os.walk to populate two lists with values from a directory. The first list contains all the files in the directory and subdirectories. The second list contains...
4
by: supriyamk | last post by:
Hi, I am trying to search a directory for subdirectories, recreate the subdirectories elsewhere and copy only certain files from current subdirectory to new subdirectory. In other words i am...
9
by: kkhan5000 | last post by:
Hello, I am new to PHP and have put together the following code just by copying other examples from the web. Now I am stuck and would like to sort and display files alphabetically, and also filter...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.