Solved

Insert CRLF chars in a flat text file

Posted on 2006-11-01
3
1,406 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
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…

759 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