begin process at 2008 08 22 06:18:38
1 229 779 membres
50 nouveaux aujourd'hui
14 267 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

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


Information sur la source

Catégorie :VBA Niveau : Initié Date de création : 11/07/2003 Date de mise à jour : 11/07/2003 14:09:35 Vu : 7 457

Note :
1 / 10 - par 1 personne
1,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (2)
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




 
  • signaler à un administrateur
    Commentaire de fljmbonnet le 13/01/2004 10:50:18

    il manque la fonction InStrRev pour que ça marche

  • signaler à un administrateur
    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.

Ajouter un commentaire

Pub



Appels d'offres

CalendriCode

Août 2008
LMMJVSD
    123
45678910
11121314151617
18192021222324
25262728293031

Boutique

Boutique de goodies CodeS-SourceS