Autres Gestion_Fichiers

  • Initiateur de la discussion Initiateur de la discussion VIARD
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

re,

deuxième petite modif proposée, on passe par des tableaux vb plutôt que d'écrire dans chaque cellule, c'est beaucoup plus rapide sur un grand nombre de fichiers.

Bien cordialement, @+
VB:
'===========================
Sub Liste_des_Fichiers()
Dim Msg$, Directory$, Nb%
Dim i As Long, Ext$, Ex$
Dim Dossier As Object
Dim Fichier As Object
Dim Tablo(), Tablo2()

    Msg = "Sélectionnez un emplacement contenant les fichiers que vous souhaitez lister."
    Directory = GetDirectory(Msg)
    If Directory = "" Then Exit Sub
    If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
'------- Insérer en-têtes ------
    i = 2
    Range("A:D").ClearContents
    Cells(i, 1) = "Nom de fichier"
    Cells(i, 2) = "Taille"
    Cells(i, 3) = "Date"
    Range("A1:D2").Font.Bold = True: Range("C:C").ColumnWidth = 16 ': Exit Sub
    Range("A1:D2").HorizontalAlignment = xlCenter
    Range("A2:D2").Interior.ColorIndex = 6: Range("A1").Interior.ColorIndex = 44
    'On Error Resume Next
    Set Dossier = CreateObject("Scripting.FileSystemObject").GetFolder(Directory)
'--------- Choix Extention --------
Ex = InputBox("Choix extention :" & Chr(10) & ".xls, .doc, .PDF, .txt, .jpg, .avi, .zip, .gif" _
& Chr(10) & ".bmp, .ico, .mid, .mp3, .wma, .xlsm, .xlsx, .docx" _
& Chr(10) & "Ne pas oublier le point" _
& Chr(10) & "Pour tout lister extention vide --> valider directement", "EXTENTION")
'------- Affichage Fichiers -------
       
        i = 0
        For Each Fichier In Dossier.Files
            If Fichier.Name Like "*" & Ex Then
                i = i + 1
                ReDim Preserve Tablo(1 To 3, 1 To i)
                Tablo(1, i) = Fichier.Name: Tablo(2, i) = Fichier.Size: Tablo(3, i) = Fichier.datecreated
            End If
            Cells(2, 4) = "Q = " & i - 2
        Next Fichier
        ReDim Tablo2(LBound(Tablo, 2) To UBound(Tablo, 2), 1 To 3)
        For i = LBound(Tablo, 2) To UBound(Tablo, 2)
            Tablo2(i, 1) = Tablo(1, i): Tablo2(i, 2) = Tablo(2, i): Tablo2(i, 3) = Tablo(3, i)
        Next i
        Range("A3:C" & 3 + UBound(Tablo, 2) - LBound(Tablo, 2)).Value = Tablo2
        Range("A1").Value = Directory
        Columns("A:C").EntireColumn.AutoFit
End Sub
'============================
 
Dernière édition:
Re,

pour le fun, une variante plus rapide encore avec transpose mais limitée à 65536 fichiers

Bien cordialement, @+
Code:
'===========================
Sub Liste_des_Fichiers()
Dim Msg$, Directory$, Nb%
Dim i As Long, Ext$, Ex$
Dim Dossier As Object
Dim Fichier As Object
Dim Tablo()

    Msg = "Sélectionnez un emplacement contenant les fichiers que vous souhaitez lister."
    Directory = GetDirectory(Msg)
    If Directory = "" Then Exit Sub
    If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
'------- Insérer en-têtes ------
    i = 2
    Range("A:D").ClearContents
    Cells(i, 1) = "Nom de fichier"
    Cells(i, 2) = "Taille"
    Cells(i, 3) = "Date"
    Range("A1:D2").Font.Bold = True: Range("C:C").ColumnWidth = 16 ': Exit Sub
    Range("A1:D2").HorizontalAlignment = xlCenter
    Range("A2:D2").Interior.ColorIndex = 6: Range("A1").Interior.ColorIndex = 44
    'On Error Resume Next
    Set Dossier = CreateObject("Scripting.FileSystemObject").GetFolder(Directory)
'--------- Choix Extention --------
Ex = InputBox("Choix extention :" & Chr(10) & ".xls, .doc, .PDF, .txt, .jpg, .avi, .zip, .gif" _
& Chr(10) & ".bmp, .ico, .mid, .mp3, .wma, .xlsm, .xlsx, .docx" _
& Chr(10) & "Ne pas oublier le point" _
& Chr(10) & "Pour tout lister extention vide --> valider directement", "EXTENTION")
'------- Affichage Fichiers -------
       
        i = 0
        For Each Fichier In Dossier.Files
            If Fichier.Name Like "*" & Ex Then
                i = i + 1
                ReDim Preserve Tablo(1 To 3, 1 To i)
                Tablo(1, i) = Fichier.Name: Tablo(2, i) = Fichier.Size: Tablo(3, i) = Fichier.datecreated
            End If
            Cells(2, 4) = "Q = " & i - 2
        Next Fichier
        Range("A3:C" & 3 + UBound(Tablo, 2) - LBound(Tablo, 2)).Value = Application.Transpose(Tablo)
        Range("A1").Value = Directory
        Columns("A:C").EntireColumn.AutoFit
End Sub
'============================
 
Dernière édition:
Re,

une proposition de modif pour ajouter les types de fichiers et ajouter des filtres auto.
[édition: code modifié, mieux en variant pour les tableaux]

Bien cordialement, @+

Sans titre.jpg

Code:
'===========================
Sub Liste_des_Fichiers()
Dim Msg$, Directory$, Nb%
Dim i As Long, Ext$, Ex$
Dim Dossier As Object
Dim Fichier As Object
Dim Tablo(), Tablo2()

    Msg = "Sélectionnez un emplacement contenant les fichiers que vous souhaitez lister."
    Directory = GetDirectory(Msg)
    If Directory = "" Then Exit Sub
    If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
'------- Insérer en-têtes ------
    i = 2
    ActiveSheet.AutoFilterMode = False
    Range("A:D").ClearContents
    Cells(i, 1) = "Nom de fichier"
    Cells(i, 2) = "Taille"
    Cells(i, 3) = "Date"
    Cells(i, 4) = "Type"
    With Range("A1:E2")
        .Font.Bold = True: Range("C:C").ColumnWidth = 16 ': Exit Sub
        .HorizontalAlignment = xlCenter
        .Interior.ColorIndex = 6: Range("A1").Interior.ColorIndex = 44
    End With
    On Error Resume Next
    Set Dossier = CreateObject("Scripting.FileSystemObject").GetFolder(Directory)
'--------- Choix Extention --------
Ex = InputBox("Choix extention :" & Chr(10) & ".xls, .doc, .PDF, .txt, .jpg, .avi, .zip, .gif" _
& Chr(10) & ".bmp, .ico, .mid, .mp3, .wma, .xlsm, .xlsx, .docx" _
& Chr(10) & "Ne pas oublier le point" _
& Chr(10) & "Pour tout lister extention vide --> valider directement", "EXTENTION")
'------- Affichage Fichiers -------
       
        i = 0
        For Each Fichier In Dossier.Files
            If Fichier.Name Like "*" & Ex Then
                i = i + 1
                ReDim Preserve Tablo(1 To 4, 1 To i)
                Tablo(1, i) = Fichier.Name: Tablo(2, i) = Fichier.Size: Tablo(3, i) = Fichier.datecreated:: Tablo(4, i) = Fichier.Type
            End If
            Cells(2, 5) = "Q = " & i - 2
        Next Fichier
        ReDim Tablo2(LBound(Tablo, 2) To UBound(Tablo, 2), 1 To 4)
        For i = LBound(Tablo, 2) To UBound(Tablo, 2)
            Tablo2(i, 1) = Tablo(1, i): Tablo2(i, 2) = Tablo(2, i): Tablo2(i, 3) = Tablo(3, i): Tablo2(i, 4) = Tablo(4, i)
        Next i
        Range("A3:D" & 3 + UBound(Tablo, 2) - LBound(Tablo, 2)).Value = Tablo2
        Range("A1").Value = Directory
        Columns("A:E").EntireColumn.AutoFit
        Range("A2:D2").AutoFilter
End Sub
 

Pièces jointes

Dernière édition:
Re,

et une petite dernière pour la route, ajout de la date de dernière modification, j'utilise souvent.

Bonne journée


VB:
'===========================
Sub Liste_des_Fichiers()
Dim Msg$, Directory$, Nb%
Dim i As Long, Ext$, Ex$
Dim Dossier As Object
Dim Fichier As Object
Dim Tablo(), Tablo2()

    Msg = "Sélectionnez un emplacement contenant les fichiers que vous souhaitez lister."
    Directory = GetDirectory(Msg)
    If Directory = "" Then Exit Sub
    If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
'------- Insérer en-têtes ------
    i = 2
    ActiveSheet.AutoFilterMode = False
    Range("A:F").ClearContents
    Cells(i, 1) = "Nom de fichier"
    Cells(i, 2) = "Taille"
    Cells(i, 3) = "Date de création"
    Cells(i, 4) = "Date de dernière modification"
    Cells(i, 5) = "Type"
    With Range("A1:F2")
        .Font.Bold = True: Range("C:C").ColumnWidth = 16 ': Exit Sub
        .HorizontalAlignment = xlCenter
        .Interior.ColorIndex = 6: Range("A1").Interior.ColorIndex = 44
    End With
    On Error Resume Next
    Set Dossier = CreateObject("Scripting.FileSystemObject").GetFolder(Directory)
'--------- Choix Extention --------
Ex = InputBox("Choix extention :" & Chr(10) & ".xls, .doc, .PDF, .txt, .jpg, .avi, .zip, .gif" _
& Chr(10) & ".bmp, .ico, .mid, .mp3, .wma, .xlsm, .xlsx, .docx" _
& Chr(10) & "Ne pas oublier le point" _
& Chr(10) & "Pour tout lister extention vide --> valider directement", "EXTENTION")
'------- Affichage Fichiers -------
      
        i = 0
        For Each Fichier In Dossier.Files
            If Fichier.Name Like "*" & Ex Then
                i = i + 1
                ReDim Preserve Tablo(1 To 5, 1 To i)
                Tablo(1, i) = Fichier.Name: Tablo(2, i) = Fichier.Size: Tablo(3, i) = Fichier.datecreated:: Tablo(4, i) = Fichier.datelastmodified: Tablo(5, i) = Fichier.Type
            End If
            Cells(2, 6) = "Q = " & i - 2
        Next Fichier
        ReDim Tablo2(LBound(Tablo, 2) To UBound(Tablo, 2), 1 To 5)
        For i = LBound(Tablo, 2) To UBound(Tablo, 2)
            Tablo2(i, 1) = Tablo(1, i): Tablo2(i, 2) = Tablo(2, i): Tablo2(i, 3) = Tablo(3, i): Tablo2(i, 4) = Tablo(4, i):: Tablo2(i, 5) = Tablo(5, i)
        Next i
        Range("A3:E" & 3 + UBound(Tablo, 2) - LBound(Tablo, 2)).Value = Tablo2
        Range("A1").Value = Directory
        Range("A2:E2").AutoFilter
        Columns("A:F").EntireColumn.AutoFit
End Sub
'============================
'--> Fonction de Walkenbach
Function GetDirectory(Optional Msg) As String
    Dim bInfo As BROWSEINFO
    Dim path As String
    Dim r As Long, x As Long, pos As Integer

'---------- Dossier racine = Bureau ---------
    bInfo.pidlRoot = 0&
'------ Titre dans la boîte de dialogue -----
    If IsMissing(Msg) Then
        bInfo.lpszTitle = "Sélectionnez un dossier."
    Else
        bInfo.lpszTitle = Msg
    End If
'-------- Type de dossier à retourner -------
    bInfo.ulFlags = &H1
'------- Afficher la boîte de dialogue ------
    x = SHBrowseForFolder(bInfo)
'---------- Afficher le résultat ------------
    path = Space$(512)
    r = SHGetPathFromIDList(ByVal x, ByVal path)
    If r Then
        pos = InStr(path, Chr$(0))
        GetDirectory = Left(path, pos - 1)
    Else
        GetDirectory = ""
  End If
End Function
'==========================
 

Pièces jointes

Dernière édition:
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
0
Affichages
383
  • Question Question
Réponses
9
Affichages
930
Réponses
4
Affichages
149
  • Question Question
Réponses
5
Affichages
864
Réponses
4
Affichages
167
Réponses
24
Affichages
2 K
Réponses
3
Affichages
121
Réponses
5
Affichages
215
Retour