Home  |  Latest News  |  Contribute  |  F.A.Q.  |  Account  |  Scripts | About
Search ASPNut:
 
ASP Reference
Server Variables
ASP Glossary
Related Sites

Free Components

HTML Reference
Web Colors
Entities
CSS Level 1
Encode/Decode
Glossary

Our Sponsors
ASP Nuke CMS
Free Auctions
PHP 5 Script
Funktastic Blog
ULost Directory
Team Task

 

 

 

 

 

 

 

 

 

 

 

 

FileSystemObject Basics
9/8/2003 10:53:58 AM - Kenneth Richards

This article explains how you can use the built-in FileSystemObject to persist data. This alternative may be more desirable than using a relational database such as Microsoft Access or SQL Server.

In a future article, we will discuss how you can structure your data files to store multiple data fields or properties related to a single object. If you are comfortable doing this already, then this article will be all you need.

Folder Permissions



Before we begin, you must make sure that you have the proper permissions on your server to setup "write access" for the web service user. This is required for creating and updating files on the server (through ASP scripting.) If you have full admin rights to the server then you can skip this section since you definitely have access.

If you have "shell access" to your web server using Windows Terminal Server or some other client, you can connect to the server to check your permissions. Simply open up Windows Explorer and browse to the folder where you want to write data to. If you do not already have a folder, then you should create a new one.

Right-click on the folder and select "Properties..." This will show you all of the properties for the folder. You need to go to the "Security" tab to view permissions for the folder you clicked. Next you will need to click the "Add..." button underneath "Group or Usernames" box. This allows you to add a new user with permissions to the folder.

You will need to select the Internet user which corresponds to the user account the web service runs under. This is typically "IUSR_COMPUTERNAME". If you are on a network and you are seeing all of the users on the domain, you will need to change the "Location" to the local machine.

After you add the internet user to the Groups and Users, you will need to select it and set the permissions. Setup full permissions for the user for the specified folder. Be careful because the security permissions are inheritted by any child folders. It is generally a bad idea to give full permissions to the web user to any folder on the site (more about that in a later article) so the best practice is to make sure there are no sub-folders for the one you define.

If you cannot see the "Security" tab, then you will need to change your Explorer options. Go to "Tools -> Folder Options -> View" in the main menu. Find the checkbox for "Use simple file sharing" and make sure this is cleared. Click the "Apply" button to finalize this change. You should now be able to see the security tab.

Check if Folder Exists



The following code will allow you to check if a folder exists on the local filesystem. It is useful if you want to not only create and update files, but also create new sub-folders to help organize your data files.


Dim oFSO

' create FileSystemObject
Set oFSO=Server.CreateObject("Scripting.FileSystemObject")
' check if folder exists
If Not oFSO.FolderExists(Server.MapPath("/data/abc")) Then
' do something
End If


Using code such as this, you could easily create a procedure to automatically create folders that don't already exist when a file is being saved. You can have this code repeated (recursively) to create a folder structure of any depth.

Creating a New Folder



Once you have determined that a folder does not exist, you may want to create the folder. Especially if you are writing the procedure to automatically create folders in the pathname as described previously.


On Error Resume Next
Set oFolder=oFSO.CreateFolder(Server.MapPath("/data/abc"))
If Err.Number <> 0 Then
strError = "Unable to create folder: /data/abc
" &_
Err.Number & " - " & Err.Description
End If
On Error Goto 0


Read an Entire File



If you already have a file created that you want to read, it is a pretty simple matter to do so. Using the "OpenTextFile" method of the FileSystemObject and the "ReadAll" method of the File object, we can easily accomplish this task:


Dim oFSO, oFile, sContents

Const VB_FORREADING = 1
Const VB_FORWRITING = 2

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile(Server.MapPath(sPathName), _
VB_FORREADING)
sContents = oFile.ReadAll
Set oFile = Nothing
Set oFSO = Nothing


In practice, it is a wise idea to encapsulate all FileSystemObject method calls with an error handler using the "On Error Goto..." statement. Otherwise, your script will throw an error if it's expecting a file and the file doesn't exist.

Creating a New File



The process of creating a new file is very simple. In order for this operation to succeed, you must make sure that you have setup the folder permissions as described in the previous section. Otherwise, your script will throw an error when the call to "CreateTextFile" is made.


Dim oFSO, oFile, sPathName, sContents

sPathName = "/data/abc/info.dat"
Set oFSO = CreateObject("Scripting.FileSystemObject")
' False param indicates "overwrite if file already exists"
Set oFile=oFSO.CreateTextFile(Server.MapPath(sPathName),_
False)
oFile.Write(sContents)
oFile.Close
Set oFile = Nothing
Set oFSO = Nothing


Also, make sure that the path exists (is valid) by using the "FolderExists" method to test each folder within the path. I wrote a procedure to perform these checks recursively by iterating over the path structure. You may optionally want to create the folders along the path as you go through the iteration.

Updating an Existing File



If you want to update an existing file, you use code almost exactly the same as when you create a new file. You simply change the parameter that indicates whether you want to allow the code to overwrite an existing file.


Dim oFSO, oFile, sPathName, sContents

sPathName = "/data/abc/info.dat"
Set oFSO = CreateObject("Scripting.FileSystemObject")
' True param indicates "overwrite if file already exists"
Set oFile=oFSO.CreateTextFile(Server.MapPath(sPathName),_
True)
oFile.Write(sContents)
oFile.Close
Set oFile = Nothing
Set oFSO = Nothing


The same file permissions are required when updating a file as when you are creating a new file. So make sure your folder permissions are setup properly.

When working with text files, it is necessary to rewrite the entire file every time a modification is made. In a later article we will discuss binary files and how modifications can be made without re-writing the entire file.

Summary



The FileSystemObject is a simple abstraction of the underlying API calls the operating system uses to manipulate files. It is very reliable (when combined with the proper error handling routines) and will allow you to store large amounts of data.

The FileSystemObject is quite fast and for simple operations will match that of a dedicated database such as Microsoft Access or SQL Server. It is an excellent option when you don't have access to a database and need a way to persist data. This type of data storage can be used to create dynamic web applications such as a guest book, message forums or a classified ad system.

Related Links



Microsoft (MSDN) FileSytemObject Reference

<< Return to Latest News

Orvado Technologies

ASP Nuke CMS
Free Open Source
Content Manager
HomeFix Boards
Home Remodeling
Message Boards

Contact Us | Contribute | About | Site Map



"I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image." - Stephen Hawking

©2010 San Diego Web Design - Orvado Technologies, All Rights Reserved
ASP Nut provides articles and reference documentation for Active Server Page developers.