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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
Option Compare Database
Option Explicit
' Cette classe fournit des procédures pour :
' rechercher des fichiers ; trier un tableau de noms de fichiers ;
' lire des fichiers texte et y écrire ; localiser des fichiers ;
' analyser des informations de nom de fichier.
'
' Méthodes publiques :
' GetFileList()
' DoesFileExist(strFileName)
' ParseFilePath(strFileName, strPath, strFile)
' ReadFromTextFile(strFileName)
' WriteToTextFile(strFileName, strText [, intOutputMode])
'
'
' Propriétés publiques :
' SearchPath (en lecture/écriture, chaîne, par défaut = "c:\")
' SearchName (en lecture/écriture, chaîne, par défaut = "*.*")
' SearchSubDirs (en lecture/écriture, booléen, par défaut = False)
' SortBy (en lecture/écriture, nombre entier, par défaut = msoSortByfileName)
' SortOrder (en lecture/écriture, nombre entier, par défaut = msoSortOrderAscending)
' MatchingFilesFound (en lecture seule, nombre entier)
'
' Utilisation :
' À partir d'un module standard, déclare une variable objet
' du type clsGetFileInfo. Utilise cette variable objet
' pour accéder à toutes les méthodes et
' propriétés de cette classe.
'
' Exemple :
' Sub Foo()
' Dim objFiles As New clsGetFileInfo
' Dim strFileName As String
'
' strFileName = "c:\config.sys"
' With objFiles
' If .DoesFileExist(strFileName) = False Then
' MsgBox "'" & strFileName & "' does not exist!"
' End If
' End With
' End Sub
'
' Définit les variables privées pour enregistrer les
' valeurs de propriété.
Private p_strPath As String
Private p_strName As String
Private p_blnSearchSubs As Boolean
Private p_intFoundFiles As Integer
Private p_intSortOrder As Integer
Private p_intSortBy As Integer
' Définit les constantes
Private Const OUTPUTMODE_APPEND As Integer = 1
Private Const OUTPUTMODE_OUTPUT As Integer = 2
Property Let SearchPath(strPath As String)
' Définit la propriété SearchPath.
p_strPath = strPath
End Property
Property Get SearchPath() As String
' Renvoie la propriété Searchpath.
SearchPath = p_strPath
End Property
Property Let SearchName(strName As String)
' Définit la propriété SearchName.
p_strName = strName
End Property
Property Get SearchName() As String
' Renvoie la propriété SearchName.
SearchName = p_strName
End Property
Property Let SearchSubDirs(blnSearchSubs As Boolean)
' Définit la propriété SearchSubDirs.
p_blnSearchSubs = blnSearchSubs
End Property
Property Get SearchSubDirs() As Boolean
' Renvoie la propriété SearchSubDirs.
SearchSubDirs = p_blnSearchSubs
End Property
Property Get MatchingFilesFound() As Integer
' Renvoie la propriété MatchingFilesFound.
MatchingFilesFound = p_intFoundFiles
End Property
Property Get SortOrder() As Integer
' Renvoie la propriété SortOrde.
SortOrder = p_intSortOrder
End Property
'Property Let SortOrder(intSortOrder As Integer)
' ' Définit la propriété SortOrder.
' If intSortOrder > 2 Or intSortOrder < 1 Then
' p_intSortOrder = msoSortOrderAscending
' Else
' p_intSortOrder = intSortOrder
' End If
'End Property
Property Get SortBy() As Integer
' Renvoie la propriété SortBy.
SortBy = p_intSortBy
End Property
'Property Let SortBy(intSortBy As Integer)
' ' Définit la propriété SortBy.
' If intSortBy > 4 Or intSortBy < 1 Then
' p_intSortBy = msoSortByFileName
' Else
' p_intSortBy = intSortBy
' End If
'End Property
Private Sub Class_Initialize()
' Définit les propriétés par défaut de l'objet class.
' Propriété SearchPath par défaut.
p_strPath = "c:\"
' Propriété SearchName par défaut.
p_strName = "*.*"
' Propriété SearchSubDirs par défaut.
p_blnSearchSubs = False
' Propriété SortOrder par défaut.
' p_intSortOrder = msoSortOrderAscending
' Propriété SortBy par défaut.
' p_intSortBy = msoSortByFileName
' Propriété MatchingFilesFound (en lecture seule) par défaut.
p_intFoundFiles = 0
End Sub
Function RemoveLinkedFileDuplicates(objFileSearch) As Variant
' L'objet FileSearch peut renvoyer des doublons des fichiers .lnk.
' Cette procédure indique une manière de supprimer les doublons des noms de fichier
' de la liste renvoyée par l'objet FileSearch.
' Il existe un exemple commenté qui montre comment appeler
' cette fonction dans la procédure GetFileList.
Dim collUniqueFiles As New Collection
Dim astrFiles() As String
Dim intFoundFiles As Integer
Dim strCurrFile As String
Dim strPrevFile As String
With objFileSearch
For intFoundFiles = 1 To .FoundFiles.Count
strCurrFile = .FoundFiles(intFoundFiles)
If strCurrFile <> strPrevFile Then
collUniqueFiles.Add strCurrFile, CStr(intFoundFiles)
strPrevFile = strCurrFile
End If
Next intFoundFiles
End With
p_intFoundFiles = collUniqueFiles.Count
ReDim astrFiles(collUniqueFiles.Count - 1)
For intFoundFiles = 1 To collUniqueFiles.Count
astrFiles(intFoundFiles - 1) = collUniqueFiles(intFoundFiles)
Next intFoundFiles
RemoveLinkedFileDuplicates = astrFiles
End Function
Public Function DoesFileExist(strFileName As String) As Boolean
' Cette procédure renvoie True si le fichier
' spécifié dans strFileName existe et False
' s'il n'existe pas.
Dim strSearchResults As String
' strFileName contient-il quelque chose ?
If Len(strFileName) = False Then
DoesFileExist = False
Exit Function
' strFileName contient-il une extension de fichier ?
ElseIf InStr(strFileName, ".") = False Then
DoesFileExist = False
Exit Function
' Recherche le fichier.
Else
strSearchResults = Dir$(strFileName)
If Len(strSearchResults) > 0 Then
DoesFileExist = True
Else
DoesFileExist = False
End If
End If
End Function
Public Function ParseFilePath(strFileName As String, _
strPath As String, _
strFile As String) As Boolean
' Cette fonction sépare le nom de fichier du nom de fichier et de chemin complets
' communiqués dans strFileName. La procédure renvoie le nom de fichier
' au paramètre strFile parameter et le chemin de fichier au paramètre
' strPath. Le paramètre strFileName peut être un chemin de fichier
' commençant par une lettre d'unité.
' Par exemple : "c:\mes documents\lettres\Merci.doc"
' Il peut être aussi un chemin réseau complet.
' Par exemple : \\Serveur1\Public\Documents\VentesTrim.xls"
Dim intSavedPosition As Integer
Dim intNewPosition As Integer
' strFileName contient-il quelque chose ?
If Len(strFileName) = 0 Then
ParseFilePath = False
Exit Function
' strFileName contient-il une extension de fichier ?
ElseIf InStr(strFileName, ".") = 0 And InStr(strFileName, "\") Then
ParseFilePath = False
Exit Function
Else
' Recherche la position du dernier caractère "\" de la chaîne, puis
' sépare le nom de fichier du chemin de fichier.
Do
intSavedPosition = intNewPosition
intNewPosition = InStr(intNewPosition + 1, strFileName, "\")
Loop While intNewPosition
strPath = left$(strFileName, intSavedPosition)
strFile = Mid$(strFileName, intSavedPosition + 1)
ParseFilePath = True
End If
End Function
Public Function ReadFromTextFile(strFileName As String) As String
' Cette procédure lit toutes les données du fichier spécifié dans
' strFileName. Elle utilise d'abord DoesFileExist() pour s'assurer
' que le fichier existe, puis lit les données du fichier. Elle renvoie
' le texte contenu dans strFileName.
Dim strTempString As String
Dim intFileNum As Integer
If DoesFileExist(strFileName) = True Then
intFileNum = FreeFile
Open strFileName For Input As #intFileNum
Do While Not EOF(intFileNum)
strTempString = strTempString & Input$(1, #intFileNum)
Loop
Close #1
ReadFromTextFile = strTempString
End If
End Function
Public Function WriteToTextFile(strFileName As String, _
strText As String, _
Optional intOutputMode As Integer = 1) _
As Boolean
' Cette procédure écrit les données de strText dans le fichier
' spécifié dans strFileName. Elle appelle d'abord la procédure
' DoesFileExist() pour déterminer si strFileName est un fichier
' existant. Dans la négative, elle appelle ParseFilePath() pour s'assurer que
' la valeur de strFileName contient à la fois un chemin et un nom de fichier.
' Si tout est vérifié, le fichier est écrit dans
' strFileName en mode Ajout ou en mode d'ouverture en fonction de la valeur
' de l'argument intOutputMode.
Dim intFileNum As Integer
Dim strPath As String
Dim strFile As String
If Len(strFileName) > 0 Then
If Len(strText) > 0 Then
If DoesFileExist(strFileName) = False Then
If ParseFilePath(strFileName, strPath, strFile) = False Then
WriteToTextFile = False
Exit Function
End If
End If
Else
WriteToTextFile = False
Exit Function
End If
Else
WriteToTextFile = False
Exit Function
End If
intFileNum = FreeFile
' Valide le paramètre intOutputMode.
If intOutputMode = OUTPUTMODE_APPEND Then
Open strFileName For Append As #intFileNum
ElseIf intOutputMode = OUTPUTMODE_OUTPUT Then
Open strFileName For Output As #intFileNum
Else
' Déclenche l'erreur ici.
WriteToTextFile = False
End If
Print #intFileNum, strText
Close #intFileNum
WriteToTextFile = True
End Function |
Partager