Sub test()
Dim Fichier As Variant
Fichier = Application.GetOpenFilename("Text Files (*.txt), *.txt", 1, "ouvrir un fichier")
If Fichier = False Then Exit Sub
convertToCSV Fichier
End Sub
Sub convertToCSV(Fichier) ' ligne par ligneavec input simple
Dim x As Integer, y As Integer, DataLine As String, ligne$, Fichier2$, I&, t
Fichier2 = Mid(Fichier, 1, InStrRev(Fichier, ".") - 1) & ".csv"
x = FreeFile: Open Fichier For Input As #x
If Dir(Fichier2) <> "" Then Kill Fichier2
y = FreeFile: If Dir(Fichier2) = "" Then Open Fichier2 For Output As #y Else Open Fichier2 For Append As #y
While Not EOF(x)
re:
I = I + 1
Line Input #x, DataLine 'lecture de la ligne
DataLine = Application.Trim(DataLine)
If I > 9 Then
If InStr(1, DataLine, "On Hold") > 0 Then GoTo re
If InStr(1, DataLine, "STATUS") > 0 Then GoTo re
If InStr(1, DataLine, "restores") > 0 Then GoTo re
End If
If InStr(1, DataLine, "Server") > 0 Then GoTo re
If I >= 7 Then
t = Split(DataLine, " ")
For a = 0 To UBound(t)
If IsDate(t(a)) Then If Format(t(a), "dd/mm/yyyy") = t(a) Then DataLine = Replace(DataLine, t(a) & " ", t(a) & "|")
Next
t = Split(DataLine, " ")
If UBound(t) >= 11 Then If Trim(t(11)) = "0" Then t(11) = DataLine = DataLine & ";0"
End If
If I = 1 Then
Print #y, DataLine & ";"
Else
DataLine = Replace(DataLine, "FULL SUSPENDED", "FULL-SUSPENDED")
DataLine = Replace(DataLine, "On Hold", "On|Hold")
If I >= 2 And I < 5 Then
DataLine = Replace(DataLine, "last update", "last-update")
DataLine = Replace(DataLine, "last read", "last-read")
DataLine = Replace(DataLine, " STATUS ", "STATUS")
DataLine = Replace(DataLine, vbCrLf, " ")
End If
If DataLine = "0" Or Left(DataLine, 2) = "--" Or DataLine = "" Then
If ligne & Replace(DataLine, "--", "") <> "" Then Print #y, Replace(ligne & Replace(DataLine, "--", ""), "|", " ") & ";"
ligne = "" '& vbCrLf
Else
ligne = ligne & Replace(DataLine, " ", ";") & ";"
End If
End If
Wend
Close #x
Close #y
End Sub