Link to home
Start Free TrialLog in
Avatar of rickh082997
rickh082997

asked on

Replace line-feeds with carriage return / line feed

I have a system that receives ASCII text files from various outside systems. The problem is that Unix systems terminate each line with a line feed and windows terminates with a carriage return and line feed. The difference is causing some problems in a program that processes the files. How would one go about parsing the file and replacing the terminators with CR / LF. This sounds easy enough, but what I don't know is how VB does it. How do you view the contents of a file in terms of ASCII characters. Also is there an existing routine that does what I need.
Thanks, RickH
Avatar of AzraSound
AzraSound
Flag of United States of America image

if you have vb6 you just use replace function:

Replace(string,vbLf,vbCrLf)
w/o vb6 try this:

Private Sub Command1_Click()
    Dim i As Long
    Dim tempStr, finalStr As String
    For i = 1 To Len(text)
        tempStr = Mid$(text, i, 1)
        If tempStr = vbLf Then
            finalStr = finalStr & vbCrLf
        Else
            finalStr = finalStr & tempStr
        End If
    Next
    text = finalStr
End Sub

Avatar of Joe_Griffith
Joe_Griffith

I think VB would read the file ok as is.  You said it is causing problems.  What are the problems?
Avatar of rickh082997

ASKER

I have VB6. I'll give that a try. Thanks
I'm not 100% sure what's going on, but here's what's happening: When I get a file from a Unix system, the contents of the file are strung out on one line. (There is some sort of separator between each line - usually a small rectangular block - sometimes a tilde). One of the things my VB program does is go to the last line of the file and check to see if it has a certain value I'm looking for. Since the file is now looked at as only one line, the program erroneously thinks that the line it's looking for is not there.
Another symptom of the problem is that when I view the Unix-produced file in NotePad it is strung out on one line. When I view it in WordPad it looks normal - multiple lines. So even though VB may sometimes process these files, I am having some viewing problems later. I need to be able to view the files consistently.
you should be able to read this single line into a string and check what the rightmost characters are using the Right function or other string manipulation depending on what you are looking for.  did you the replace function do what you had asked?
To get around this I use a program "unix2dos".

I shell out to this program to do the unix conversion.  

I have to deal with large ascii files 300mb +  In vb I would have to use a byte array and read it byte by byte.  If i would use line input or scripting's readline I would run out of memory.  As it reads the whole file as one line

this program formats the file so I can use readline.

http://shareware.cnet.com/shareware/0-13628-500-1268762.html?tag=st.sw.16165_501_1.lst.titledetail
Avatar of Brendt Hess
How are you retrieving the files?  If you are using FTP, then you should be able to have the FTP Client do the conversion on the fly for you.  Just ensure that you don't transfer ASCII files using a Binary transfer - use a Text / ASCII transfer, and the conversion will be done on receipt.
I am receiving the files with a windows cgi program (written in VB) over the internet (http).
ASKER CERTIFIED SOLUTION
Avatar of mcrider
mcrider

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Before you do anything else I think you need to determine what the offending characters really are.  It seems more likely to me that it is a carriage return rather than a line feed.  They both look the same in notepad.  If you don't have some sort of hex editor to look at the file directly then I would do something like this:

Open FileName For Input as #1
Line Input#1,temp
For i=1 to len(temp)
  char=mid$(temp,i,1)
  debug.print char;asc(char)
next
close#1

Then look at the result for linefeeds (10) and carriage returns (13) or whatever odd character is there.
Joe... Every unix system I have *ever* dealt with natively stores end-of-line markers as 0x0A which is a linefeed...
Comment accepted as answer
Excellent suggestions from everybody. mcrider's code works best for my situation.
Thanks, RickH
Thanks for the points! Glad I could help!


Cheers!®©