computing
  • 1

Solved Insert a Carriage Return Into a Txt File

  • 1

Hi all.
I believe I have a challenge for you all today.
Basically I just need to insert a carriage return into a txt file before a certain (known ) string using a .bat file. Basically the txt file has about 2000 records clumped together. The findstr would be perfect as I am looking for the reference number of the user, but the findstr just returns the entire file as there are no carriage returns. I need to insert a carriage return before every instance of the word “header” and after every instance of the word “MTR”.If I can get the carriage return in those places it would be great because the findstr will return every bit of the record between the “Header” and “MTR”.
However as easy as this may sound there are a few catches.
1. there are currently no carriage returns in the file.
2.The file is about 70mb
3.Although there are delimiters in the file, the actual number of delimiters changes per record and is always well above the 26 allowed by tokens.
4.there are a huge number of lines (over 2000 a4 pages) worth of records, using a loop would be inpractable ?
5.Findstr just returns the entire txt file because there are no carriage returns
6.There are about 2000 instances of the word “header”

Sample format
Header’there is just so’much text that : goes here and the ‘amount of dilimiters’? is just socrazy’the RECORD NUMBER MAYBE BOUT HERE+and then there would be more stuff.This∧ the dilimiters would:go on a further+50lines’before it hits the’MTR’Header:and were off again for another record of 50 lines’MTR
The final appearance (if possible to export into a .txt file with changes ?) it to look like the below as the find str will grab everything between the “header” and “MTR”.the RECORD NUMBER will be based on user input but I can do that.Here is what im hoping for

Header’there is just so’much text that : goes here and the ‘amount of dilimiters’? is just socrazy’the RECORD NUMBER MAYBE BOUT HERE+and then there would be more stuff.This∧ the dilimiters would:go on a further+50lines’before it hits the’MTR’

Header’there is just so’much text that : goes here and the ‘amount of dilimiters’? is just socrazy’the RECORD NUMBER MAYBE BOUT HERE+and then there would be more stuff.This∧ the dilimiters would:go on a further+50lines’before it hits the’MTR’

Header’there is just so’much text that : goes here and the ‘amount of dilimiters’? is just socrazy’the RECORD NUMBER MAYBE BOUT HERE+and then there would be more stuff.This∧ the dilimiters would:go on a further+50lines’before it hits the’MTR’

Thanks a million if you can help

Share

1 Answer

  1. Option Explicit
    Const magicStr = "'header="
    
    If WScript.Arguments.Count = 0 Then
      MsgBox "No Arguments; aborting", vbCritical, WScript.ScriptName
      WScript.Quit 1
    End If
    Dim fso, fIn, fOut, buff, peek, arg, strLen
    Set fso = CreateObject("Scripting.FileSystemObject")
    strLen = Len(magicStr) - 1
    
    For Each arg In WScript.Arguments
      peek = ""
      Set fIn  = fso.OpenTextFile(arg)
      Set fOut = fso.OpenTextFile(fso.GetBaseName(arg) & ".out." _
        & fso.GetExtensionName(arg), 2, True)
    
      Do Until fIn.AtEndOfStream
        buff = peek & fIn.Read(4096)
        If Not fIn.AtEndOfStream Then
          peek = fIn.Read(strLen)
        Else
          peek = ""
        End If
        buff = Replace(buff & peek, magicStr, vbNewLine & magicStr)
        fOut.Write Left(buff, Len(buff) - Len(peek))
      Loop
      fOut.Write peek
    Next 'arg
    MsgBox "Done", vbOKOnly, WScript.ScriptName

    • 0