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

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
0
sidwelle
Asked:
sidwelle
  • 6
  • 3
1 Solution
 
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 MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
 
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
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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