new line after n'th character in text file

July 8, 2009 at 08:39:41
Specs: Windows XP
How do I make a bat script to make a new line after every n'th character in a text file filled with characters without spaces or new lines

See More: new line after nth character in text file

Report •


#1
July 8, 2009 at 08:57:58
And another question if it's not too dificult :)

If I have a text file:
1111110111111102111111031111110411111105.....1111
NEW LINE
1111110111111102111111031111110411111105.....1111
NEW LINE
1111110111111102111111031111110411111105.....1111
NEW LINE
etc.

Then i want to arrange it as the following example:

11111101 0 1 0
11111102 1
11111103
11111104
11111105
11111106
11111107
11111108
11111109
11111110
11111111
11111112
11111113
11111114
11111115
11111116
11111117
11111118
11111119
11111120
11111121
11111122
11111123
11111124
11111125
11111126
11111127
11111128
11110000 1 0 0
11111101 0 1 0
11111102 1
11111103
11111104
11111105
11111106
11111107
11111108
11111109
11111110
11111111
11111112
11111113
11111114
11111115
11111116
11111117
11111118
11111119
11111120
11111121
11111122
11111123
11111124
11111125
11111126
11111127
11111128
11110000 1 0 0
11111101 0 1 0
11111102 1
11111103
11111104
11111105
11111106
11111107
11111108
11111109
11111110
11111111
11111112
11111113
11111114
11111115
11111116
11111117
11111118
11111119
11111120
11111121
11111122
11111123
11111124
11111125
11111126
11111127
11111128
11110000 1 0 0

e.g.

1. New line after every 8th character.

2. The FIRST line needs a SPACE 0 SPACE 1 SPACE 0 after the 8 characters

3.The SECOND line needs a SPACE 1 after the 8 characters.

4. The LAST LINE needs to be padded with zeros ONLY IF the last line has 4 characters (to get 8 characters). Then a SPACE 1 SPACE 0 SPACE 0 after the 8 characters.

5. Then the original text file has a new line and a lot of characters again, which have to be placed just under the previous data with the same structure described in section 1 - 4.

Thanks.

Help would be appreciated very much as this is a very important part of my project... Thanks..


Report •

#2
July 8, 2009 at 09:00:15
EDIT: Damnit, you just keep adding on, don't you.

It's fairly easy with VBScript, just replace the first two values with whatever you need:

const lineSize = 8
const inFile = "some.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.OpenTextFile("output.txt", 2, True)
arryLine = Array("", "")
With fso.OpenTextFile(inFile, 1)
  Do Until .AtEndOfStream
    outFile.WriteLine arryLine(1) _
        & .Read(lineSize - Len(arryLine(1))) & " 0 1 0"
    line = .Read(lineSize) & " 1"
    Do Until .AtEndOfStream Or InStr(line, vbNewLine)
      outFile.WriteLine line
      line = .Read(lineSize)
    Loop
    If Not .AtEndOfStream Then _
      line = line & .Read(Len(vbNewLine))
    arryLine = Split(line, vbNewLine, 2)
    If arryLine(0) = "" Then
      arryLine(0) = arryLine(1)
      arryLine(1) = ""
    End If
    If Not .AtEndOfStream Then
      outFile.WriteLine arryLine(0) _
          & String(lineSize - Len(arryLine(0)), "0") & " 1 0 0"
    Else
      outFile.Write arryLine(0) _
          & String(lineSize - Len(arryLine(0)), "0") & " 1 0 0"
    End If
  Loop
End With

EDIT: Changed script to reflect new requirements.


Report •

#3
July 8, 2009 at 09:27:48
Seriously... You are the man!

But not quite there... see req. :)


Report •

Related Solutions

#4
July 8, 2009 at 10:00:18
You're f...ing amazing... :)

You just saved my day...


Report •

#5
July 8, 2009 at 10:18:29
So close...
It was buggy, so I removed it until I could fix it.

There you go. When it hits a new line, the script will now go into its EoF logic, then start its SoF logic. That should be it, unless I missed something or you have another requirement you forgot to mention. Again.

Also, this is sloppy code. I really should refactor it, or something. I'm not going to, but I acknowledge that I should.


Report •

#6
July 8, 2009 at 10:25:00
He He.. sorry... was editing my post when you first answered..

A very minor thing .. there is a blank line after the last line.. not that big of a deal.. but.... :)

I really appreciate your help! Thanks for taking the time...


Report •

#7
July 8, 2009 at 10:45:56
Okay, I removed the new line at EoF, but the change is untested. Let me know if it bombs or breaks.

Report •

#8
July 8, 2009 at 11:50:34
Looks like it's working like a charm..!

Report •


Ask Question