VBScript does not read Cr

When I use Classic ASP to read text files I find that the CrLf(s) are not read.  Does not follow all the examples that I see on-line.  Can anyone tell me why ??

Neither of these methods does not seem to work:
fil.ReadAll

'or

do while not fil.AtEndOfStream
       stuff = stuff & fil.ReadLine()
Loop

'Only way that does work is:

do while not fil.AtEndOfStream
       stuff = stuff & fil.ReadLine() & vbcrlf
Loop
sidwelleAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
You answered your own question.  If you are reading a text file such as a csv file, you have to add the line feed (vbcrlf) as  you are doing.   If you are reading in a text file that has line feeds and paragraphs, it may be better to replace the line feeds with a br tag.

stuff = fil.ReadAll
stuff = replace (stuff,vbcrlf,"<br>")

Open in new window

From a previous question, I have something similar to show you.

<form action="" method="post">
<textarea name="content" cols="80" rows=10"></textarea><br>
<button type="submit">Post</button>

</form>
<hr>
<%
if request.form<>"" then
	response.write "your posted data"
	response.write request.form
	response.write "<hr>"
	content = request.form("content")
	newContent = replace(content, vbCrlf,"<br>")
	response.write content&"<hr>"&newContent
end if
%>

Open in new window

You can test out that code here http://mypadas.com/ee/wb/.  Note that if you add a line feed in the text box by using your enter key, the result shown in the response.write request.form for each line feed is "%0D%0A". If you look at a hex chart http://www.cisco.com/c/en/us/td/docs/ios/12_2/configfun/command/reference/ffun_r/frf019.pdf you can see that hex 0D is a carriage return  and 0A is a line feed. The carriage return and line feed is hidden when you just response.write request.form("content") but once you replace it with a br tag using replace(content, vbCrlf,"<br>") it works as expected.
0
sidwelleAuthor Commented:
I will try to be more clear. If I read the contents of a file with the following code, it always returns '0'

stuff = fil.ReadAll
response.write("Lines: "  & instr(stuff, vbcrlf))

This Method returns the correct line count:

stuff = ""
do while not fil.AtEndOfStream
        stuff = stuff & fil.ReadLine() & vbcrlf
Loop
response.write("Lines: "  & instr(stuff, vbcrlf))
0
Big MontyWeb Ninja at largeCommented:
The ReadAll() method reads in the entire method as a single string, ignoring line breaks. if you want to account for line breaks, then you need to use the ReadLine method, just like you're doing
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

sidwelleAuthor Commented:
Yes, but line Breaks are part of the file.  Why are they not included.
In the examples that I find on-line, the examples are written as if the line breaks are there !
Why are they not included when I read the file ?
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
The answer you are being given by replacing the vbcrlf works if there are actual line breaks in the text file.

Note the results of the code below http://mypadas.com/ee/readtext/ while reading reading the text file http://mypadas.com/ee/readtext/test.txt.   When using instr to find vbcrl does work as noted in the 2nd example of the code below.  There must be something about the way your text file is formatted where the line breaks are not full carriage return + line feed.  
<%
dim fs,t,x
set fs=Server.CreateObject("Scripting.FileSystemObject")


set t=fs.OpenTextFile(server.MapPath("test.txt"),1,false)
x=t.ReadAll
t.close
Response.Write("The text in the file is:<br>" & x)

response.write("<hr>Lines: "  & instr(x, vbcrlf))

response.write "<hr> using line breaks"
x=replace(x,vbcrlf,"<br>")
response.write x
%>

Open in new window


If this is not working, open your text file in a text editor that can find hidden characters such as hard or soft returns.  It is very possible that there is not an actual carriage return + line feed that is creating the lines.

Instead of vbCrlf, try using vbCR or vbLF and see if that works.
0
sidwelleAuthor Commented:
I will view it with a hex editor tomorrow and post back.
0
sidwelleAuthor Commented:
Checked it, every line is terminated with a CrLf.

I have seen this before, don't understand why. Like it's a setting somewhere in the environment ??
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Try pulling the sample text I used and see if you get the same result.  

Also upload a sample of the text file here for us to try out.
test.txt
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sidwelleAuthor Commented:
Seems I have code problem somewhere.  I attached the two code samples that I used to test and it worked as expected.  I will search some more and post back.
ReadTest.vbs
ReadTest.asp.txt
0
sidwelleAuthor Commented:
Thanks for the help .
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.