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
Bonjour perso j'ai fait un petit test de lecture écriture dans une fichier et ça colle
autrement dit je converti le texte en lecture ligne par ligne et le ré écrit dans un fichier du même nom mais en extension csv avec separateur ";" ligne par ligne en Append sans passer par une feuille
moins de 3 sec chez moi pour refaire le fichier
 

Pièces jointes

  • convertion fichier mal foutu en csv.xlsm
    16.4 KB · Affichages: 6

patricktoulon

XLDnaute Barbatruc
Bonjour @job75
5 ligne de titre en trop ???🤔
on doit pas avoir le même medialist.txt alors
aperçu du medialist converti en csv
1640605068402.png
 

patricktoulon

XLDnaute Barbatruc
re
apres la ligne 9 je zappe carrément les ligne qui contienne des mots de l'entetes
dans le csv je filtre la colonne on hold et j'ai bien qu'un seul titre
mais j'ai bien 14696 lignes pourtant tu peux vérifier j'ai bien qu'une seule ligne d'entete
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 And Trim(DataLine) Like "*On Hold*" Then GoTo re
            If I > 9 And Trim(DataLine) Like "*STATUS*" 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 t(UBound(t)) = 0 And UBound(t) = 11 Then t(UBound(t)) = ";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
le csv résultat en pièce jointe dans un zip car il fait 1.31 Mo
 

Pièces jointes

  • medialist.zip
    271.3 KB · Affichages: 1

patricktoulon

XLDnaute Barbatruc
autant pour moi les mots cle sont sur 3 lignes
donc
résultat 1691 lignes plus la ligne de titre 1692 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 Trim(DataLine) Like "*On Hold*" Then GoTo re
            If Trim(DataLine) Like "*STATUS*" Then GoTo re
            If Trim(DataLine) Like "*restores*" Then GoTo re
            If Trim(DataLine) Like "*Server*" Then GoTo re
        End If

        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 t(UBound(t)) = 0 And UBound(t) = 11 Then t(UBound(t)) = ";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
 

cp4

XLDnaute Barbatruc
autant pour moi les mots cle sont sur 3 lignes
donc
résultat 1691 lignes plus la ligne de titre 1692 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 Trim(DataLine) Like "*On Hold*" Then GoTo re
            If Trim(DataLine) Like "*STATUS*" Then GoTo re
            If Trim(DataLine) Like "*restores*" Then GoTo re
            If Trim(DataLine) Like "*Server*" Then GoTo re
        End If

        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 t(UBound(t)) = 0 And UBound(t) = 11 Then t(UBound(t)) = ";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
Bonjour PatrickToulon, Job75, Hal9000,

@patricktoulon : J'ai testé ton code mais il plante.
Execution.gif


A+
 

Discussions similaires

Statistiques des forums

Discussions
312 361
Messages
2 087 626
Membres
103 608
dernier inscrit
rawane