473,406 Members | 2,369 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,406 software developers and data experts.

Loop thru all subfolders and list all files under each

I need a VB routine to loop thru a select top folder to find all subfolders
and list all subfolders/files under each of these subfolders.
Any help is greatly appreciated.
Bill
Nov 21 '05 #1
9 18929
"Bill Nguyen" <bi*****************@jaco.com> schrieb:
I need a VB routine to loop thru a select top folder to find all subfolders
and list all subfolders/files under each of these subfolders.


<URL:http://dotnet.mvps.org/dotnet/samples/filesystem/FileSystemEnumerator.zip>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
I appreciated this but I really don't know where to start. The readme.txt
doesn't tell me a thing about how to get this going.
Any thing I have to do in order to get the demo running?

Bill

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
"Bill Nguyen" <bi*****************@jaco.com> schrieb:
I need a VB routine to loop thru a select top folder to find all
subfolders and list all subfolders/files under each of these subfolders.


<URL:http://dotnet.mvps.org/dotnet/samples/filesystem/FileSystemEnumerator.zip>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
"Bill Nguyen" <bi*****************@jaco.com> schrieb:
I appreciated this but I really don't know where to start. The readme.txt
doesn't tell me a thing about how to get this going.
Any thing I have to do in order to get the demo running?


I see, my sample is a bit "oversized". The listing below shows a reduced
version:

\\\
Imports System.IO

Friend Module Module1
Friend Sub Main()
EnumerateDirectory("C:\WINDOWS")
End Sub

Private Sub EnumerateDirectory(ByVal RootDirectory As String)

For Each s As String In Directory.GetFiles(RootDirectory)
Console.WriteLine("File found: " & s)
Next s
For Each s As String In Directory.GetDirectories(RootDirectory)

' Prevent enumeration of reparse points.
'
' Platform SDK: Storage -- Reparse Points
'
<URL:http://msdn.microsoft.com/library/en-us/fileio/base/reparse_points.asp>
'
' You can create an infinitely recursive directory tree
'
<URL:http://blogs.msdn.com/oldnewthing/archive/2004/12/27/332704.aspx>
If _
Not (File.GetAttributes(s) And FileAttributes.ReparsePoint)
= _
FileAttributes.ReparsePoint _
Then
Console.WriteLine("Directory found: " & s)
EnumerateDirectory(s)
End If
Next s
End Sub
End Module
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4
herfried;
I can't run any of your code in your application. It gave me some kind of
error!
Below is my newbie version but all it can do is to list all files in a
single-level directory.
How can I loop thru all directories to list files in lowest level folder and
move to the next?

Thanks a billion

Bill

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim mDir, mFile, rootDir, mrootPath, mFolder, mFileName As String

Dim mCount As Integer

rootDir = "C:\windows"

' For Each s As String In Directory.GetFiles(rootDir)

For Each d As String In Directory.GetDirectories(rootDir)

'Console.WriteLine("File found: " & s)

'mSourceFile = .FileName.Substring(OpenFileDialog1.FileName.LastI ndexOf("\")
+ 1)

mFolder = d.Substring(d.LastIndexOf("\") + 1)

mrootPath = d.Substring(0, d.LastIndexOf("\"))

mDir += mrootPath & "\" & mFolder & vbCrLf

For Each f As String In Directory.GetFiles(d)

mFileName = f.Substring(f.LastIndexOf("\") + 1)

'mrootPath = f.Substring(1, d.LastIndexOf("\"))

mFile += mFileName & vbCrLf

Next f

MsgBox(mFile)

mFile = ""

Next d

MsgBox(mDir)

End Sub

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Oc**************@TK2MSFTNGP14.phx.gbl...
"Bill Nguyen" <bi*****************@jaco.com> schrieb:
I appreciated this but I really don't know where to start. The readme.txt
doesn't tell me a thing about how to get this going.
Any thing I have to do in order to get the demo running?


I see, my sample is a bit "oversized". The listing below shows a reduced
version:

\\\
Imports System.IO

Friend Module Module1
Friend Sub Main()
EnumerateDirectory("C:\WINDOWS")
End Sub

Private Sub EnumerateDirectory(ByVal RootDirectory As String)

For Each s As String In Directory.GetFiles(RootDirectory)
Console.WriteLine("File found: " & s)
Next s
For Each s As String In Directory.GetDirectories(RootDirectory)

' Prevent enumeration of reparse points.
'
' Platform SDK: Storage -- Reparse Points
'
<URL:http://msdn.microsoft.com/library/en-us/fileio/base/reparse_points.asp>
'
' You can create an infinitely recursive directory tree
'
<URL:http://blogs.msdn.com/oldnewthing/archive/2004/12/27/332704.aspx>
If _
Not (File.GetAttributes(s) And FileAttributes.ReparsePoint)
= _
FileAttributes.ReparsePoint _
Then
Console.WriteLine("Directory found: " & s)
EnumerateDirectory(s)
End If
Next s
End Sub
End Module
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5
"Bill Nguyen" <bi*****************@jaco.com> schrieb:
I can't run any of your code in your application. It gave me some kind of
error!


Error message?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #6
The whole purpose of Herfried's method was that it is a *recursive*
method, i. e. it calls itself. But you have removed the recursion and
therefore it only scans the first directory.

Here is Herfried's method again (slightly altered):

Imports System.IO

Friend Module Module1
Friend Sub Main()
EnumerateDirectory("C:\WINDOWS")
End Sub

Private Sub EnumerateDirectory(ByVal RootDirectory As String)

For Each s As String In Directory.GetFiles(RootDirectory)
'******* Perform task on file s here. Replace with your
own file
'******* manipulation code here
Next

For Each s As String In Directory.GetDirectories(RootDirectory)

'Correct me if I'm wrong Herfried but this line prevents
recursion into
'empty folders
If Not (File.GetAttributes(s) And
FileAttributes.ReparsePoint) = _
FileAttributes.ReparsePoint Then

'***** Perform work in folder s here. Replace with
your
'***** folder manipuation code here

'Notice we're calling ourself here to get data on child
folders
'This is the recursive part!
EnumerateDirectory(s)
End If
Next
End Sub
End Module
BTW: Why are you using substring parse out the portions of your
folders and filename? why not use the methods in the System.IO
namespace?

For example, instead of:

mrootPath = d.Substring(0, d.LastIndexOf("\"))

use this instead:

mrootPath = Path.GetPathRoot(d)

and instead of

mFileName = f.Substring(f.LastIndexOf("\") + 1)

use this instead:

mFileName = Path.GetFileName(f)

Nov 21 '05 #7
Chris;

Thanks for the tip.
mFileName = Path.GetFileName(f) worked great!

mrootPath = Path.GetPathRoot(d) only give me the root path. I need to get
the name of the folder at subsequent levels.
For example, the full path:

C:\Base\Level1\level2\level3\Level4\Filename.TXT
C:\Base is the base (root) dir.
I need to parse the folder name Level1 thru level4 and assign a
corresponding folder level ID 2,3,4) to these folders.

Any idea how to do this?
Thanks in advance.

Bill


"Chris Dunaway" <du******@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
The whole purpose of Herfried's method was that it is a *recursive*
method, i. e. it calls itself. But you have removed the recursion and
therefore it only scans the first directory.

Here is Herfried's method again (slightly altered):

Imports System.IO

Friend Module Module1
Friend Sub Main()
EnumerateDirectory("C:\WINDOWS")
End Sub

Private Sub EnumerateDirectory(ByVal RootDirectory As String)

For Each s As String In Directory.GetFiles(RootDirectory)
'******* Perform task on file s here. Replace with your
own file
'******* manipulation code here
Next

For Each s As String In Directory.GetDirectories(RootDirectory)

'Correct me if I'm wrong Herfried but this line prevents
recursion into
'empty folders
If Not (File.GetAttributes(s) And
FileAttributes.ReparsePoint) = _
FileAttributes.ReparsePoint Then

'***** Perform work in folder s here. Replace with
your
'***** folder manipuation code here

'Notice we're calling ourself here to get data on child
folders
'This is the recursive part!
EnumerateDirectory(s)
End If
Next
End Sub
End Module
BTW: Why are you using substring parse out the portions of your
folders and filename? why not use the methods in the System.IO
namespace?

For example, instead of:

mrootPath = d.Substring(0, d.LastIndexOf("\"))

use this instead:

mrootPath = Path.GetPathRoot(d)

and instead of

mFileName = f.Substring(f.LastIndexOf("\") + 1)

use this instead:

mFileName = Path.GetFileName(f)

Nov 21 '05 #8

C:\Base\Level1\level2\level3\Level4\Filename.TXT
C:\Base is the base (root) dir.
I need to parse the folder name Level1 thru level4 and assign a
corresponding folder level ID 2,3,4) to these folders.

Any idea how to do this?
Thanks in advance.

Do something like this (one I prepared earlier... you don't need to use XML.
To add the folder level counter add a line to increment a class-level counter
inside the recursion loop and include its value in the output:

Private Function GetFiles(ByRef XmlDoc As XmlDocument, ByRef ParentNode
As XmlElement, ByVal DirPath As String, Optional ByVal IncludeSubFolders As
Boolean = True) As XmlNode

Dim objFileInfo As FileInfo
Dim objDir As DirectoryInfo = New DirectoryInfo(DirPath)
Dim objSubFolder As DirectoryInfo

'add a node for each file
For Each objFileInfo In objDir.GetFiles()
Dim MyFileNode As XmlElement = XmlDoc.CreateElement("Node")
MyFileNode.SetAttribute("type", "file")
MyFileNode.SetAttribute("id", intCount.ToString)
intCount += 1
MyFileNode.SetAttribute("type", objFileInfo.Extension)
MyFileNode.SetAttribute("size", objFileInfo.Length.ToString)
MyFileNode.SetAttribute("text", objFileInfo.Name)
ParentNode.AppendChild(MyFileNode)
Next

'call recursively to do sub folders
'if you don't want this set optional
'parameter to false

'add the folder node
If IncludeSubFolders Then
For Each objSubFolder In objDir.GetDirectories()
Dim DirNode As XmlElement = XmlDoc.CreateElement("Node")
DirNode.SetAttribute("type", "folder")
DirNode.SetAttribute("id", intCount.ToString)
intCount += 1
DirNode.SetAttribute("text", objSubFolder.Name)
ParentNode.AppendChild(DirNode)
GetFiles(XmlDoc, DirNode, objSubFolder.FullName, True)
Next
End If

Return ParentNode

End Function
Nov 21 '05 #9
Stuart;

Sorry for my very late response. I'd been away for a long period of time.
Thanks for your help!

Bill

"Stuart Irving" <Stuart Ir****@discussions.microsoft.com> wrote in message
news:62**********************************@microsof t.com...

C:\Base\Level1\level2\level3\Level4\Filename.TXT
C:\Base is the base (root) dir.
I need to parse the folder name Level1 thru level4 and assign a
corresponding folder level ID 2,3,4) to these folders.

Any idea how to do this?
Thanks in advance.

Do something like this (one I prepared earlier... you don't need to use
XML.
To add the folder level counter add a line to increment a class-level
counter
inside the recursion loop and include its value in the output:

Private Function GetFiles(ByRef XmlDoc As XmlDocument, ByRef ParentNode
As XmlElement, ByVal DirPath As String, Optional ByVal IncludeSubFolders
As
Boolean = True) As XmlNode

Dim objFileInfo As FileInfo
Dim objDir As DirectoryInfo = New DirectoryInfo(DirPath)
Dim objSubFolder As DirectoryInfo

'add a node for each file
For Each objFileInfo In objDir.GetFiles()
Dim MyFileNode As XmlElement = XmlDoc.CreateElement("Node")
MyFileNode.SetAttribute("type", "file")
MyFileNode.SetAttribute("id", intCount.ToString)
intCount += 1
MyFileNode.SetAttribute("type", objFileInfo.Extension)
MyFileNode.SetAttribute("size", objFileInfo.Length.ToString)
MyFileNode.SetAttribute("text", objFileInfo.Name)
ParentNode.AppendChild(MyFileNode)
Next

'call recursively to do sub folders
'if you don't want this set optional
'parameter to false

'add the folder node
If IncludeSubFolders Then
For Each objSubFolder In objDir.GetDirectories()
Dim DirNode As XmlElement = XmlDoc.CreateElement("Node")
DirNode.SetAttribute("type", "folder")
DirNode.SetAttribute("id", intCount.ToString)
intCount += 1
DirNode.SetAttribute("text", objSubFolder.Name)
ParentNode.AppendChild(DirNode)
GetFiles(XmlDoc, DirNode, objSubFolder.FullName, True)
Next
End If

Return ParentNode

End Function

Nov 21 '05 #10

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

Similar topics

1
by: Eric Martin | last post by:
Hello, Does anyone know of a way to loop thru a SQL table using code in a stored procedure? I need to go thru each record in a small table and build a string using values from the fields...
5
by: Andrew Young | last post by:
How do I loop thru a result set Without using a curosr?
1
by: Jeremy Langworthy | last post by:
Hi I have a dynamicly generated form (well the elements are at least) that looks something like this: while( not end of returned records): <input name="plan_id" type="checkbox" id=""...
3
by: Pete Gomersall | last post by:
Hi All, I have seen many examples of deleting folders with subfolders and files. (system.io.directory/info etc delete method) However, I cannot find a decent code example which will work where...
4
by: VB Programmer | last post by:
How can I loop thru all textboxes on a form? I want to remove any single quotes in the data entry. I was thinking something like this: For Each c As Control In Me.Controls Dim t As TextBox =...
2
by: Michael Beck | last post by:
I am new to .net programming and I want to know if I can loop thru a specified number of controls on an html form. In Access, the code looks like this: For i = 1 to 10 Me("txtAlternate" & i) =...
6
by: doncee | last post by:
Using a multi select list box to open several records in a pre - defined form. Most of the code that follows is taken from a posting by Alan Browne on his web site. The click routine is supposed...
3
by: Marc Miller | last post by:
Hi all, I need to store a list of modules.functions() in an array and then loop thru the array and call the functions. I have not been able to get any code to work. Here's what I need to do:...
2
by: fniles | last post by:
I would like to loop thru all the files in a folder. For ex: I have a folder c:\temp and inside that folder there are many zip files. I want to read each zip files in c:\temp. How can I do that ?...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.