VS 2010 Recursive search hard drive for file types-VBForums
Results 1 to 4 of 4

Thread: Recursive search hard drive for file types

  1. #1

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    209

    Recursive search hard drive for file types

    Hello i am a bit new to vb.net i used to code in vb6 and realbasic for years but went to vb.net. I need to some help on scanning a whole hard drive for multiple with a array of file types if you guys can help me would be awesome thanks!


    Code:
    Public Class Form1
        Public Function ListAllDrives() As String
            Dim allDrives() As IO.DriveInfo = IO.DriveInfo.GetDrives()
            Dim d As IO.DriveInfo
            For Each d In allDrives
                Return d.Name
            Next
        End Function
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ComboBox1.Items.Add(ListAllDrives)
            ComboBox1.SelectedIndex = 0
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim lstResult As New List(Of String)
            Dim stkStack As New Stack(Of String)
            stkStack.Push(ComboBox1.text)
    
            Do While (stkStack.Count > 0)
                Dim strDirectory As String = stkStack.Pop
                Try
                    lstResult.AddRange(IO.Directory.GetFiles(strDirectory, "*.png"))
                    Dim strDirectoryName As String
                    For Each strDirectoryName In IO.Directory.GetDirectories(strDirectory)
                        stkStack.Push(strDirectoryName)
                        Label1.Text = strDirectoryName
    
                    Next
                Catch ex As UnauthorizedAccessException
                    MsgBox("file in use")
                End Try
            Loop
        End Sub
    End Class

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,517

    Re: Recursive search hard drive for file types

    Assuming your code works for .png files, simply creating an array and putting a loop around the lstResult.AddRange line should do it, eg:
    Code:
            stkStack.Push(ComboBox1.text)
            Dim fileTypesToFind as String() = {"*.png", "*.jpg", "*.gif"}
    
            Do While (stkStack.Count > 0)
                Dim strDirectory As String = stkStack.Pop
                Try
                    For Each fileType as String in fileTypesToFind 
                        lstResult.AddRange(IO.Directory.GetFiles(strDirectory, fileType))
                    Next
                    Dim strDirectoryName As String

  3. #3

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    209

    Re: Recursive search hard drive for file types

    that code above works just the way i want how can i get folder paths like folders other then special folders want to get the windows prefetch folder any ideas what im doing wrong?

    Code:
     Dim Prefetch As String = (Environment.GetFolderPath("%systemroot%\prefetch"))
    Last edited by sinner0636; Dec 18th, 2017 at 06:32 PM.

  4. #4

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    209

    Re: Recursive search hard drive for file types

    nm figured it out

    Code:
      Dim Prefetch As String = (Environment.GetFolderPath(Environment.SpecialFolder.Windows)) + "\Prefetch"
            MsgBox(Prefetch)

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.