Hello guys,
Actually I am working on a software in .NET. The software objectives is to search for all music files in a specifies directory or the whole computer and display the files names in a list box.

But the problem is with the directory. How can I used .Net to scan the whole computer or a "specific directory."

So far, I have been able to scan only special directories:

Public Class frmSpecusAnalyser

    Private Sub btnAnalyse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyse.Click
        Me.lstMusicFiles.Items.Clear()

        Dim numOfFiles As Integer = 0
        Dim foundSong As String
        Dim progValue As Integer = 0
        prgBrAnalyser.Value = 0


        For Each foundSong In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
            FileIO.SearchOption.SearchAllSubDirectories, "*.mp3*", "*.mp4*")


            Me.lstMusicFiles.Items.Add(foundSong)
            numOfFiles += 1

            If progValue <= 100 Then
                prgBrAnalyser.Value = progValue
                progValue += 1
            End If

        Next

        lblNumOfFiles.Text = numOfFiles.ToString

        If Me.lstMusicFiles.Items.Count < 1 Then
            MsgBox("Oops, no music files found!")
        End If



    End Sub



End Class

Thanks a lot in advance guys.

Regards,
Mike

I ripped my old code for searching files

Imports System.IO

''' <summary>
''' Return file names
''' </summary>
''' <param name="RootPath">The path to start searching files</param>
''' <param name="FileMask">An array of file masks</param>
''' <param name="FileNames">Argument returns fully qualified file names</param>
''' <param name="ShortFileNames">Argument returns file names only</param>
''' <param name="RecurseDirs">If True, searches subdirectories too</param>
''' <remarks></remarks>
Public Sub ToolDir(ByVal RootPath As String, ByVal FileMask() As String, _
  ByRef FileNames() As String, ByRef ShortFileNames() As String, _
  ByVal RecurseDirs As Boolean)
  '
  Dim DirInfo As DirectoryInfo
  Dim Files() As FileInfo
  Dim OneFile As FileInfo
  Dim i As Integer
  Dim j As Integer

  Try
    If FileMask.GetUpperBound(0) < 0 Then
      ReDim FileMask(0)
      FileMask(0) = "*.*"
    End If
    i = 0
    For j = 0 To FileMask.GetUpperBound(0)
      ReDim Files(0)
      If RecurseDirs Then
        DirInfo = New DirectoryInfo(RootPath)
        Files = DirInfo.GetFiles(FileMask(j), IO.SearchOption.AllDirectories)
      Else
        DirInfo = New DirectoryInfo(RootPath)
        Files = DirInfo.GetFiles(FileMask(j), IO.SearchOption.TopDirectoryOnly)
      End If

      For Each OneFile In Files
        ReDim Preserve FileNames(i)
        ReDim Preserve ShortFileNames(i)
        FileNames(i) = OneFile.FullName
        ShortFileNames(i) = OneFile.Name
        i += 1
      Next
    Next j
  Catch ex As Exception
    ' Handle errors
  End Try

End Sub

If you use this code, your code changes to something like following (didn't test this part)

Private m_FileNames() As String

Private Sub btnAnalyse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyse.Click
    '
    Dim RootPath As String
    Dim FileMask(1) As String
    Dim ShortFileNames() As String
    Dim i As Integer
    
    Me.lstMusicFiles.Items.Clear()
    
    ' Root path comes from user selected folder?
    RootPath = "C:\" ' Hard coded to scan the whole C drive
    FileMask(0) = "*.mp3"
    FileMask(1) = "*.mp4"
    ReDim m_FileNames(0)
    ReDim ShortFileNames(0)
    
    ToolDir(RootPath, FileMask, m_FileNames, ShortFileNames, True)

    If String.IsNullOrEmpty(m_FileNames(0)) Then
      lblNumOfFiles.Text = "0"
      MsgBox("Oops, no music files found!")
      Exit Sub
    Else
      lblNumOfFiles.Text = (m_FileNames.GetUpperBound(0) + 1).ToString
    End If
    
    For i = 0 To ShortFileNames.GetUpperBound(0)
        Me.lstMusicFiles.Items.Add(ShortFileNames(i))
    Next i

End Sub

m_FileNames() is declared at class level and list box shows file names without path part. When an item from the list box is selected, item's index refers to m_FileNames() array which has the fully qualified file name (i.e. with path). If you want to show path names in the list box, change Me.lstMusicFiles.Items.Add(ShortFileNames(i)) to Me.lstMusicFiles.Items.Add(m_FileNames(i)) .

My original ToolDir code passed a reference to a progress bar. I ripped it off, but you can add ByRef pgrProgress As ProgressBar argument to procedure and pass progValue as parameter. Only tricky part is to determine Maximum property value.

HTH

Thanks for the answer Teme64, I will try it and let you know.

I have This function from my framework, and sorry I cannot remember from where I got it, so I am saying that this function is not mine.

I wonder if I did not say the above and owner will came and say "Hey, give me my 10$" :)

Dim RootDir As New IO.DirectoryInfo("F:\Games")
Search(RootDir)
Sub Search(ByVal RootDir As IO.DirectoryInfo)
    For Each File In RootDir.GetFiles("*.mp3")
        Debug.Print(File.FullName) 'process the file
    Next
    For Each SubDir In RootDir.GetDirectories()
        Search(SubDir)
    Next
End Sub

I had tested this function, and it is really VERY fast finding files, but I did not find a way to search for *.mp3 and *.wav at the same time yet.

If I found it, i will post it, if someone knows how, post it as well.

I had tested this function, and it is really VERY fast finding files, but I did not find a way to search for *.mp3 and *.wav at the same time yet.

Yes, the fastest method I've seen. You have basically same approach as my code. Just two main differences. I don't use recursion, GetFiles method can handle sub directories ( DirInfo.GetFiles(FileMask(j), IO.SearchOption.AllDirectories) ) as well as just given root directory ( DirInfo.GetFiles(FileMask(j), IO.SearchOption.TopDirectoryOnly) ).

If I found it, i will post it, if someone knows how, post it as well.

I pass multiple file masks in an array ( ByVal FileMask() As String ) and loop each file mask.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.