0

Directory.GetFiles - SearchOption.AllDirectories Does not give files that are located in subfolders. I'm trying to copy al .jpg files from a usb to the computer but when files are located in sub-folders, it does not copy them.

Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
    Dim sourceDir As String = ComboBoxUsbSelect.Text
    Dim backupDir As String = FileDestination

    Try

        Dim picList As String() = Directory.GetFiles(sourceDir, "*.jpg", SearchOption.AllDirectories)

        ' Copy picture files. 
        For Each f As String In picList
            'Remove path from the file name. 
            Dim fName As String = f.Substring(sourceDir.Length)

            ' Use the Path.Combine method to safely append the file name to the path. 
            ' Will overwrite if the destination file already exists.
            If delete = False Then
                File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), True)
            Else
                File.Move(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName))
            End If
        Next
    Catch dirNotFound As DirectoryNotFoundException
        Console.WriteLine(dirNotFound.Message)
    End Try
    MessageBox.Show("Done!")
End Sub
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143

0 Answers0