Posted By

martinbrait on 08/10/19


Tagged


Versions (?)

[vba-excel] lister fichiers d'un type particulier de dossier et sous-dossier


 / Published in: Visual Basic
 

URL: list files type directories

How to list files with type, from directories and sub-directories ?

  1. Sub ListeFichiersAll()
  2. TousLesDossiers "C:\Temp\", 1
  3. DirFichiers
  4. End Sub
  5. Private Sub TousLesDossiers(LeDossier$, Idx As Long)
  6. Dim Fso As Object, Dossier As Object
  7. Dim sousRep As Object, Flder As Object
  8. Set Fso = CreateObject("Scripting.FileSystemObject")
  9. Set Dossier = Fso.GetFolder(LeDossier)
  10. 'examen du dossier courant
  11. For Each Flder In Dossier.SubFolders
  12. Idx = Idx + 1
  13. Cells(Idx, 1).Value = Flder.Path
  14. Next
  15. 'traitement récursif des sous dossiers
  16. For Each sousRep In Dossier.SubFolders
  17. TousLesDossiers sousRep.Path, Idx
  18. Next sousRep
  19. Set Fso = Nothing
  20. End Sub 'fs
  21.  
  22. Private Sub DirFichiers()
  23. For Each cell In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
  24. cell.Select
  25. Fichiers
  26. Next
  27. End Sub
  28. Private Sub Fichiers()
  29. Application.ScreenUpdating = False
  30. Dim myPath As String, myFile As String
  31.  
  32. myPath = ActiveCell.Value 'ThisWorkbook.Path
  33. myFile = Dir(myPath & "\*.xls*")
  34.  
  35. c = 2
  36. Do While myFile <> ""
  37. 'Cells(c, 1) = myFile
  38. Cells(ActiveCell.Row, c) = myFile
  39. myFile = Dir()
  40. c = c + 1
  41. Loop
  42. End Sub

Report this snippet  

You need to login to post a comment.