Solved

Insert CRLF chars in a flat text file

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

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 62

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

815 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now