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

Same way we can use objFSO.FileExists to check the existence of a file at any location.

Dim FSOobj,FilePath
FilePath=Server.MapPath("text.txt") ' located in the same director
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")
if FSOobj.fileExists(FilePath) Then
Response.Write "File Exists "
Response.Write "File does not exist"
End if
Set FSOobj = Nothing

Be the first to post comment on this article :

Post your comments , suggestion , error , requirements etc here .

We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2022 All rights reserved worldwide Privacy Policy Disclaimer