Login | Register | FAQ
Anonymous

Lesson 14: The File System Object

With the DocumentationFileSystemObject you can access the server's filesystem. This allows you to manipulate drives, folders and text files in ASP scripts.

For example, you can use the {0}{1/}FileSystemObject{/0} to read or write a text file. Or you can list all files in a specified folder. There are many possibilities and the FileSystemObject can save you lots of tedious work.

Here, we'll look at how you can use the FileSystemObject to work with drives, folders and files. The goal is to give you a quick overview. In the next lessons, we will look more closely at the different possibilities with the FileSystemObject.

Drives

We start by looking at the computer's drives. Not because this is what you will be using the most, but because it's sensible to start with drives before digging into the topic of folders and files.

In the example below, we get all the drives on the server listed. This is done by the FileSystemObject, which creates an array with the drives. Each drive letter is then written as we run through the array.


	<html>

	<head>
	<title>FileSystemObject</title>
	</head>
	<body>
	<%
	' Variables

	Dim FSO, d, dc

	' FileSystemObject
	Set fso = CreateObject("Scripting.FileSystemObject")

	' Array of drives
	Set dc = fso.Drives

	' Loop through the array and write the drive letter
	For Each d in dc
	   Response.Write d.DriveLetter & "<br />" 

	Next
	%>

	</body>
	</html>
	
	

Now we have listed all the drives on the server. It is also possible to retrieve additional information about each drive. For example, let's look closer at the E drive and examine what type of drive it is. For this, we use the property DocumentationDriveType:

	<html>

	<head>
	<title>FileSystemObject</title>
	</head>
	<body>
	<%

	' Variables
	Dim fso, d, t

	' FileSystemObject
	Set fso = CreateObject("Scripting.FileSystemObject")

	' Object Drive
	Set d = fso.GetDrive("E:")

	' Find drive type and set description

	Select Case d.DriveType
	   Case 0: t = "Unknown"
	   Case 1: t = "Removable"
	   Case 2: t = "Fixed"

	   Case 3: t = "Network"
	   Case 4: t = "CD-ROM"

	   Case 5: t = "RAM Disk"
	End Select
	Response.Write "Drive" & dDriveLetter & ": is a " & t & " drive"

	%>
	</body>

	</html>
	
	

At this time, we will not go into more depth with drives. See DocumentationDrive Object in the VBScript documentation if you want more information on the different properties, etc.

Folders

The FileSystemObject also allows you to work with folders on the server. But before we begin to look at the DocumentationFolder object, we need to be able to find the physical location of a folder or file.

The problem is that your website normally is hosted on a web-hosting server. Therefore, you probably don't know the physical location of a folder or a file. But help is near: You can use the method DocumentationServer.Mappath to find the physical location of any file or folder.

For example, to find the physical location of the folder we are in right now, we write:


	<%
	Response.Write Server.Mappath("/tutorials/asp/")
	%>
	
	

Which returns:

	d:\www2\html\tutorials\asp

	
	

We now know that we are in the folder "www2" on drive d at the web server - a very nifty feature!

Let's now use this method together with the FileSystemObject and the FolderObject. We will not go through all the different possibilities - only a few selected. Again, see the documentation for a complete listing.

DocumentationDateCreated
Returns the date and time when a file or folder was created.
DocumentationDateLastModified
Returns the date and time when a file or folder was edited/modified.
DocumentationSize
Returns the size of a file or a folder (including all files and subfolders) in bytes.

Let us try to find the three properties of the folder we are in now, which is http://www.html.net/tutorials/asp/.


	<html>
	<head>
	<title>FileSystemObject</title>
	</head>
	<body>
	<%

	' Variables

	Dim fso, f, folderspec

	' Find the physical location of the folder
	folderspec = Server.Mappath ("/tutorials/asp/")

	' FileSystemObject
	Set fso = CreateObject("Scripting.FileSystemObject")

	' Folder object

	Set f = fso.GetFolder(folderspec)

	' Find and write properties
	Response.Write "<h1>Folder: " & folderspec & "</h1>"

	Response.Write "<p>Was created: " & f.DateCreated & "</p>"

	Response.Write "<p>Last edited: " & f.DateLastModified & "</p>"

	Response.Write "<p>Size: " & f.Size & " bytes</p>"
	%>
	</body>

	</html>
	
	

Files

Just as with folders, you can use the FileSystem object to find the properties of a file.

In exactly the same way as above, we can find the same properties for the file you are looking at now: http://www.html.net/tutorials/asp/lesson14.asp.


	<html>
	<head>
	<title>File System object</title>
	</head>
	<body>
	<%
	' Variables

	Dim fso, f, filespec

	' Find the physical location of the file
	filespec = Server.Mappath("/tutorials/asp/lesson4.asp")

	' FileSystemObject
	Set fso = CreateObject("Scripting.FileSystemObject")

	' File object
	Set f = fso.GetFiles(filespec)

	Find and write properties

	Response.Write "<h1>The file: " & filespec & "</h1>"

	Response.Write "<p>Was created: " & f.DateCreated & "</p>"
	Response.Write "<p>Last edited: " & f.DateLastModified & "</p>"

	Response.Write "<p>Size: " & f.Size & " bytes</p>"
	%>
	</body>

	</html>
	
	

In the next lesson, we will continue to look at the FileSystem object. Among other things, we will look at how to read from and write to a text file.



<< Lesson 13: Cookies

Lesson 15: Reading from a text file >>