FSO (File System Object ): 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


Google






   



MSN Nick Name



More Resources...





Most Viewed Services:
  1. HTML Tutorial
  2. XHTML Tutorial
  3. CSS Tutorial
  4. Javascript Tutorial
  5. DHTML Tutorial
  6. VB Script
  7. TCP/IP Tutorial
  8. ADO Tutorial
  9. MYSQL Tutorial
  10. ASP Tutorial
  11. AJAX Tutorial
  12. CFML Tutorial
  13. PHP Tutorial
  14. WML Tutorial
  15. FLASH Tutorial
  16. XML Tutorial
  17. RSS Tutorial
  18. SQL Tutorial
  19. HTML Articles
  1. Javascript Articles
  2. PHP Articles
  3. SEO Articles
  4. Web Design Articles
  5. SEO Tips
  6. Web Design Tips
  7. Articles
  8. CSS
  9. CSS Tips
  10. HTML Tips
  11. JAVASCRIPT Tips
  12. MYSQL Tips
  13. PHP Tips
  14. Money
  15. Tutorials
  16. Web Hosting



  • Home
  • Web Directory
  • Top Directoriers
  • Webmaster Directories
  • Contact
  • © Copyright 2006-2010 All Rights Reserved By CodeDcode.Com : HTML : RSS : TEXT : XML