Bookmark and Share

ASP Components

An ASP Server Component is a collection of code that has been made by Microsoft (advanced users can also create their own components), and included in IIS. With the use of ASP you can unlock the power of this pre-made code.

Advertise on Tizag.com

These objects can be used to do a ton of things, such as: an easy-to-use ad rotation service, an interface to a database, a means of manipulating files and much more.

Using ASP Components

Making use of Microsoft's ASP Components in your ASP programming will allow you to do so much with ASP that you'll be kicking yourself for not using components earlier. You can access these built in components by creating objects of them. See our previous lesson if you need a refresher on what ASP Objects are.

In this lesson we will be utilizing Microsoft's FileSystem Component to display all the files in our current directory. The first thing we need to do is to create a FileSystem object so we can use all the properties and methods that are in this component. Note: FSO stands for File System Object in this example.

tizagComponent.asp ASP Code:

<%
Dim myFSO
Set myFSO = _
Server.CreateObject("Scripting.FileSystemObject")
Set myFSO = nothing
%>

Accessing an ASP Component's Features

Once you have created an object of your desired component you can access all the methods and variables of that component. We have created an instance of the File System Component and stored it into myFSO. We need the folder that we're going to list all the files of and the GetFolder method of our FSO will do the job.

Using the same directory we decided to use back in the Running ASP lesson we are going to set the path of this FSO to "C:\Inetpub\wwwroot\tizagASP\", the same directory that "tizagComponent.asp" was saved in.

Updated tizagComponent.asp ASP Code:

<%
Dim myFSO, myFolder
Set myFSO = _
Server.CreateObject("Scripting.FileSystemObject")
Set myFolder = _myFSO.GetFolder("C:\Inetpub\wwwroot\tizagASP")
Response.Write("Current folder is: " & myFolder.Name)
Set myFolder = nothing
Set myFSO = nothing
%>

Display:

Current folder is: tizagASP

Finishing Up

The last thing on our to-do list is to get access to the names of the files in our working directory. The Folder object contains a method that returns a collection of all the files in the current directory. The code for accessing this collection and displaying the filenames is in the example below.

Updated tizagComponent.asp ASP Code:

<%
Dim myFSO, myFolder
Set myFSO = _
Server.CreateObject("Scripting.FileSystemObject")
Set myFolder = _myFSO.GetFolder("C:\Inetpub\wwwroot\tizagASP")
Response.Write("Current folder is: " & myFolder.Name)

For Each fileItem In myFolder.Files
	Response.Write("<br />" & fileItem.Name)
Next
Set myFolder = nothing
Set myFSO = nothing
%>

If you have done every example in this ASP Tutorial your web page produced would look like this. Notice that the files are automatically sorted alphabetically!

Display:

firtscript.asp
tizagComponent.asp
tizagEmail.asp
tizagForm.html
tizagGet.asp
tizagPost.asp
Bookmark and Share




Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!

Advertise Here

More Tutorials!
Microsoft Office Tutorials Artist Tutorials