• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 907
  • Last Modified:

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!
0
djlurch
Asked:
djlurch
  • 3
  • 2
1 Solution
 
hieloCommented:
>>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)
mac=chr(13)
unx=chr(10)

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

lines=Split(strContents,win)
If UBound(lines)=0 Then
	lines=Split(strContents,unx)
	If UBound(r)=0 Then
		lines=Split(strContents,mac)
	End If
End If
objFile.Close

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

Open in new window

0
 
djlurchAuthor Commented:
Interesting.

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
Else
   format = unx
End If


0
 
djlurchAuthor Commented:
Nevermind...re-read your post and figured it out.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
hieloCommented:
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

Regards,
Hielo
0
 
djlurchAuthor Commented:
Elegant solution. Thanks!
0
 
mharzlCommented:
Line 15 is wrong

If UBound(r)=0 Then

should be

If UBound(lines)=0 Then
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now