Results 1 to 11 of 11

Thread: [2008] read a directory tree

  1. #1

    Thread Starter
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    [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.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2008] read a directory tree

    System.IO.Files and System.IO.Directory is what you are looking for....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2008] read a directory tree

    vb Code:
    1. dim path as string = "c:\windows\system32\drivers\cabs"
    2. dim depth as integer = 3
    3. dim tempArray() as string = path.split("\")
    4.  
    5. dim newPath as string
    6. for x as integer = 1 to depth
    7.     newPath &= tempArray(x-1) & "\"
    8. next

    or

    io.Directory.GetParent

  4. #4
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    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

  5. #5

    Thread Starter
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    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.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  6. #6
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    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

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    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.
    Last edited by Lord Orwell; Dec 15th, 2007 at 08:45 PM.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Sub VisitAllFiles(ByVal folder As String)
    2.     Try
    3.         For Each file As String In IO.Directory.GetFiles(folder)
    4.             Console.WriteLine(file)
    5.         Next file
    6.  
    7.         For Each subfolder As String In IO.Directory.GetDirectories(folder)
    8.             Me.VisitAllFiles(subfolder)
    9.         Next subfolder
    10.     Catch ex As Exception
    11.         Console.WriteLine("Folder inaccessible: " & folder)
    12.     End Try
    13. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    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

  11. #11

    Thread Starter
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    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
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width