SQL PHP HTML ASP JavaScript articles and free scripts to download
 

Reading data of a file by ASP File system object

Using file System object we can read the content of a file. As we have already discussed FileSystemObject is used to check the presence of the file before we want to read.

While reading the content of the file we can use three different ways to collect the data. One is by specifying number of chars we are interested in collecting or reading.

Response.Write OpenFileobj.Read(3)


The above line will collect 3 characters from the file to read the next 3 again we have to use the same command. As we are not aware of how many characters are present inside the file so we will use a loop to collect all the data in a set of 3 chars. Here we have to check that we have not reached the end of the file. We will get this error message
Error Type:
Microsoft VBScript runtime (0x800A003E)
Input past end of file

If there are two chars left then our last command should collect the two chars and should not try again. To do this we will use command OpenFileobj.AtEndOfStream . This will became true once the file end is reached.

Do While Not OpenFileobj.AtEndOfStream


We can also read line by line by changing the read command to ReadLine. Here is the code to read one line each time.

Response.Write OpenFileobj.ReadLine


Same way we can read all the content of the file by using a single command ReadAll like this

Response.Write OpenFileobj.ReadAll


This is to be used when the file we are reading is not big as this command uses lot of memory to store all the data.

Here is the code used to read the data from a text file. You can see we have used Server.MapPath to map the path of the file we are going to read.

<%
Dim OpenFileobj, FSOobj,FilePath
FilePath=Server.MapPath("text.txt") ' located in the same director
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")
if FSOobj.fileExists(FilePath) Then
Set OpenFileobj = FSOobj.OpenTextFile(FilePath, 1)

Do While Not OpenFileobj.AtEndOfStream
Response.Write OpenFileobj.ReadLine & "<br>"
Loop

OpenFileobj.Close
Set OpenFileobj = Nothing
Else
Response.Write "File does not exist"
End if
Set FSOobj = Nothing
%>
Further readings
Server.MapPath to convert virtual path to physical path of files and directory.
FileSystemObject:Checking file or folder exist by using File system object .
Getting the file name from full path of the current running script in ASP.
Getting the file extension and file name by using FSO
Deleting file by using DeleteFile in FSO
Getting the Last updating and creation date and time of a file.
Listing all files present inside a folder using FileSystemObject
Reading data from a text file using File System Object
Writing data to a text file using File System Object
Read & Writing data to a text file: Developing a page hit counter
Appending data to a text file: Storing emails in a text file
 
Scripts
PHP
JavaScript
All ASP Tutorials
File Management
MapPath
FileSystemObject
append
name
Updating Time
Listing all files
Reading file
Writing file
Read & Write
File type
Delete File
Popular Tutorials
Managing two drop downs
ASP Tutorials
Date and time
Declaring array
Form in ASP
Server.MapPath
Date Time & MSSQL
Select Query
File System Object
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.