Microsoft 365 Conversion d'un fichier TXT mal foutu....

HAL9000

XLDnaute Nouveau
Bonjour. en possession d'un TX atteint d'un lourd handicap (export de NetBackup) toutes les colonnes (dont celles d'entête) sont reparties sur 3 lignes suivies d'une ligne blanche.. J'ai commencé par une importation via "requete", suivie d'une concaténation, d'une conversion en colonnes et d'une suppression des lignes vides... J'avoue avoir été au plus simple... Je vous joins le fichier TXT... Si quelqu'un avait de bonnes idées, et j'en suis persuadé ce serait sympa. En attendant ce que j'ai mis en place fonctionne mais c'est lent.
 
Solution
Bonjour HAL9000,

Pour supprimer les lignes de titres sauf celle des 1ères en-têtes de colonnes ce n'est guère plus difficile, voyez le fichier (2) zippé et la macro :
VB:
Sub Importer()
Dim t#, liste, x%, texte$, a$(), n&, i&, texte1$, texte2$, s, ub%, j%
t = Timer
liste = Array("FULL", "SUSPENDED", "FROZEN", "IMPORTED") 'liste pour la colonne STATUS, à adapter
x = FreeFile
Open ThisWorkbook.Path & "\medialist.txt" For Input As #x
While Not EOF(x) 'fin du fichier
    Line Input #x, texte
    texte = Application.Trim(texte) 'SUPPRESPACE
    If Replace(texte, "-", "") <> "" And Not texte Like "Server*" Then
        Select Case i Mod 3
            Case 0: texte1 = texte
            Case 1: texte2 = texte
            Case 2...

patricktoulon

XLDnaute Barbatruc
re
ok c'etait la ta 2d archive
tu devrait l'enlever la première post #11

bon ben si j’enlève chez moi la ligne "serveur..." d'en haut on est pareil 10691 lignes
VB:
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
 

patricktoulon

XLDnaute Barbatruc
re
oui c'est ça je me suis juste trompé dans la redac du post c'est bien 14691
1640634434195.png
 

Discussions similaires

Statistiques des forums

Discussions
312 354
Messages
2 087 548
Membres
103 588
dernier inscrit
Tom59300Tom