Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Insert CRLF chars in a flat text file

Posted on 2006-11-01
3
Medium Priority
?
1,411 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 64

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 64

Accepted Solution

by:
Fernando Soto earned 900 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
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 anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

886 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