ASP Files
All file interactions in ASP
are done through the File System
Component that is included with
IIS. It includes many objects that
give you a window into your
system's file system. Some of the
more important objects include:
- File System Object
- File Object
- Folder Object
Before you can gain access the
second and third objects we listed
above you must create the grand
daddy master File System Object (FSO).
Through this overarching object,
everything in the File System
Component can be accessed.
ASP Creating the FileSystem
Object
To create the FSO you simply
create an object reference to it
like any other object in ASP. For
a quick review on ASP Objects
check out our ASP
Object Lesson.
In the example below we create
a filesystem object and destroy
it.
ASP Code:
<%
Dim myFSO
Set myFSO =
Server.CreateObject _
("Scripting.FileSystemObject")
Set myFSO = nothing
%> |
Using the File Object
To retrieve a File Object in
ASP you must know the relative or
complete file path to the desired
file. In this example we will
assume that a file exists at "C:\Inetpub\wwwroot\codetellerASP\firstscript.asp".
myFO stands for my File Object.
This small script will get a file
object and print out the filename
using the Name property.
ASP Code:
<%
Dim myFSO, myFO
Set myFSO =
Server.CreateObject _
("Scripting.FileSystemObject")
Set myFO = myFSO.GetFile _
("C:\Inetpub\wwwroot\codetellerASP\firstscript.asp")
Response.Write("The filename
is: " & myFO.Name)
Set myFO = nothing
Set myFSO = nothing
%> |
Display:
|
The filename is:
firstscript.asp |
Using the Folder Object
To retrieve the Folder Object
you must once again supply the
relative or complete folder path.
If you followed along with our How
to Run
ASP setup
then you will have a folder
codetellerASP located at "C:\Inetpub\wwwroot\codetellerASP\".
myFolderO stands for my Folder
Object. This example is borrowed
from one of the previous lessons, ASP
Components and
it will display a list of all the
files in the "codetellerASP"
folder. We will once again be
using the Name property for both
the Folder and File objects.
ASP Code:
<%
Dim myFSO, myFolderO
Set myFSO =
Server.CreateObject("Scripting.FileSystemObject")
Set myFolderO = _
myFSO.GetFolder("C:\Inetpub\wwwroot\codetellerASP")
Response.Write("Current folder
is: " & myFolderO.Name)
For Each fileItem In
myFolderO.Files
Response.Write("<br />" &
fileItem.Name)
Next
Set myFolderO = nothing
Set myFSO = nothing
%>
|
If you have been following
along with this tutorial start to
finish your browser will display
something like this when you
execute the script.
Display:
The filename is: codetellerASP
firtscript.asp
codeteller.mdb
codetellerComponent.asp
codetellerEmail.asp
codetellerForm.html
codetellerGet.asp
codetellerPost.asp |
|