1

I want to list all JPG files from all drives in a computer with there full path. So I tried following code but it only list few random files, it wont search all files. i got this from here: Searching a drive with vb.net

Public Sub DirSearch(ByVal sDir As String)
    Dim fl As String

    Try
        For Each dir As String In Directory.GetDirectories(sDir)
            For Each fl In Directory.GetFiles(dir, "*.jpg")
                listbox1.Items.Add(fl)

            Next

            DirSearch(dir)
        Next


    Catch ex As Exception
        Debug.WriteLine(ex.Message)
    End Try
End Sub

'form1 load event

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    DirSearch("c:\")
    DirSearch("d:\")
    DirSearch("e:\")
    DirSearch("f:\")
    DirSearch("g:\")
    DirSearch("h:\")
    DirSearch("i:\")
    DirSearch("j:\")
    DirSearch("k:\")
    'DirSearch("so on.....")
    savetxtfile()

   End Sub

Save searched result to text file in system drive

Sub savetxtfile()
    Dim systemdrv As String = Mid(Environment.GetFolderPath(Environment.SpecialFolder.System), 1, 3)


    TextBox1.Text = listbox1.Items.Count
    Dim w As IO.StreamWriter
    Dim r As IO.StreamReader

    Dim i As Integer
    w = New IO.StreamWriter(systemdrv + "temp\test.txt")
    For i = 0 To listbox1.Items.Count - 1
        w.WriteLine(listbox1.Items.Item(i))
    Next
    w.Close()
End Sub
Community
  • 1
  • 1

1 Answers1

0

You're ignoring your exception...

Debug.WriteLine(ex.Message)

Use this instead (so you can't miss it for debugging)...

MessageBox.Show(ex.Message)

Knowing that what's happening is probably a folder access error, you need to handle that accordingly.

For Each dir As String In Directory.GetDirectories(sDir)
    Try
        For Each fl In Directory.GetFiles(dir, "*.jpg")
            lstbxTest.Items.Add(fl)
        Next
    Catch ex As Exception
        Continue For
    End Try
Next

Effectively, continue if you get an error, because you don't really care in this case.

You might want to add some exceptions in there (If dir = "whatever" Then Continue For) so you're not trying to go through every single system folder in operating system.

Keith
  • 1,331
  • 2
  • 13
  • 18
  • It is inside a `For Loop`. You need to check your code. I changed mine (yours) so that it would catch errors but ignore them and `Continue For`. – Keith Jul 01 '15 at 18:32