Solved

Insert CRLF chars in a flat text file

Posted on 2006-11-01
3
1,403 Views
Last Modified: 2008-01-09
Hi,

I am in urgent need of the following. Should be quite simple but I am in a hurry so:

I have a flat text file that has no end of line character (no LF or CRLF). It's all in one line.

So I need to insert a CRLF at position 768 for line 1 and at all its multiple until end of file is read.

Thanks !
0
Comment
Question by:scml
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Expert Comment

by:Shakti109
ID: 17853279

One possible way :


Imports System.Text

private sub SplitFile()

Dim new_Str as new stringbuilder
Dim FileString as string
Dim FileProcessed as boolean

FileProcessed = False

'Open file, read to end into string.
Dim MyFileReader as new streamreader("PATH TO FILE")
FileString = MyFileReader.readtoend
MyFileReader.close()

' Loop through the string take 768 characters, add a chr(13) which is a return and add this to a stringbuilder
Do while FileProcessed <> true
   if len(FileString) => 768 then
     new_str.append(mid(filestring,1,768)) & chr(13)
     FileString.remove(1,768)
   else
      new_str.append(FileString)
      FileProcessed = true
   end if
Loop
   
' Write out the new "stuff"
Dim MyFileWriter as new streamwriter("PATH TO NEW FILE")
MyFileWriter.writeline(new_str.tostring)
MyFileWriter.flush
MyFileWriter.close

end sub
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17853457
Hi scml;

This Regex solution will do what you need.

Imports System.IO
Imports System.Text.RegularExpressions

        Dim sr As New StreamReader("C:\Temp\OneLine.txt")
        Dim input As String = sr.ReadToEnd()
        sr.Close()

        Dim output As String
        output = Regex.Replace(input, "(.{768})", "$1" & Environment.NewLine)

        Dim sw As New StreamWriter("C:\Temp\OnlineModified.txt")
        sw.Write(output)
        sw.Close()


Fernando
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 300 total points
ID: 17853497
Hi scml;

If you need to set the position of the NewLine, CrLf, to a different position just change the 768 in the below line to the character position where you want to insert the NewLine.

    output = Regex.Replace(input, "(.{768})", "$1" & Environment.NewLine)

So if you want to set it to position 801 it would look like this.

    output = Regex.Replace(input, "(.{801})", "$1" & Environment.NewLine)

Fernando
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question