Process data from a flat file

0

I am doing an activity that consists of the following: I have a list of data in plain text as follows:

and I want to compare the data after the second comma, if the data is a number I have to move it after the third comma, and if the data is a word in this case it is (Count 1 Pist.7) I will not do anything.

I am trying this but it does not work out, I would really appreciate your help. I'm doing it with VBA from Excel ...

Sub leerArchivoTexto()
'Leer archivos de texto separados por coma
'
Dim archivo As String  'ruta y nombre del archivo
Dim texto As String    'línea de texto a leer


Dim i As Integer
Dim n As Integer
Dim renglon As Integer
Dim columna As Integer
Dim contador As Integer
Dim texto2 As String


contador = 0
'Abrir el archivo
archivo = "C:\Users\Usuario\Desktop\Pistolas\conteo1Pistola7_mal.txt"
Open archivo For Input As #1

'Leer y procesar el contenido del archivo
While Not EOF(1)
    Line Input #1, texto

    texto2 = Trim(Mid(texto, 35, 6))


    If (texto2 <> "Conteo") Then
        contador = contador + 1
    End If



Wend
MsgBox (contador)

Close #1

End Sub
    
asked by Silvestre Silva 17.10.2017 в 02:12
source

1 answer

0

I found the solution and here I share it in case someone serves at some point. Basically what I did is to obtain strings with specific number of characters using the Mid function of VBA, compare the values and in case of obtaining a number to re-assemble the string and in case it is text, re-establish the string as it is. at the end I write everything in a new file so as not to alter the original.

Open Ruta1 For Input As #1
    Open Ruta2 For Output As #2
        While Not EOF(1) 'Recorrer los registros del archivo original
            Line Input #1, Texto

            texto2 = Trim(Mid(Texto, 35, 6))    'Obtengo el inicio despues de la segunda coma

            If texto2 <> "Conteo" Then
            Else
               conteo_pistola = Mid(Texto, 35, 16)
            End If

            If texto2 <> "Conteo" Then
                texto3 = Mid(Texto, 35, 5)
                cadena = Mid(Texto, 1, 16) & "," 'Armar la cadena.
                cadena = cadena & Mid(Texto, 18, 16) & ","
                cadena = cadena & conteo_pistola & ","
                cadena = cadena & texto3 & ","
                cadena = cadena & Mid(Texto, 58, 1) & ","
                cadena = cadena & Mid(Texto, 60, 16) & ","
                cadena = cadena & Mid(Texto, 77, 10) & ","
                cadena = cadena & Mid(Texto, 88, 8)
                Print #2, cadena 'Escribir en el 2do archivo la nueva cadena
                Cont = Cont + 1
                Else
                cadena = Texto
                Print #2, cadena 'Escribir en el 2do archivo la nueva cadena
            End If

        Wend
    Close #2
Close #1
    
answered by 19.10.2017 / 16:13
source