ASP ReadLine: Detect Format Of Line Breaks

My users are uploading a TXT file and I am using the following code:

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")	
Set objFile = objFSO.OpenTextFile(strFilePath,1,False,-1)
Line = objFile.ReadLine

Open in new window

The problem is that some are uploading a Mac text file and some are uploading a PC text file. A Hex compare of the files indicates that the EOL is 0A in one and 0D in the other.

First off, what is the proper name for each the formats?

Secondly, how can determine which is which via Classic ASP?

I tried changing x to 0,-1,-2 in the following:

Set objFile = objFSO.OpenTextFile(strFilePath,1,False,x)

Open in new window

Ultimately, when I do a readline for the Mac formatted file, the Line contains the whole text file instead of just one line like when I use a PC formatted file.

Thanks in advance for your assistance!
Who is Participating?
hieloConnect With a Mentor Commented:
>>First off, what is the proper name for each the formats?
They are not different formats. What you are experiencing is just cross-platform incompatibilities.
Each of the major platforms use different characters as a newline delimiter.

Try the following:
Dim strContents,lines,win,mac,unx
'delimiters for the various platforms
win=chr(13) & chr(10)

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFilePath, ForReading)
strContents = objFile.ReadAll

If UBound(lines)=0 Then
	If UBound(r)=0 Then
	End If
End If

For i=0 TO UBound(lines)
	'lines(i) will hold the line content
	'do whatever with it...

Open in new window

djlurchAuthor Commented:

Unfortunately, I will not know which format they are using. I need to detect it server side. Possibly this will work:

If InStr(strContents,chr(13) & chr(10)) > 0 Then
   format = win
ElseIf InStr(strContents,chr(13)) > 0 Then
   format = mac
   format = unx
End If

djlurchAuthor Commented: your post and figured it out.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

What I posted should work, regardless of which platform they are uploading from. Whatever it is that you were attempting to do with a line of text, would now be done at the place where I have:
'do whatever with it...

where "it" refers to lines(i) which is a line of text

djlurchAuthor Commented:
Elegant solution. Thanks!
Line 15 is wrong

If UBound(r)=0 Then

should be

If UBound(lines)=0 Then
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.