Solved

ASP ReadLine: Detect Format Of Line Breaks

Posted on 2010-08-25
6
864 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
  • 3
  • 2
6 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
Comment Utility
>>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
Comment Utility
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
Comment Utility
Nevermind...re-read your post and figured it out.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 82

Expert Comment

by:hielo
Comment Utility
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
Comment Utility
Elegant solution. Thanks!
0
 

Expert Comment

by:mharzl
Comment Utility
Line 15 is wrong

If UBound(r)=0 Then

should be

If UBound(lines)=0 Then
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now