FileSystemObject: Reading a file
Read, ReadLine, ReadAll, Skip, SkipLine, Column, Line, AtEndOfLine, AtEndOfStream

We will consider a file with the following content:
 

learn.txt
1234567890
abcdefghij
ABCDEFGHIJ

We will read this file in different ways in order to use various commands available with filesystemobject.
 
 

Read (number)  Will read number of characters specified (after reading position in the file will change)
<%
Set fso = CreateObject("Scripting.FileSystemObject") 
Set a = fso.OpenTextFile(Server.MapPath("\") & "/learn.txt") 

mystring=a.Read(4) 
Response.Write (mystring)

Response.Write ("<hr>")

mystring=a.Read(4) 
Response.Write (mystring)
 

Set a = Nothing 
Set fso = Nothing 
%>

1234
5678
ReadLine (will read content in one line of the text file)
<%
Set fso = CreateObject("Scripting.FileSystemObject") 
Set a = fso.OpenTextFile(Server.MapPath("\") & "/learn.txt") 

mystring=a.readLine
Response.Write (mystring & "<BR>")
mystring=a.readLine
Response.Write (mystring & "<BR>")
mystring=a.readLine
Response.Write (mystring & "<BR>")

Set a = Nothing 
Set fso = Nothing 
%>

1234567890
abcdefghij
ABCDEFGHIJ
ReadAll (will read all the content in the text file)
<%
Set fso = CreateObject("Scripting.FileSystemObject") 
Set a = fso.OpenTextFile(Server.MapPath("\") & "/learn.txt") 

mystring=a.readAll
Response.Write (mystring & "<hr>")

Response.Write ("<pre>" & mystring & "</pre>")
 

Set a = Nothing 
Set fso = Nothing 
%>

1234567890abcdefghijABCDEFGHIJ
1234567890
abcdefghij
ABCDEFGHIJ
Skip (number) Skip number of characters specified (after reading position in the file will change)
<%
Set fso = CreateObject("Scripting.FileSystemObject") 
Set a = fso.OpenTextFile(Server.MapPath("\") & "/learn.txt") 

a.Skip(5)
mystring=a.read(8) 
Response.Write mystring
Response.Write "<BR>"
a.Skip(1)
mystring=a.read(8) 
Response.Write mystring

Set a = Nothing 
Set fso = Nothing 
%>
<HR>Line break is two characters

67890 a
cdefghij
Line break is two characters
SkipLine Skips line in the text file (after reading position in the file will change)
<%
Set fso = CreateObject("Scripting.FileSystemObject") 
Set a = fso.OpenTextFile(Server.MapPath("\") & "/learn.txt") 

a.SkipLine
mystring=a.readLine 
Response.Write mystring

Set a = Nothing 
Set fso = Nothing 
%>

abcdefghij
Column / Line  will return position in the text file
<%
Set fso = CreateObject("Scripting.FileSystemObject") 
Set a = fso.OpenTextFile(Server.MapPath("\") & "/learn.txt") 

a.Skip(15)
ColumnNumber=a.Column
Response.Write ("ColumnNumber: " & ColumnNumber & "<BR>")
LineNumber=a.Line
Response.Write ("LineNumber: " & LineNumber & "<BR>")

Set a = Nothing 
Set fso = Nothing 
%>

ColumnNumber: 4
LineNumber: 2
AtEndOfLine Will let us know when the line ends
<%
Set fso = CreateObject("Scripting.FileSystemObject") 
Set a = fso.OpenTextFile(Server.MapPath("\") & "/learn.txt") 

While not a.AtEndOfLine
 Response.write(a.Read(1) & "<BR>")
Wend

Set a = Nothing 
Set fso = Nothing 
%>

1
2
3
4
5
6
7
8
9
0
AtEndOfStream Will let us know when the file ends
<%
Set fso = CreateObject("Scripting.FileSystemObject") 
Set a = fso.OpenTextFile(Server.MapPath("\") & "/learn.txt") 

While not a.AtEndOfStream
 Response.write(a.readline & "<BR>")
Wend

Set a = Nothing 
Set fso = Nothing 
%>

1234567890
abcdefghij
ABCDEFGHIJ

 

AspTutorial.info.