Results 1 to 5 of 5

Thread: DirectoryInfo questions.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    Garden Grove, CA
    Posts
    72

    DirectoryInfo questions.

    I'm in the process of making a small intranet site for use in my home. I have ripped all our CD's to MP3's and they are pretty well organized, and i'm trying to automate loading the info I need into a SQL database.

    Directory structure is like this

    Code:
    Music
     |----Artist - AlbumName
     |     |--01 - SongName.mp3
     |----Artist - AlbumName
     |     |--01 - SongName.mp3
     |----Etc...
    But some of my CD's are multiple disc sets so the structure is like this:

    Code:
    Music
    Music
     |----Artist - AlbumName
     |     |--01 - SongName.mp3
     |----Artist - AlbumName
     |     |--CD1
     |     |    |--01 - SongName.mp3
     |     |--CD2
     |     |    |--01 - SongName.mp3
     |----Etc...
    Currently I have code using IO.DirectoryInfo & IO.FileInfo pulling the information, but it doesn't work when it comes to the multiple CD ones. My current code reads it to the screen correctly and my database code works perfectly for inserting. What I need is to read sub directories under the current directory. Whats the best way to do that?

    Code:
      Sub getDirs()
            Dim Dir1 As IO.DirectoryInfo = New IO.DirectoryInfo("\\pvr\c\music")
            Dim dirs As IO.DirectoryInfo() = Dir1.GetDirectories("*")
            Dim diNext As IO.DirectoryInfo
            Dim files As IO.FileInfo()
            Dim filename As IO.FileInfo
    
                For Each diNext In dirs
                    Console.WriteLine(diNext.Name)
                    files = diNext.GetFiles("*")
                    For Each filename In files
                        Console.WriteLine("      " & filename.Name)
                    Next
                    Console.ReadLine()
                 Next
        End Sub
    Chris Wilson

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Make it recursive, that is it calls the sub with in itself.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    Garden Grove, CA
    Posts
    72
    Originally posted by Lunatic3
    Make it recursive, that is it calls the sub with in itself.

    Good Idea. I've been out of programming for about a year so I'm a bit rusty. Here is my first pass at a non-recursive way of doing it.... It works, but I know it can be optimized a ton.

    Code:
     Function ReadDirectories() As Boolean
            'declarations
            Dim MainDirectory As IO.DirectoryInfo = New IO.DirectoryInfo(strRootDirectory)
            Dim DirectoryList As IO.DirectoryInfo() = MainDirectory.GetDirectories("*")
            Dim DiNext As IO.DirectoryInfo
    
            Dim DirectoryChildRoot As IO.DirectoryInfo
            Dim DirectoryChild As IO.DirectoryInfo()
            Dim DiNextChild As IO.DirectoryInfo
    
            Dim Files As IO.FileInfo()
            Dim FileName As IO.FileInfo
    
            Try
                For Each DiNext In DirectoryList
                    If DiNext.GetDirectories("*").Length > 0 Then
                        DirectoryChildRoot = New IO.DirectoryInfo(DiNext.FullName)
                        DirectoryChild = DirectoryChildRoot.GetDirectories("*")
                        For Each DiNextChild In DirectoryChild
                            Files = DiNextChild.GetFiles
                            For Each FileName In Files
                                Console.WriteLine(FileName.FullName)
                            Next
                        Next
                    End If
                    Files = DiNext.GetFiles
                    For Each FileName In Files
                        'write files
                    Next
                Next
                Return True
            Catch ex As Exception
                HandleError(ex)
                Return False
            End Try
    
        End Function
    Chris Wilson

  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I have written this sample code, it may help you and may need some tweaks. What it does is getting all folders and files of a directory matching especial extension and writing the results to a text file.
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Me.Cursor = Cursors.WaitCursor
    3.         Dim strwri As New StreamWriter("c:\dir.txt")
    4.         getdir("D:\", ".mp*", strwri)
    5.         strwri.Close()
    6.         Me.Cursor = Cursors.Default
    7. End Sub
    8. Private Sub getdir(ByVal rootdir As String, ByVal ext As String, ByVal sw As StreamWriter)
    9.         Try
    10.             Dim maindir As New DirectoryInfo(rootdir)
    11.             Dim allfiles As FileSystemInfo, fileext, dirs As String
    12.             For Each allfiles In maindir.GetFileSystemInfos()
    13.                 If TypeOf allfiles Is DirectoryInfo Then
    14.                     getdir(allfiles.FullName, ext, sw)
    15.                 Else
    16.                     fileext = allfiles.Extension
    17.                     If System.Text.RegularExpressions.Regex.IsMatch(fileext, ext.Replace("*", "")) Then
    18.                         dirs += "   " & allfiles.Name + Environment.NewLine
    19.                     End If
    20.                 End If
    21.             Next
    22.             If dirs.Length > 0 Then
    23.                 sw.WriteLine(CType(allfiles, FileInfo).Directory.FullName)
    24.                 sw.WriteLine(dirs)
    25.             End If
    26.         Catch
    27.         End Try
    28. End Sub

    BTW, you should import System.IO
    Last edited by Lunatic3; Oct 7th, 2003 at 07:33 PM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    Garden Grove, CA
    Posts
    72
    Thanks for the help. I'll have to play with the code, tear it apart and put it back together.

    I need to learn vb.net I could of done this in about 10min in VB6 :P
    Chris Wilson

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