1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| Option Explicit
Private Const vbDot = 46
Private Const MAX_PATH As Long = 260
Private Const INVALID_HANDLE_VALUE = -1
Private Const vbBackSlash = "\"
Private Const ALL_FILES = "*.*"
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Private Type FILE_PARAMS
bRecurse As Boolean
bFindOrExclude As Long
nCount As Long
nSearched As Long
sFileNameExt As String
sFileRoot As String
End Type
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function FindFirstFile Lib "kernel32" _
Alias "FindFirstFileA" _
(ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" _
Alias "FindNextFileA" _
(ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenW" (ByVal lpString As Long) As Long
Private Declare Function PathMatchSpec Lib "shlwapi" _
Alias "PathMatchSpecW" _
(ByVal pszFileParam As Long, ByVal pszSpec As Long) As Long
Private FP As FILE_PARAMS
Private iNbDossier As Long
Private Sub Effacer()
With ShParam
.Activate
.Columns("B:B").ClearContents
.Range("A1").ClearContents
.Range("A3:A5").ClearContents
.Range("A6:A" & .Rows.Count).ClearContents
With .Range("A6:A" & .Rows.Count)
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.Interior.ColorIndex = xlNone
End With
PosBoutons
.Activate
.Range("B2").Select
End With
End Sub
Private Function MatchSpec(sFile As String, sSpec As String) As Boolean
MatchSpec = PathMatchSpec(StrPtr(sFile), StrPtr(sSpec)) = FP.bFindOrExclude
End Function
Private Sub PosBoutons()
Dim T As Range
Application.ScreenUpdating = False
With ShParam
.Activate
.Rows(1).RowHeight = 12.75
Set T = .Cells(1, 3)
With .Buttons("btnListe")
.Left = T.Left + 3
.Top = T.Top + 15
.Width = 100
.Height = 2 * Rows(1).RowHeight - 5
End With
With .Buttons("btnExtraction")
.Left = ShParam.Buttons("btnListe").Left + ShParam.Buttons("btnListe").Width + 5
.Top = ShParam.Buttons("btnListe").Top
.Width = ShParam.Buttons("btnListe").Width + 30
.Height = ShParam.Buttons("btnListe").Height
End With
With .Buttons("btnEffacer")
.Left = ShParam.Buttons("btnExtraction").Left + ShParam.Buttons("btnExtraction").Width + 5
.Top = ShParam.Buttons("btnListe").Top
.Width = ShParam.Buttons("btnListe").Width + 30
.Height = ShParam.Buttons("btnListe").Height
End With
With .Buttons("btnEffacerExt")
.Left = ShParam.Buttons("btnEffacer").Left
.Top = ShParam.Buttons("btnEffacer").Top + ShParam.Buttons("btnEffacer").Height + 5
.Width = ShParam.Buttons("btnListe").Width + 30
.Height = ShParam.Buttons("btnListe").Height
End With
With .Shapes("chkRecur")
.Left = ShParam.Shapes("btnListe").Left
.Top = ShParam.Shapes("btnListe").Top + ShParam.Shapes("btnListe").Height + 5
.Width = ShParam.Buttons("btnListe").Width
.Height = ShParam.Buttons("btnListe").Height
End With
.Range("B2").Select
End With
Application.ScreenUpdating = False
End Sub
Private Function QualifyPath(sPath As String) As String
If Right$(sPath, 1) <> vbBackSlash Then
QualifyPath = sPath & vbBackSlash
Else
QualifyPath = sPath
End If
End Function
Private Sub Rch(sRacine As String)
Dim Debut As Currency, Fin As Currency, Freq As Currency
With ShParam
.Columns("A:B").ClearContents
.Cells(1, 1) = sRacine
.Cells(2, 1) = srch
.Cells(3, 1) = ""
.Cells(4, 1) = ""
.Cells(5, 1) = ""
.Range("B" & RDepart & ":B" & Rows.Count).Clear
End With
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1
Application.ScreenUpdating = False
With FP
.sFileRoot = QualifyPath(ShParam.Cells(1, 1))
.sFileNameExt = ShParam.Cells(2, 1)
.bRecurse = ShParam.CheckBoxes("chkRecur").Value = 1
.nCount = 0
.nSearched = 0
iNbDossier = 0
' 0=inclus tous les fichiers
' 1=exclus sauf extension : ici pdf
.bFindOrExclude = 1
End With
QueryPerformanceCounter Debut
sDossierDepart = FP.sFileRoot
SearchForFiles FP.sFileRoot
QueryPerformanceCounter Fin
QueryPerformanceFrequency Freq
With ShParam
.Cells(3, 1) = iNbDossier & " Dossier(s)"
.Cells(4, 1) = Format$(FP.nCount, "###,###,###,##0 Fichier(s)")
.Cells(5, 1) = FormatNumber((Fin - Debut) / Freq, 3) & " s"
.Range("A1:A5").HorizontalAlignment = xlLeft
.Range("A2:A5").Columns.AutoFit
PosBoutons
.Activate
.Range("B2").Select
End With
Application.ScreenUpdating = True
End Sub
Private Sub SearchForFiles(sRoot As String)
Dim WFD As WIN32_FIND_DATA
Dim hFile As Long, i As Long, sDoss As String
hFile = FindFirstFile(sRoot & ALL_FILES, WFD)
If hFile <> INVALID_HANDLE_VALUE Then
Do
If (WFD.dwFileAttributes And vbDirectory) Then
If Asc(WFD.cFileName) <> vbDot Then
iNbDossier = iNbDossier + 1
If FP.bRecurse Then SearchForFiles sRoot & TrimNull(WFD.cFileName) & vbBackSlash
End If
Else
If MatchSpec(WFD.cFileName, FP.sFileNameExt) Then
FP.nCount = FP.nCount + 1
i = Len(sDossierDepart)
sDoss = Right$(sRoot & TrimNull(WFD.cFileName), Len(sRoot & TrimNull(WFD.cFileName)) - i)
ShParam.Cells(FP.nCount + RDepart - 1, 2) = sDoss
End If
End If
Loop While FindNextFile(hFile, WFD)
End If
Application.StatusBar = iNbDossier & " / " & FP.nCount
FindClose hFile
End Sub
Sub SelDossierRacine(Optional Dummy As String)
Dim sChemin As String, sDossierDepart As String
Dim FSO As Object
sChemin = ThisWorkbook.Path
sDossierDepart = ShParam.Cells(1, 1)
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(sDossierDepart) Then sChemin = sDossierDepart
Set FSO = Nothing
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = sChemin & "\"
.Title = "Sélectionner le Dossier Racine"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewDetails
.ButtonName = "Sélection Dossier"
.Show
If .SelectedItems.Count > 0 Then
ShParam.Activate
Effacer
ShParam.Range("B2").Select
DoEvents
Rch .SelectedItems(1)
End If
End With
End Sub
Private Function TrimNull(startStr As String) As String
TrimNull = Left$(startStr, lstrlen(StrPtr(startStr)))
End Function
Sub Usf(Optional Dummy As String)
UserForm1.Show vbModeless
End Sub |
Partager