Public Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean, TestRetour)
'exp: ListFilesInFolder "C:\FolderName\", True < avec sous dossiers) !!! récursivité (***)
Dim FSO, SourceFolder, SubFolder, FileItem, R As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FSO.GetFolder(SourceFolderName)
If TestRetour = 0 Then
Cells.Clear
Columns.ColumnWidth = 10
Cells(4, 1) = "Fichier": Cells(4, 2) = "Créé": Cells(4, 3) = "Modifié": Cells(4, 4) = "Capacité": Cells(4, 5) = "Chemin"
End If
[COLOR=red]limite = 100[/COLOR]
col = 0
R = 5 'Range("A65500").End(xlUp).Row + 1
For Each FileItem In SourceFolder.Files
Cells(R, col + 1).Formula = FileItem.Name
Cells(R, col + 2).Formula = FileItem.DateCreated
Cells(R, col + 3).Formula = FileItem.DateLastModified
If FileItem.Size >= 1024 Then
Cells(R, col + 4).Formula = Int(FileItem.Size / 1024) & " Ko"
Else
Cells(R, col + 4).Formula = FileItem.Size & " Oct"
End If
ActiveSheet.Hyperlinks.Add Cells(R, col + 5), FileItem.Path ' FileItem.Name
'Cells(R, 5).Formula = SourceFolderName 'FileItem.Path
R = R + 1
If R > limite Then
col = col + 7
R = 5
Cells(4, col + 1) = "Fichier": Cells(4, col + 2) = "Créé": Cells(4, col + 3) = "Modifié": Cells(4, col + 4) = "Capacité": Cells(4, col + 5) = "Chemin"
End If
Next
If IncludeSubfolders Then
For Each SubFolder In SourceFolder.SubFolders
ListFilesInFolder SubFolder.Path, True, 1 '(***)
Next
End If
Columns.AutoFit
'On Error Resume Next
Set FileItem = Nothing
Set SourceFolder = Nothing
Set FSO = Nothing
End Sub