Get all directories and subdirectories
-
Hi, Does anybody knows how to get all driections and subdirectoires of a hdd? I took a look at System.IO.IsolatedStorage, but I could not specify the hdd. If someone has any ideas.... Thanks
Use the DirectoryInfo and FileInfo classes. --Colin Mackay--
EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
-
Use the DirectoryInfo and FileInfo classes. --Colin Mackay--
EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
-
Hi, Does anybody knows how to get all driections and subdirectoires of a hdd? I took a look at System.IO.IsolatedStorage, but I could not specify the hdd. If someone has any ideas.... Thanks
This code is for VB. VB.Net is quite similar though. Set a reference to Microsoft Scripting Runtime
Dim srt As New Scripting.FileSystemObject
Dim drv As Drive
Dim fld As Folder
Dim f As FileSet drv = srt.Drives("C:\")
Set fld = drv.RootFolder
For Each f In fld.Files
Debug.Print f.Name
NextIf you want to loop through each directory as well you will need to create another function to return the folder object. To check to see if a folder has sub folder use the following code: fld.SubFolders Michael
-
All the information is available on MSDN. It is an excellent resource. Iterating over the subdirectories[^] Getting the files in a directory[^] FileInfo class[^] DirectoryInfo class[^] This should help. --Colin Mackay--
EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
-
Hi, Does anybody knows how to get all driections and subdirectoires of a hdd? I took a look at System.IO.IsolatedStorage, but I could not specify the hdd. If someone has any ideas.... Thanks
This is some code I have used. It writes all files in a directory and sub directories to an xml document. The functions are recusrsive. Modify to suite your needs as appropriate.
'Create a new xml textwriter and write to a stringwriter object Dim sw As New StringWriter Dim writer As New XmlTextWriter(sw) 'Write document element writer.WriteStartElement("Files") 'Get the files here ListFoldersAndFiles(Server.MapPath("MyFolder"), writer) writer.WriteEndElement() 'Write end document element 'Clean up objects writer.Flush() writer.Close() writer = Nothing Dim xmldoc As New XmlDocument xmldoc.LoadXml(sw.ToString()) Private Sub ListFoldersAndFiles(ByVal pth As String, ByRef writer As XmlTextWriter) AddFiles(pth, writer) AddFolders(pth, writer) End Sub Public Sub AddFiles(ByVal pth As String, ByRef writer As XmlTextWriter) Dim sFiles() As String = Directory.GetFiles(pth) Dim iFiles As Integer For iFiles = 0 To UBound(sFiles) writer.WriteStartElement("Files") writer.WriteStartElement("Path") writer.WriteString(pth) writer.WriteEndElement() writer.WriteStartElement("Filename") writer.WriteString(Path.GetFileName(sFiles(iFiles))) writer.WriteEndElement() writer.WriteEndElement() Next End Sub Public Sub AddFolders(ByVal pth As String, ByRef writer As XmlTextWriter) Dim sDirectories() As String = Directory.GetDirectories(pth) Dim iDirectories As Integer Dim sDirectoryName As String For iDirectories = 0 To UBound(sDirectories) AddFolders(sDirectories(iDirectories), writer) AddFiles(sDirectories(iDirectories), writer) Next End Sub
Jim
-
All the information is available on MSDN. It is an excellent resource. Iterating over the subdirectories[^] Getting the files in a directory[^] FileInfo class[^] DirectoryInfo class[^] This should help. --Colin Mackay--
EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
-
This code is for VB. VB.Net is quite similar though. Set a reference to Microsoft Scripting Runtime
Dim srt As New Scripting.FileSystemObject
Dim drv As Drive
Dim fld As Folder
Dim f As FileSet drv = srt.Drives("C:\")
Set fld = drv.RootFolder
For Each f In fld.Files
Debug.Print f.Name
NextIf you want to loop through each directory as well you will need to create another function to return the folder object. To check to see if a folder has sub folder use the following code: fld.SubFolders Michael
-
This is some code I have used. It writes all files in a directory and sub directories to an xml document. The functions are recusrsive. Modify to suite your needs as appropriate.
'Create a new xml textwriter and write to a stringwriter object Dim sw As New StringWriter Dim writer As New XmlTextWriter(sw) 'Write document element writer.WriteStartElement("Files") 'Get the files here ListFoldersAndFiles(Server.MapPath("MyFolder"), writer) writer.WriteEndElement() 'Write end document element 'Clean up objects writer.Flush() writer.Close() writer = Nothing Dim xmldoc As New XmlDocument xmldoc.LoadXml(sw.ToString()) Private Sub ListFoldersAndFiles(ByVal pth As String, ByRef writer As XmlTextWriter) AddFiles(pth, writer) AddFolders(pth, writer) End Sub Public Sub AddFiles(ByVal pth As String, ByRef writer As XmlTextWriter) Dim sFiles() As String = Directory.GetFiles(pth) Dim iFiles As Integer For iFiles = 0 To UBound(sFiles) writer.WriteStartElement("Files") writer.WriteStartElement("Path") writer.WriteString(pth) writer.WriteEndElement() writer.WriteStartElement("Filename") writer.WriteString(Path.GetFileName(sFiles(iFiles))) writer.WriteEndElement() writer.WriteEndElement() Next End Sub Public Sub AddFolders(ByVal pth As String, ByRef writer As XmlTextWriter) Dim sDirectories() As String = Directory.GetDirectories(pth) Dim iDirectories As Integer Dim sDirectoryName As String For iDirectories = 0 To UBound(sDirectories) AddFolders(sDirectories(iDirectories), writer) AddFiles(sDirectories(iDirectories), writer) Next End Sub
Jim
-
There are some classes in System.IO that do the same that you are doing. The problem is to loop over all directories with this classes. I will try to do a loop with your code. Thanks again!!!
Here is something I wrote a few years back in VB6 to simple create a text file of all of my MP3s. It doesn't filter them by *.MP3 since that is all that is in that particular shared drive but you get the idea. I haven't had to mess around with the system.io yet. But the below does work. Give it a try.
Dim fso As New FileSystemObject
Dim List As TextStreamConst C_PLUS As String = "+ "
Dim i As Integer ' IndentPrivate Sub Main()
Dim d As FolderSet d = fso.GetFolder("N:\")
Set List = fso.CreateTextFile(d.Path & "NewSongList.txt", True)DoFolders d
List.CloseEnd Sub
Private Sub DoFolders(fldr As Folder)
Dim d As Folder
If i = 0 Then
List.WriteLine Space(i) & C_PLUS & fldr.Path
Else
List.WriteLine Space(i) & C_PLUS & fldr.Name
End If
i = i + 2
DoFiles fldr
For Each d In fldr.SubFolders
DoFolders d
Next
i = i - 2
End SubPrivate Sub DoFiles(fldr As Folder)
Dim f As FileFor Each f In fldr.Files
List.WriteLine Space(i + 2) & f.Name
Next
End SubMichael
-
Here is something I wrote a few years back in VB6 to simple create a text file of all of my MP3s. It doesn't filter them by *.MP3 since that is all that is in that particular shared drive but you get the idea. I haven't had to mess around with the system.io yet. But the below does work. Give it a try.
Dim fso As New FileSystemObject
Dim List As TextStreamConst C_PLUS As String = "+ "
Dim i As Integer ' IndentPrivate Sub Main()
Dim d As FolderSet d = fso.GetFolder("N:\")
Set List = fso.CreateTextFile(d.Path & "NewSongList.txt", True)DoFolders d
List.CloseEnd Sub
Private Sub DoFolders(fldr As Folder)
Dim d As Folder
If i = 0 Then
List.WriteLine Space(i) & C_PLUS & fldr.Path
Else
List.WriteLine Space(i) & C_PLUS & fldr.Name
End If
i = i + 2
DoFiles fldr
For Each d In fldr.SubFolders
DoFolders d
Next
i = i - 2
End SubPrivate Sub DoFiles(fldr As Folder)
Dim f As FileFor Each f In fldr.Files
List.WriteLine Space(i + 2) & f.Name
Next
End SubMichael
-
Hi, I have already seen some these web pages, but by problem is to look at all subdirectories, I am going to make a kind of loop over each directory. I hope that it help me. Thanks, you helped me a lot too.
ltt19 wrote: but by problem is to look at all subdirectories, I am going to make a kind of loop over each directory That sounds about right. For each directory you then have to loop over the subdirectories, for each of the subdirectories you have to loop over the subdirectories, and so on... --Colin Mackay--
EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
-
Hi, Does anybody knows how to get all driections and subdirectoires of a hdd? I took a look at System.IO.IsolatedStorage, but I could not specify the hdd. If someone has any ideas.... Thanks
I did it, it works, for me, the code is above, if you want to make any changes... Thanks for everybody who helped me. Here is the code, using a ListBox named as 'lst'
Dim folders As New System.Collections.Specialized.StringCollection Private Function GetAllDirectories(ByVal Name As String) On Error Resume Next Dim finder As System.IO.Directory Dim cdir As New System.Collections.Specialized.StringCollection Dim cdir2 As New System.Collections.Specialized.StringCollection Dim cancel As Boolean = False Dim dir As String cdir.AddRange(finder.GetDirectories(Name)) folders.AddRange(finder.GetDirectories(Name)) Do While cancel = False For Each dir In cdir folders.AddRange(finder.GetDirectories(dir)) cdir2.AddRange(finder.GetDirectories(dir)) Next If cdir2.Count = 0 Then cdir = Nothing cdir2 = Nothing Dim i As Integer For i = 0 To folders.Count lst.Items.Add(folders(i).ToString) Next cancel = True Exit Do Else cdir.Clear() Dim y As Integer For y = 0 To cdir2.Count cdir.Add(cdir2(y)) Next cdir2.Clear() dir = Nothing y = Nothing End If Loop MessageBox.Show(lst.Items.Count) End Function Private Function GetAllFiles() On error resume next Dim files As System.IO.Directory Dim dir As String For Each dir In folders lst.Items.AddRange(files.GetFiles(dir)) Next End Function