begin process at 2012 02 17 04:08:37
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

VBA

 > LISTER TOUS LES FICHIERS D'UN DOSSIER ET DE SES SOUS-DOSSIERS

LISTER TOUS LES FICHIERS D'UN DOSSIER ET DE SES SOUS-DOSSIERS


 Information sur la source

Note :
5,5 / 10 - par 2 personnes
5,50 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :VBA Niveau :Initié Date de création :11/07/2003 Date de mise à jour :11/07/2003 14:09:35 Vu :12 244

Auteur : papaours

Ecrire un message privé
Site perso
Commentaire sur cette source (4)
Ajouter un commentaire et/ou une note

 Description

Lister tous les fichiers d'un dossier et de ses sous-dossiers
source net

Source

  • Attribute VB_Name = "ListeFichiersEtDossiers"
  • Option Explicit
  • '32-bit API declarations
  • Declare Function SHGetPathFromIDList Lib "shell32.dll" _
  • Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
  • Declare Function SHBrowseForFolder Lib "shell32.dll" _
  • Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
  • Public Type BROWSEINFO
  • hOwner As Long
  • pidlRoot As Long
  • pszDisplayName As String
  • lpszTitle As String
  • ulFlags As Long
  • lpfn As Long
  • lParam As Long
  • iImage As Long
  • End Type
  • Sub ListFiles()
  • Dim msg As String, answer As String
  • Dim Directory As String
  • Dim R As Integer
  • Dim i As Integer
  • Dim StartDate As Single
  • msg = "Select a location containing the files you want to list."
  • Directory = GetDirectory(msg)
  • If Directory = "" Then Exit Sub
  • If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
  • ' Insert headers
  • R = ActiveCell.Row
  • Cells(R, 1) = "FilePath"
  • Cells(R, 2) = "Size"
  • Cells(R, 3) = "Date/Time"
  • Cells(R, 4) = "Filename"
  • Range("A1:C1").Font.Bold = True
  • R = R + 1
  • On Error Resume Next
  • With Application.FileSearch
  • .NewSearch
  • .LookIn = Directory
  • .Filename = "*.*" ' this can be '*.*
  • .SearchSubFolders = True
  • .Execute
  • For i = 1 To .FoundFiles.Count
  • If FileDateTime(.FoundFiles(i)) > StartDate Then
  • Cells(R, 1) = .FoundFiles(i)
  • Cells(R, 4) = Right(Cells(R, 1), Len(.FoundFiles(i)) - InStrRev(Cells(R, 1).Value, "\"))
  • Cells(R, 2) = FileLen(.FoundFiles(i))
  • Cells(R, 3) = FileDateTime(.FoundFiles(i))
  • R = R + 1
  • End If
  • Next i
  • End With
  • 'Columns("A:C").Select
  • 'Selection.Columns.AutoFit
  • 'ActiveSheet.Columns("1:3").AutoFit
  • MsgBox "file listing complete"
  • End Sub
  • Function GetDirectory(Optional msg) As String
  • Dim bInfo As BROWSEINFO
  • Dim path As String
  • Dim R As Long, x As Long, pos As Integer
  • ' Root folder = Desktop
  • bInfo.pidlRoot = 0&
  • ' Title in the dialog
  • If IsMissing(msg) Then
  • bInfo.lpszTitle = "Select a folder"
  • Else
  • bInfo.lpszTitle = msg
  • End If
  • ' Type of directory to return
  • bInfo.ulFlags = &H1
  • ' Display the dialog
  • x = SHBrowseForFolder(bInfo)
  • ' Parse the result
  • 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
  • Function InStrLast(iStart As Integer, szSrchIn As String, _
  • szSrchFor As String, iCompare As Integer) As Integer
  • Dim iPrevFoundAt As Integer
  • Dim iFoundAt As Integer
  • On Error GoTo ErrExit_InStrLast
  • iPrevFoundAt = 0
  • iFoundAt = InStr(iStart, szSrchIn, szSrchFor, iCompare)
  • Do While iFoundAt > 0
  • iPrevFoundAt = iFoundAt
  • iFoundAt = InStr(iPrevFoundAt + 1, szSrchIn, szSrchFor, iCompare)
  • Loop
  • ErrExit_InStrLast:
  • If Err <> 0 Then MsgBox Error$, vbExclamation
  • InStrLast = iPrevFoundAt
  • Exit Function
  • End Function
Attribute VB_Name = "ListeFichiersEtDossiers"


Option Explicit

 '32-bit API declarations
 
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
  Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long

Declare Function SHBrowseForFolder Lib "shell32.dll" _
  Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long

Public Type BROWSEINFO
  hOwner As Long
  pidlRoot As Long
  pszDisplayName As String
  lpszTitle As String
  ulFlags As Long
  lpfn As Long
  lParam As Long
  iImage As Long
End Type


Sub ListFiles()

Dim msg As String, answer As String
Dim Directory As String
Dim R As Integer
Dim i As Integer
Dim StartDate As Single
     
          
  msg = "Select a location containing the files you want to list."
  Directory = GetDirectory(msg)
  If Directory = "" Then Exit Sub
  If Right(Directory, 1) <> "\" Then Directory = Directory & "\"

 '   Insert headers
     
  R = ActiveCell.Row
  Cells(R, 1) = "FilePath"
  Cells(R, 2) = "Size"
  Cells(R, 3) = "Date/Time"
  Cells(R, 4) = "Filename"
  Range("A1:C1").Font.Bold = True
  R = R + 1

  On Error Resume Next
  With Application.FileSearch
    .NewSearch
    .LookIn = Directory
    .Filename = "*.*"  ' this can be '*.*
    .SearchSubFolders = True
    .Execute
    For i = 1 To .FoundFiles.Count
      If FileDateTime(.FoundFiles(i)) > StartDate Then
        Cells(R, 1) = .FoundFiles(i)
        Cells(R, 4) = Right(Cells(R, 1), Len(.FoundFiles(i)) - InStrRev(Cells(R, 1).Value, "\"))
        Cells(R, 2) = FileLen(.FoundFiles(i))
        Cells(R, 3) = FileDateTime(.FoundFiles(i))
        R = R + 1
      End If
    Next i
  End With
  'Columns("A:C").Select
  'Selection.Columns.AutoFit
    
  'ActiveSheet.Columns("1:3").AutoFit
  MsgBox "file listing complete"
 
End Sub

 Function GetDirectory(Optional msg) As String
     Dim bInfo As BROWSEINFO
     Dim path As String
     Dim R As Long, x As Long, pos As Integer

 ' Root folder = Desktop
     bInfo.pidlRoot = 0&

 ' Title in the dialog
     If IsMissing(msg) Then
         bInfo.lpszTitle = "Select a folder"
     Else
         bInfo.lpszTitle = msg
   End If

 ' Type of directory to return
     bInfo.ulFlags = &H1

 ' Display the dialog
     x = SHBrowseForFolder(bInfo)

 ' Parse the result
     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

Function InStrLast(iStart As Integer, szSrchIn As String, _
                    szSrchFor As String, iCompare As Integer) As Integer
Dim iPrevFoundAt As Integer
Dim iFoundAt As Integer
    On Error GoTo ErrExit_InStrLast
    iPrevFoundAt = 0
    iFoundAt = InStr(iStart, szSrchIn, szSrchFor, iCompare)
    Do While iFoundAt > 0
        iPrevFoundAt = iFoundAt
        iFoundAt = InStr(iPrevFoundAt + 1, szSrchIn, szSrchFor, iCompare)
    Loop
ErrExit_InStrLast:
    If Err <> 0 Then MsgBox Error$, vbExclamation
    InStrLast = iPrevFoundAt
    Exit Function
End Function




 



 Sources du même auteur

LISTER LES RÉPERTOIRES ET SOUS RÉPERTOIRES D'UN DOSSIER
PROTÉGER ET DÉPROTÉGER TOUTES LES FEUILLES D'UN CLASSEUR
REPÉRER VISUELLEMENT DANS UNE FEUILLE DE CALCUL LES CELLULES...
REMPLACER EN VBA LE POINT PAR UNE VIRGULE
FONCTION POUR RENVOYER L'ADRESSE DE LA CELLULE OÙ ELLE EST A...

 Sources de la même categorie

Source avec Zip Source avec une capture OUTLOOK ATTACHEMENT SAVER par MoiLafouine
Source avec Zip GESTION PERSONNEL par oudlarbi
Source avec Zip Source avec une capture CALENDRIER EN VBA POUR EXCEL 2010 par nounou94
Source avec Zip Source avec une capture MANIPULER LES FENETRES ENFANT D'EXCEL par bigfish_le vrai
Source avec Zip Source avec une capture COLLECTION ID par Le Pivert

Commentaires et avis

Commentaire de fljmbonnet le 13/01/2004 10:50:18

il manque la fonction InStrRev pour que ça marche

Commentaire de antra le 04/03/2005 06:24:48

C'est intéressant!

Est ce que tu n'a pas de code pour VB pour cette application! Ou bien comment l'exporter vers un Exe VB.

Je l'exporterai pour une application Réseau, si tu le veux bien.

Commentaire de tbbuim1 le 17/09/2009 14:48:25 10/10

Marche à la perfection, bravo.
Infos pour les noobs
Ce code est à mettre sous Excel... pour cela, faire :
Alt+F11 => Insertion => module => copier/coller du code
Ctrl + H pour enlever les #
Sauvegarde Ctrl + S
Puis Alt + F8 => exécuter
Choisir le dossier a afficher et hop!, "c'est la magie"

Commentaire de atel11 le 08/06/2011 13:53:36

Pourquoi quand je cherche à l'exécuter il me met l'erreur: "erreur de syntaxe"

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,593 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales