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
| Option Explicit
Dim fso, dossier, fichier, ligne, lignes, i
Dim cheminDossier, colonnes, idxHeure, idxStatut, valeurHeure, valeurStatut
Dim tsIn, tsOut, enTete
cheminDossier = "C:\TEMP\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set dossier = fso.GetFolder(cheminDossier)
For Each fichier In dossier.Files
If LCase(fso.GetExtensionName(fichier.Name)) = "csv" Then
' Gérer les erreurs pour ce fichier uniquement
On Error Resume Next
Set tsIn = fso.OpenTextFile(fichier.Path, 1, False, -1)
If Err.Number <> 0 Then
WScript.Echo "Erreur ouverture : " & fichier.Name
Err.Clear
On Error GoTo 0
GoTo FichierSuivant
End If
On Error GoTo 0 ' Réactiver les erreurs
If Not tsIn.AtEndOfStream Then
enTete = tsIn.ReadLine
colonnes = Split(enTete, ",")
idxHeure = -1
idxStatut = -1
For i = 0 To UBound(colonnes)
If Trim(colonnes(i)) = "Heures" Then idxHeure = i
If Trim(colonnes(i)) = "Statut dapprobation" Then idxStatut = i
Next
If idxHeure = -1 Or idxStatut = -1 Then
WScript.Echo "Colonnes manquantes dans : " & fichier.Name
tsIn.Close
GoTo FichierSuivant
End If
lignes = Array()
ReDim Preserve lignes(0)
lignes(0) = enTete
Do Until tsIn.AtEndOfStream
ligne = tsIn.ReadLine
If Trim(ligne) <> "" Then
Dim champs
champs = Split(ligne, ",")
If UBound(champs) >= idxStatut Then
valeurHeure = Trim(champs(idxHeure))
valeurStatut = Trim(champs(idxStatut))
If valeurHeure <> "" And valeurHeure <> "0" Then
If valeurStatut = "Approuvé" Then
ReDim Preserve lignes(UBound(lignes) + 1)
lignes(UBound(lignes)) = ligne
End If
End If
End If
End If
Loop
End If
tsIn.Close
' Écriture sécurisée
On Error Resume Next
Set tsOut = fso.CreateTextFile(cheminDossier & "filtré_" & fichier.Name, True, True)
If Err.Number <> 0 Then
WScript.Echo "Erreur écriture : " & fichier.Name
Err.Clear
On Error GoTo 0
GoTo FichierSuivant
End If
On Error GoTo 0
For i = 0 To UBound(lignes)
tsOut.WriteLine lignes(i)
Next
tsOut.Close
WScript.Echo "Fichier filtré : " & fichier.Name
End If
FichierSuivant:
Next
WScript.Echo "Traitement terminé." |
Partager