Edit rows in vbscript

0

I am trying to learn VBS to solve a problem that happened when I tried to import a file on my system. The file has more than 4000 rows and all rows must be 240 characters long. (I do not have backup)

Currently, the document is unconfigured like this:

The first line is ok with 240.

The second line is only with CRLF. (0).

240 + 0 = 240

The third line is 238 characters + CRLF (238 characters).

The fourth line is 2 characters + CRLF (2 characters).

238 + 2 = 240

The fifth line is 236 characters + CRLF (236 characters).

The sixth line is 4 characters + CRLF (4).

236 + 4 = 240

The seventh line is 234 characters + CRLF (234 characters).

The eighth line is 6 characters + CRLF (6 characters).

234 + 6 = 240

I noticed that this pattern continues throughout the file, so I think I can fix this for VBS.

Could anyone help me create a script to solve this problem?

I think I should read each line, use Len () to check the amount of characters and give a DELETE where the CRLF would be if the line has fewer than 240 positions.

Or maybe do Split and concatenate line k with line k + 1, in a loop where k starts with 0 (line 1 of the file) and will receive K = K + 2 at each iteration.

I'll try first to make the Split option. If someone wants to collaborate with some kind of code or just say a smarter way to solve the case, thank you!

    
asked by anonymous 07.07.2018 / 19:03

1 answer

0

Script vbs:

Dim arq
Dim obj

Set obj = CreateObject("Scripting.FileSystemObject")
Set arq = obj.OpenTextFile(WScript.Arguments(0), 1)

Dim texto
Dim conteudo
Dim tamanho

texto = arq.ReadAll
arq.Close
Set obj = Nothing

conteudo = Split(texto, vbCrLf)
tamanho = UBound(conteudo) - LBound(conteudo) + 1

Dim i
Dim numlinha

numlinha = 0
Do While numlinha < tamanho
    Dim novo
    novo = conteudo(numlinha)

    If Len(novo) < 240 Then
        Dim primeiralinha
        primeiralinha = numlinha
        Do While numlinha < tamanho And Len(novo) < 240
            numlinha = numlinha + 1
            novo = novo & conteudo(numlinha)
        Loop
        If Len(novo) <> 240 Then
            Err.Raise vbObjectError + 514, _
                "Erro concatenando linhas entre " & (primeiralinha + 1) & _ 
                " e " & (numlinha + 1) & " (inclusive)"
        Else
            WScript.Echo novo
        End If
    ElseIf Len(novo) > 240 Then
        Err.Raise vbObjectError + 515, _
            "Linha " & (numlinha + 1) & " tem mais de 240 caracteres"
    Else
        WScript.Echo novo
    End If

    numlinha = numlinha + 1
Loop

To use:

cscript script.vbs c:\dados\meuarquivo.txt > c:\dados\meunovoarquivo.txt
    
10.07.2018 / 02:49