Solved

Insert CRLF chars in a flat text file

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

13 Experts available now in Live!

Get 1:1 Help Now