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.
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.
codetellerComponent.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\codetellerASP\",
the same directory that "codetellerComponent.asp"
was saved in.
Updated
codetellerComponent.asp ASP Code:
<%
Dim myFSO, myFolder
Set myFSO = _
Server.CreateObject("Scripting.FileSystemObject")
Set myFolder = _myFSO.GetFolder("C:\Inetpub\wwwroot\codetellerASP")
Response.Write("Current folder
is: " & myFolder.Name)
Set myFolder = nothing
Set myFSO = nothing
%> |
Display:
|
Current folder is:
codetellerASP |
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
codetellerComponent.asp ASP Code:
<%
Dim myFSO, myFolder
Set myFSO = _
Server.CreateObject("Scripting.FileSystemObject")
Set myFolder = _myFSO.GetFolder("C:\Inetpub\wwwroot\codetellerASP")
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
codetellerComponent.asp
codetellerEmail.asp
codetellerForm.html
codetellerGet.asp
codetellerPost.asp |
|