views:

233

answers:

1

I'm using the ASP Classic ReadLine() function of the File System Object. All has been working great until someone made their import file on a Mac in TextEdit. The line endings aren't the same, and ReadLine() reads in the entire file, not just 1 line at a time.

Is there a standard way of handling this? Some sort of page directive, or setting on the File System Object?

I guess that I COULD read in the entire file, and split on vbLF, then for each item, replace vbCR with "", then process the lines, one at a time, but that seems a bit kludgy.

I have searched all over for a solution to this issue, but the solutions are all along the lines of "don't save the file with Mac[sic] line endings."

Anyone have a better way of dealing with this problem?

+3  A: 

There is no way to change the behaviour of ReadLine, it will only recognize CRLF as a line terminator. Hence the only simply solution is the one you have already described.

Edit

Actually there is another library that ought to be available out of the box on an ASP server that might offer some help. That is the ADODB library.

The ADODB.Stream object has a LineSeparator property that can be assigned 10 or 13 to override the default CRLF it would normally use. The documentation is patchy because it doesn't describe how this can be used with ReadText. You can get the ReadText method to return the next line from the stream by passing -2 as its parameter.

Take a look at this example:-

Dim sLine
Dim oStreamIn : Set oStreamIn = CreateObject("ADODB.Stream")

oStreamIn.Type = 2 '' # Text
oStreamIn.Open 
oStreamIn.CharSet = "Windows-1252"
oStreamIn.LoadFromFile "C:\temp\test.txt"
oStreamIn.LineSeparator = 10 '' # Linefeed

Do Until oStreamIn.EOS
  sLine = oStreamIn.ReadText(-2)
  '' # Do stuff with sLine
Loop

oStreamIn.Close

Note that by default the CharSet is unicode so you will need to assign the correct CharSet being used by the file if its not Unicode. I use the word "Unicode" in the sense that the documentation does which actually means UTF-16. One advantage here is that ADODB Stream can handle UTF-8 unlike the Scripting library.

BTW, I thought MACs used a CR for line endings? Its Unix file format that uses LFs isn't it?

AnthonyWJones
You are probably dead on right about the CR on Mac and LF on Unix. Honestly, I was debugging this thing via 2 remote desktop sessions and the clipboards weren't copying over. I just knew that there are differences, and I got lucky by asking early on in the debugging session what kind of machine created the file.Anthony, I just wanted to say again thank you for the time and thought you put into your answer. I really appreciate it.
Matt Dawdy