Solved

ASP ReadLine: Detect Format Of Line Breaks

Posted on 2010-08-25
6
878 Views
Last Modified: 2013-09-25
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
Comment
Question by:djlurch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 33527115
>>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
 
LVL 1

Author Comment

by:djlurch
ID: 33527752
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
 
LVL 1

Author Comment

by:djlurch
ID: 33527791
Nevermind...re-read your post and figured it out.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 82

Expert Comment

by:hielo
ID: 33535056
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
 
LVL 1

Author Closing Comment

by:djlurch
ID: 33535223
Elegant solution. Thanks!
0
 

Expert Comment

by:mharzl
ID: 39521500
Line 15 is wrong

If UBound(r)=0 Then

should be

If UBound(lines)=0 Then
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question