[2008] read a directory tree
is there an easy way to read the directory tree? I would like to be able to do this:
a user picks a number
i dump into a text file the directory tree, but only read as many levels deep as the number entered.
example:
a path on disk: c:\windows\system32\drivers\cabs
user enters "3"
this is returned:
c:\
c:\windows
c:\windows\system32
I have made this work in vb6.0 but i have no idea at all where to even begin in .net.
Other storage ideas for the read path can be considered. It's for a program i am converting to .net that indexes cds. It contains a link to read the directory tree.
Re: [2008] read a directory tree
System.IO.Files and System.IO.Directory is what you are looking for....
-tg
Re: [2008] read a directory tree
vb Code:
dim path as string = "c:\windows\system32\drivers\cabs"
dim depth as integer = 3
dim tempArray() as string = path.split("\")
dim newPath as string
for x as integer = 1 to depth
newPath &= tempArray(x-1) & "\"
next
or
io.Directory.GetParent
Re: [2008] read a directory tree
Directory.GetParent() would be like this:
Code:
Imports System.IO
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Dim depth As Integer = CInt(InputBox("Enter a number"))
Dim path As String = "C:\WINDOWS\system32\drivers\cabs"
Dim requestedPath As String = ""
Dim parentDirectories As New List(Of String)
parentDirectories.Add(Directories.GetParent(path))
For i As Integer = 0 To depth - 2
parentDirectories.Add(Directories.GetParent(parentDirectories(i)))
Next i
requestedPath = parentDirectories(depth-2)
End Sub
something along that lines, some of the code may be slightly wrong...
Cheers
Re: [2008] read a directory tree
i might be able to modify my vb6 code to use system.io.files and system.io.directory. I will look into this. I was hoping someone would have better code than the mess i came up with though: I wrote a recursive function that calls itself (duh) but knows how deep in the calling it is and exits if it is too deep. The problem is i use dir$ and it doesn't let you tell it where to start in a particular directory, so reading a subdirectory's contents would force me to re-read the current directory to get the name of the next directory.
Re: [2008] read a directory tree
Take a look at the CodeBank entry by <ABC
It is complete recursive file search, you could adapt this.
http://www.vbforums.com/showthread.php?t=341919
Re: [2008] read a directory tree
Look at the GetDirectories method of the Directory class.... it allows you to specify the directory to look in (so it doesn't even need to be the current directory), and to apply a search criteria...
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Dirs() As String
Dirs = System.IO.Directory.GetDirectories("C:\Windows", "*System*")
Dim dir As String
For Each dir In Dirs
MessageBox.Show(dir)
Next
End Sub
-tg
Re: [2008] read a directory tree
phill64, i checked out that code bank entry. I wish it was commented better. I don't understand it at all, thanks to the fact it's a class. I have never programmed a class before :( Also i can't cut and paste the code since they used vbcode tags instead of the standard code ones.
Re: [2008] read a directory tree
Quote:
Originally Posted by Lord Orwell
phill64, i checked out that code bank entry. I wish it was commented better. I don't understand it at all, thanks to the fact it's a class. I have never programmed a class before :( Also i can't cut and paste the code since they used vbcode tags instead of the standard code ones.
Of course you have. Every form you've designed is a class. A class is just a type. If you want to use it you first create an instance, then access its members. Isn't that what you do with forms? If you place a method in a form of yours then it's just as much in a class as any other.
That CodeBank submission seems to be excessively complex for most purposes. Here's some more basic code that will visit every accessible file under whatever folder you specify:
vb.net Code:
Private Sub VisitAllFiles(ByVal folder As String)
Try
For Each file As String In IO.Directory.GetFiles(folder)
Console.WriteLine(file)
Next file
For Each subfolder As String In IO.Directory.GetDirectories(folder)
Me.VisitAllFiles(subfolder)
Next subfolder
Catch ex As Exception
Console.WriteLine("Folder inaccessible: " & folder)
End Try
End Sub
Obviously you can replace Console.WriteLine with whatever you want to do to the file. You can also specify a filter when calling GetFiles if you want.
Re: [2008] read a directory tree
Quote:
Originally Posted by Lord Orwell
phill64, i checked out that code bank entry. I wish it was commented better. I don't understand it at all, thanks to the fact it's a class. I have never programmed a class before :( Also i can't cut and paste the code since they used vbcode tags instead of the standard code ones.
copy the code + paste it into wordpad. then you can select it in wordpad + paste it into your project
Re: [2008] read a directory tree
when i did that it copied the line numbers.
Thanks JMC, I will give that a shot when i get off of work later and let you know if i have any issues with it :)