Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default CR LF problem

I am using a user defined function, ReadTextFile(), to read
the contents of a text file into a string variable. The basic
code looks like this:

Dim s as string
s = ReadTextFile("C:\data.txt")

Now that the entire text file is stored in a string variable,
I would like to replace occurences of multiple carriage
returns (CR) or line feeds (LF) with a single carriage return.

In other words, I want all paragraphs to be single-spaced,
with only one CR and/or LF between paragraphs.

Finally, I need to write that single-space string to a new file,
and preserve the single-spaced format in the new file.

The biggest problem here, is that my text files come from
various sources, and each source uses different byte
representations of "carriage returns". I have noticed in some
files that a line break is represented by one CR (or
character 13 or hex "0D")....other files use CR-CR-LF
(or 13-13-10 or hex "0D-0D-0A") to represent a line break.
I think some files only use CR-LF ("0D-0A"), etc....

Does anyone know any good algorithms or functions that
can help replace any sequence of combinations of CR
and/or LF with just one CR LF line break?

Thank you

'-------------------------------------------------
' BTW, here is the ReadTextFile() function again:
Public Function ReadTextFile$(Filename$)
' Reads large amounts of data from a text file in one single step.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default CR LF problem (BTW)

I forgot to mention..... I would like all line break characters
to be the same throughout the entire string. Using
CRLF or vbCrLf will be fine. Or, just one CR is good.

As long as it's consistent all throughout the string.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default CR LF problem (BTW)

I forgot to mention..... I would like all line break characters
to be the same throughout the entire string. Using
CRLF or vbCrLf will be fine. Or, just one CR is good.

As long as it's consistent all throughout the string.


Ok, so if you say there are possibly 2 cr characters together, but only
1 LF character regardless of how many Cr chars, then...

'delete all CRs
s = Replace(s, vbCr, "")

'replace all LFs with CRLFs
s = Replace(s, vbLf, vbCrLf)

OR

s = Replace(Replace(s, vbCr, ""), vbLf, vbCrLf)

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel startup problem. Possible problem with stdole32.tlb Walter Briscoe Excel Programming 2 September 26th 11 07:09 PM
Colon at the end of excel file name(ex: problem.xls:1, problem.xls financeguy New Users to Excel 2 January 15th 10 01:15 AM
Started out as an Access problem. Now an Excel problem RobertM Excel Discussion (Misc queries) 2 April 26th 06 07:30 PM
Problem when multipple users access shared xl-file at the same time, macrocode for solve this problem? OCI Excel Programming 0 May 16th 04 10:40 PM


All times are GMT +1. The time now is 05:26 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"