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
|
Option Explicit
Private bNotFirstTime As Boolean
Private Sub test()
bNotFirstTime = False
ListFilesInFolder "K:\DATA\Commun\Données\projet\05- Realisation\02- Flux\projet\BST", True
End Sub
Function parseDate(ByVal str As String, flux As String, objetpivot As String, appli As String, annee As Integer, mois As Integer, jour As Integer, heure As Integer, minute As Integer)
Dim regex As Object ', matches As Object
Set regex = CreateObject("VBScript.RegExp")
With regex
.Pattern = "([a-zA-Z0-9]{2,})_([a-zA-Z0-9]{2,})_([a-zA-Z0-9]{2,})_([0-9]{4})-([0-9]{2})-([0-9]{2})-([0-9]{2})([0-9]{2})"
.Global = True
End With
'str = "FLUX_OP_RMC_2016-12-08-154642_1481208402431601032.xml"
Set matches = regex.Execute(str)
flux = matches(0).SubMatches(0)
objetpivot = matches(0).SubMatches(1)
appli = matches(0).SubMatches(2)
annee = matches(0).SubMatches(3)
mois = matches(0).SubMatches(4)
jour = matches(0).SubMatches(5)
heure = matches(0).SubMatches(6)
minute = matches(0).SubMatches(7)
Debug.Print str & " - " & flux & " - " & objetpivot & " - " & appli & " - " & jour & "/" & mois & "/" & annee & " " & heure & ":" & minute
End Function
Sub ListFilesInFolder(strFolderName As String, bIncludeSubfolders As Boolean)
' adapté de Ole P Erlandsen
' necessite d'activer la reference Microsoft Scripting RunTime
' dans lediteur : menu Outils => Reference pour activer
Static FSO As FileSystemObject
Dim oSourceFolder As Scripting.Folder
Dim oSubFolder As Scripting.Folder
Dim oFile As Scripting.File
Dim annee As Integer, mois As Integer, jour As Integer, heure As Integer, minute As Integer
Dim flux As String, objetpivot As String, appli As String
Static wksDest As Worksheet
Static iRow As Long
Dim dateTime As Date, dateTimePrev As Date, parentFolderPrev As String
Dim dateTimeDiffMin As Date, dateTimeDiffHeure As Date, dateTimeDiff As Date
annee = mois = jour = heure = minute = 0
dateTimePrev = 0
parentFolderPrev = ""
Columns("E:E").NumberFormat = "dd/mm/yyyy"
Columns("F:F").NumberFormat = "dd/mm/yyyy hh:mm"
Columns("G:G").NumberFormat = "hh:mm:ss"
Columns("H:H").NumberFormat = "hh:mm:ss"
'bNotFirstTime = False
'Debug.Print strFolderName
If Not bNotFirstTime Then
Set wksDest = ActiveSheet ' A adtapter
Set FSO = CreateObject("Scripting.FileSystemObject")
With wksDest
.Cells(1, 1) = "Flux"
.Cells(1, 2) = "Appli"
.Cells(1, 3) = "Fichier"
.Cells(1, 4) = "Taille en ko"
.Cells(1, 5) = "Date"
.Cells(1, 6) = "Date et heure"
.Cells(1, 7) = "Ecart dans une journee"
End With
iRow = 2
bNotFirstTime = True
End If
Set oSourceFolder = FSO.GetFolder(strFolderName)
For Each oFile In oSourceFolder.Files
'Debug.Print "iRow=" & iRow & "oFile.ParentFolder.Path " & oFile.ParentFolder.Path
If InStr(oFile.Name, "xml") Then
'parsing du nom du fichier pour recuper la date
Debug.Print "Avant appel parseDate : " & oFile.Name
parseDate oFile.Name, flux, objetpivot, appli, annee, mois, jour, heure, minute
Debug.Print "Après appel parseDate : " & oFile.Name
'dateStr = annee & "/" & mois & "/" & jour
dateTime = DateSerial(annee, mois, jour)
dateTime = DateAdd("h", heure, dateTime)
dateTime = DateAdd("n", minute, dateTime)
'dateTime
If parentFolderPrev = oFile.ParentFolder.Path Then
If Day(dateTimePrev) = Day(dateTime) And DateDiff("d", dateTimePrev, dateTime) <= 1 Then
dateTimeDiffMin = DateDiff("n", dateTimePrev, dateTime)
dateTimeDiffHeure = Int(dateTimeDiffMin / 60)
dateTimeDiffMin = dateTimeDiffMin Mod 60
dateTimeDiff = TimeSerial(dateTimeDiffHeure, dateTimeDiffMin, 0) 'le calcul doit se faire ici, car variable multi type
Else
dateTimeDiffMin = ""
dateTimeDiffHeure = ""
dateTimeDiff = ""
End If
Else
dateTimeDiffMin = ""
dateTimeDiffHeure = ""
dateTimeDiff = ""
End If
With wksDest
.Cells(iRow, 1) = flux & "_" & objetpivot 'oFile.ParentFolder.Path
.Cells(iRow, 2) = appli
.Cells(iRow, 3) = oFile.Name
.Cells(iRow, 4) = Round(oFile.Size / 1024, 0) ', "### ### ##0") 'conversion en ko
.Cells(iRow, 5) = DateSerial(annee, mois, jour) 'Format(DateSerial(annee, mois, jour), "dd/mm/yyyy")
.Cells(iRow, 6) = dateTime
.Cells(iRow, 7) = dateTimeDiff
End With
iRow = iRow + 1
dateTimePrev = dateTime
parentFolderPrev = oFile.ParentFolder.Path
End If
Next oFile
If bIncludeSubfolders Then
For Each oSubFolder In oSourceFolder.SubFolders
ListFilesInFolder oSubFolder.Path, False
Next oSubFolder
End If
End Sub |
Partager