About Me

Saturday, 21 April 2012

ASP Files

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:
  1. File System Object
  2. File Object
  3. 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\nullvoidASP\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\nullvoidASP\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 tizagASP located at "C:\Inetpub\wwwroot\nullvoidASP\".
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 "tizagASP" 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\tizagASP")

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: tizagASP
firtscript.asp
nullvoid.mdb
nullvoidComponent.asp
nullvoidEmail.asp
nullvoidForm.html
nullvoidGet.asp
nullvoidPost.asp

0 comments:

Post a Comment