|
-
Oct 7th, 2003, 03:28 PM
#1
Thread Starter
Lively Member
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
-
Oct 7th, 2003, 03:48 PM
#2
Frenzied Member
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
-
Oct 7th, 2003, 03:55 PM
#3
Thread Starter
Lively Member
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
-
Oct 7th, 2003, 07:26 PM
#4
Frenzied Member
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Cursor = Cursors.WaitCursor
Dim strwri As New StreamWriter("c:\dir.txt")
getdir("D:\", ".mp*", strwri)
strwri.Close()
Me.Cursor = Cursors.Default
End Sub
Private Sub getdir(ByVal rootdir As String, ByVal ext As String, ByVal sw As StreamWriter)
Try
Dim maindir As New DirectoryInfo(rootdir)
Dim allfiles As FileSystemInfo, fileext, dirs As String
For Each allfiles In maindir.GetFileSystemInfos()
If TypeOf allfiles Is DirectoryInfo Then
getdir(allfiles.FullName, ext, sw)
Else
fileext = allfiles.Extension
If System.Text.RegularExpressions.Regex.IsMatch(fileext, ext.Replace("*", "")) Then
dirs += " " & allfiles.Name + Environment.NewLine
End If
End If
Next
If dirs.Length > 0 Then
sw.WriteLine(CType(allfiles, FileInfo).Directory.FullName)
sw.WriteLine(dirs)
End If
Catch
End Try
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
-
Oct 7th, 2003, 09:55 PM
#5
Thread Starter
Lively Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|