|
-
Sep 21st, 2010, 04:48 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Looping through SubFolders
Hey guys.
What I have to do is to search the whole system for any Mp3 files.
My Problem is to loop through all the subfolders.
What I managed to do:
Code:
Imports System.IO
Imports System.Runtime.InteropServices
Public Class Form1
Private Structure SHFILEINFO
Public hIcon As IntPtr ' : icon
Public iIcon As Integer ' : icondex
Public dwAttributes As Integer ' : SFGAO_ flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" _
(ByVal pszPath As String, _
ByVal dwFileAttributes As Integer, _
ByRef psfi As SHFILEINFO, _
ByVal cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr
Private Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_LARGEICON = &H0 ' Large icon
Private Const MAX_PATH = 260
Private Sub AddImages(ByVal strFileName As String)
Dim shInfo As SHFILEINFO
shInfo = New SHFILEINFO()
shInfo.szDisplayName = New String(vbNullChar, MAX_PATH)
shInfo.szTypeName = New String(vbNullChar, 80)
Dim hIcon As IntPtr
hIcon = SHGetFileInfo(strFileName, 0, shInfo, Marshal.SizeOf(shInfo), SHGFI_ICON Or SHGFI_SMALLICON)
Dim MyIcon As Drawing.Bitmap
MyIcon = Drawing.Icon.FromHandle(shInfo.hIcon).ToBitmap
ImageList1.Images.Add(strFileName.ToString(), MyIcon)
ImageList1.TransparentColor = Color.Black
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Folders As New List(Of String)
Dim SubFolders As New List(Of String)
Folders.AddRange(Directory.GetDirectories("C:\"))
GetFiles("C:\")
For a = 0 To Folders.Count - 1
GetFiles(Folders(a))
Try
Dim Gi As IO.DirectoryInfo
Dim Di As New DirectoryInfo(Folders(a))
For Each Gi In Di.GetDirectories
SubFolders.Add(Gi.FullName)
GetFiles(Gi.FullName)
Next
Catch ex As Exception
End Try
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListView1.View = View.Details
ListView1.Columns.Add("File Name", 190, HorizontalAlignment.Left)
ListView1.Columns.Add("File Type", 50, HorizontalAlignment.Left)
ListView1.Columns.Add("Last Accessed", 150, HorizontalAlignment.Left)
End Sub
Private Sub GetFiles(ByVal Fpath As String)
Dim Di As New DirectoryInfo(Fpath)
Dim Index As Integer = 0
Dim Fi As IO.FileInfo
Try
For Each Fi In Di.GetFiles
If Path.GetExtension(Fi.FullName).ToUpper = ".MP3" Or Path.GetExtension(Fi.FullName).ToUpper = ".wav" Then
AddImages(Fi.FullName)
ListView1.Items.Add(Fi.FullName, Index)
ListView1.Items(Index).SubItems.Add(Fi.Extension)
ListView1.Items(Index).SubItems.Add(Fi.LastAccessTime)
Index += 1
End If
Next
Catch ex As Exception
End Try
End Sub
End Class
I know there is some easy way to loop through all the sub folders but im not seeing it.
Thanks
-
Sep 21st, 2010, 05:08 AM
#2
Re: Looping through SubFolders
Last edited by amrita; Sep 21st, 2010 at 05:20 AM.
thanks
amrita
-
Sep 21st, 2010, 05:13 AM
#3
Thread Starter
Hyperactive Member
Re: Looping through SubFolders
Sorry but the link is incorrect 
Im thinking about using Classes but i think there is a much easier way.
Thanks
-
Sep 21st, 2010, 05:30 AM
#4
Re: Looping through SubFolders
-
Sep 21st, 2010, 05:31 AM
#5
Re: Looping through SubFolders
I'm not sure why that url is coming like this...the previous one.
-
Sep 21st, 2010, 06:30 AM
#6
Thread Starter
Hyperactive Member
Re: Looping through SubFolders
Thats but it has nothing to do with what I have in mind. My aim is to list all my mp3 files in the system. thanks
-
Sep 21st, 2010, 08:27 AM
#7
Re: Looping through SubFolders
I would use a BackgroundWorker for the actual scanning of the hard drive(s) on the system for better performance. I would start with looping the available drives on the system & for each fixed drive I would call a sub routine that would recursively search that "base" folder looking at each file and if it's extension is one you want, report it to the UI thread where it gets added to the LV on the form (or whatever you want it to show in) if it's a folder, then re-call that sub passing the sub folder. I would simply use a List(Of String) to hold the file extension types you want to allow, so each file can be checked against the list.
Code:
Imports System.IO
Public Class Form1
Private m_ExtensionList As List(Of String)
Private WithEvents m_BW As System.ComponentModel.BackgroundWorker
Private m_FilesFound As Integer
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If m_BW.IsBusy Then m_BW.CancelAsync() 'Cancel if busy
m_BW.Dispose() 'Clean up the BW object
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Initialize controls
m_BW = New System.ComponentModel.BackgroundWorker With {.WorkerReportsProgress = True, .WorkerSupportsCancellation = True}
m_ExtensionList = New List(Of String)
m_ExtensionList.AddRange(New String() {"mp3"})
End Sub
Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartButton.Click
'User clicked the start button on the form
ListView1.Items.Clear()
m_FilesFound = 0I
'Start the HDD scan(s)
m_BW.RunWorkerAsync()
End Sub
Private Sub m_BW_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles m_BW.DoWork
For Each Drive As DriveInfo In My.Computer.FileSystem.Drives
'Loop through each drive on the system
If Not m_BW.CancellationPending Then 'Make sure a cancelation isn't pending
If Drive.DriveType = DriveType.Fixed Then
'It's a hard drive
Call ProcFolder(Drive.Name)
End If
End If
Next Drive
End Sub
Private Sub m_BW_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles m_BW.ProgressChanged
'Add the items to the LV on the form
ListView1.Items.Add(New ListViewItem(CType(e.UserState, String()), -1I))
End Sub
Private Sub m_BW_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles m_BW.RunWorkerCompleted
MessageBox.Show(String.Format("Scan completed{0}Files Found: {1}", Environment.NewLine, m_FilesFound))
End Sub
Private Sub ProcFolder(ByVal Fldr As String)
For Each Item As String In Directory.GetFileSystemEntries(Fldr)
If Not m_BW.CancellationPending Then 'Make sure a cancelation isn't pending
If Directory.Exists(Item) Then
Try
'Recursive call
Call ProcFolder(Item)
Catch : End Try
Else
'Is a file
If m_ExtensionList.Contains(IO.Path.GetExtension(Item).Replace(".", String.Empty)) Then
'Is an MP3 or other type of music file
m_FilesFound += 1I
'Increase the counter and report it to the LV on the form
m_BW.ReportProgress(0I, New String() {IO.Path.GetFileName(Item), IO.Path.GetDirectoryName(Item)})
End If
End If
End If
Next Item
End Sub
End Class
-
Sep 21st, 2010, 09:23 AM
#8
Thread Starter
Hyperactive Member
Re: Looping through SubFolders
Wow
Yes I was going to use a backround thread but I really learnt alot from this.
Thanks alot
-
Sep 21st, 2010, 09:50 AM
#9
Re: Looping through SubFolders
 Originally Posted by Skatebone
Thats but it has nothing to do with what I have in mind. My aim is to list all my mp3 files in the system. thanks
The links shows the iterrative method of seraching through sub folders. I believe the issue was to loop through folders
-
Sep 21st, 2010, 10:17 AM
#10
Re: Looping through SubFolders
 Originally Posted by amrita
The links shows the iterrative method of seraching through sub folders. I believe the issue was to loop through folders 
The link uses a recursive call to loop the sub folders, like my example shows. The difference is in the example the files in the current folder are checked first and in my example a file or a folder may come first, but still the methodology is the same.
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
|