Free Guides

Language Tutorials


           

ASP ( Active Server Pages )

                 Index              

Your Ad Here

 

 

File and Directory handling

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

Determining the existence of file or directory by using FileSystemObject in ASP

We can check whether file of folder exists at a location by using file system object ( FileSystemObject). This is often required to check the existence of a file or directory before using, otherwise system will generate an error message which is not good to display to the visitors. So before using any file or folder we can check the status of them.

FileSystemObject or fso uses physical path of the file or folder to check the existence so before using this we have to convert the virtual path to physical path for our folders or files by using server.mappath object.

We will use one variable to hold the instance of the FileSystemObject and using this we will check the existence of the file or folder.
 

Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

Next we will use the If Then Else statement to check the condition of the file or folder.

If objFSO.FolderExists(Server.MapPath("db")) Then
Response.Write "Folder Exists"
else
Response.Write "Folder does not exit"
End if

 

Here “db” is the name of the folder we are checking the existence. We have used the command Server.MapPath to change the virtual path to physical path of the folder db.

We can print the command based the response of the object ( True or False ) and display the message accordingly.

 

How to get current ASP script running file name without using FileSystem object

We can get the current file name of the asp script without using the FileSystem object. The reason for this is we want to develop a common script which can be used ( or included ) in any file to display various file system properties. The FileSystem object needs the file name as input so we will try to develop this script which can be used as a input to the FileSystem object for further development.

The best example is to develop a script which will display the last modified date of the file. Let us start with how to read the script name in ASP. The code used below will give the file name along with the directory or the path name.
 

Dim my_array, fname
fname = Request.ServerVariables("SCRIPT_NAME")

The above code will display the file name with full path. For example if the present file is in my_file directory and file name is test.asp then the output will be

/my_file/test.asp

To get the file name out of the above full path string we have to break the string and create an array by using split function with “/” as delimiter. Once the array is created we can get the last element of the array to get the file name. Here we will be using Ubound function to get the size of the array and we will use that to get the last element of the array.

Here is the full code to display the current file name
 

Dim my_array, fullname, fname
Response.Write Request.ServerVariables("SCRIPT_NAME") & "<br>"
fullname = Request.ServerVariables("SCRIPT_NAME")
my_array=split(fullname,"/")
fname=my_array(ubound(my_array))
Response.Write fname
 


 

How to get last updated & created date and time of an ASP file

We will try to develop a code for showing the last modified date and the created date of the file and display to visitors. This code has to work on it own without any input or changed settings.

We will use the FileSystemObject for getting the last changed or updated along with created date of the file. To make this code universal we will not use name of the file as input to this FileSystemObject , it its place we will use code to get the current file name and then use that as input to FileSystemObject. This way we can make it independent of file name setting and the same code can be used inside any asp file without any changes.

Before reading this it is advisable to read the FileSystemObject and how to get the file name of the current script running page. With this knowledge we can add more code to get the last updated time of the file.

Here is the code to get the current file name and FileSystemObject created.
 

Dim objFSO, objFile, fname, fullname, my_array
fullname = Request.ServerVariables("SCRIPT_NAME")
my_array=split(fullname,"/")
fname=my_array(ubound(my_array))

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(Server.MapPath(fname))
 

To these lines we will add the file name and DateCreated and DateLastModified to get the full code to display the modified time.
 

Response.Write "Created date of file "& objFile.Name & " = " & objFile.DateCreated Response.Write "
Last Modified date of file "& objFile.Name & " = " & objFile.DateLastModified


 

Getting the list of files inside a directory by using FileSystemObject in ASP

By using FileSystemObject (FSO)we can list out all the files of a directory. Here we will be using server MapPath to map the virtual path to real path as used by file system object. After initiating the object we can instantiate the folder object. We will be using this folder object Files property to get all the files present within the folder.


Before this it is advisable to learn the FileSystemObject, Server MapPath to convert virtual path to real path before reading this.


In our example we have used the folder name my_folder which is inside the directory name t so the path of the folder is

http://mysite.com/t/my_folder/

Here is the code.
 

Dim objFSO, objFile, objFolder

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath("/t/my_folder"))

For Each objFile in objFolder.Files
Response.Write objFile.Name & "<br>"
Next
Set objFolder = Nothing
Set objFSO = Nothing

 

Displaying last updated date along with the file name
We can add the last modified date of the file by adding the DateLastModified property of the File object. This one line we will add inside the For Each Loop.
 

For Each objFile in objFolder.Files
Response.Write objFile.Name & " : "
Response.Write objFile.DateLastModified & "<br>"
Next