Home

ASP ADVANCED

This section explores the methods for working with drives and folders.  You learn how to retrieve information about the drives on the current machine.  You also learn how to create, copy, move, delete, and list the contents of folders.

Working with Drives

There are two objects that you can use to retrieve information about the drives on the local machine: the FileSystemObject object and the Drive object.  For example, the following Active Server Page displays a list of all the drives on the server and their total size and available size :

<HTML>
<HEAD><TITLE>Drive List</TITLE></HEAD>
<BODY>
<%
'   Create an instance of the FileSystemObject object
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
'   Loop through the Drives collection
FOR EACH thing in MyFileObject.Drives
%>
<BR>Drive Letter: <%=thing.DriveLetter%>
<BR>Drive Total Size: <%=thing.TotalSize%>
<BR>Drive Available Space: <%=thing.AvailableSpace%>
<HR>
<%
NEXT
%>
</BODY>
</HTML>

The Drives collection of the FileSystemObject object contains the collection of all the available drives on the server.  However, it contains only those drives that have been mapped to a drive letter.

These are the methods of the FileSystemObject object related to drives:

  • DriveExists (DriveSpecifier).  Returns TRUE if the specified drive exists.
  • Drives.  Returns the collection of drives for the local machine.
  • GetDrive (DriveSpecifier).  Returns a Drive object that represents the drive specified.
  • GetDriveName (Path).  Returns a string that contains the drive for the path specified.

Not surprisingly, the Drive object also contains a number of methods and properties that are useful for working with drives :

  • AvailableSpace.  Returns the space available on the drive in bytes.
  • DriveLetter.  Returns the letter of the drive--for example, C:, D:, or E:.
  • DriveType.  Returns a number corresponding to the type of the drive--for example, a CD-ROM or removable drive.
  • Freespace.  Returns the amount of free space on the drive in bytes (normally the same as AvailableSpace).
  • ISReady.  Indicates whether a volume is ready to be used.  This property is useful for indicating the state of removable drives.
  • Path.  Indicates the path of the drive.
  • RootFolder.  This property returns a Folder object representing the root folder on the drive.
  • SerialNumber.  Returns the serial number of the drive.
  • ShareName.  Returns the share name of the drive.
  • Totalsize.  Returns the total size of the drive in bytes.
  • VolumeName.  Returns a string representing the volume name of the drive.

To use these properties and methods, you need to create an instance of the Drive object.  You can do this by using the GetDrive( ) method of the FileSystemObject object.  The following example returns the volume name of the C: drive:

<%
'   Create an instance of the FileSystemObject object
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
'   Create an instance of the Drive object
Set MyDrive=MyFileObject.GetDrive("c:")
Response.Write(MyDrive.VolumeName)
%>

Working with Folders

This section shows how to manipulate folders and display their contents.  To work with folders, you can use both the FileSystemObject object and the Folder object.  This example displays all the files in a folder with the path C:\myfolder:

<HTML>
<HEAD><TITLE>Folder Contents</TITLE></HEAD>
<BODY>
<%
'   Create an instance of the FileSystemObject object
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
'   Create a folder object
Set MyFolder=MyFileObject.GetFolder("c:\myfolder")
'   Loop through the Files collection
FOR EACH thing in MyFolder.Files
Response.Write("<P>"&thing)
NEXT
%>
</BODY>
</HTML>

In this example, a Folder object is created by using the GetFolder( ) method of the FileSystemObject object. Once the Folder object is created, the FOR...NEXT loop is used to loop through its Files collection.  The page displays all the files in this collection.

The FileSystemObject object includes a number of methods for working with folders.  The following list provides a brief explanation of how these methods can be used:

  • CopyFolder source, destination, [overwrite].  This method copies a folder from one location to another.  You can use wildcards in the source parameter to copy multiple folders at the same time. By default, if the folder already exists, it will be overwritten.  Set overwrite to FALSE to prevent this from happening.
  • CreateFolder FolderSpecifier.  Creates the specified folder.
  • DeleteFolder FolderSpecifier.  Deletes a folder and all its contents.  You can use wildcards to delete multiple folders at the same time.
  • FolderExists (Folderspecifier).  Returns TRUE if the folder exists, FALSE otherwise.
  • GetFolder (Folderspecifier).  Returns a Folder object that represents the folder specified.
  • GetParentFolderName(Path).  Returns a string containing the path of the parent folder.
  • MoveFolder source, destination.  Moves a folder from one location to another.  You can use wildcards in the source parameter to move more than one folder at a time.

To use any of these methods, you need to first create an instance of the FileSystemObject object.  The following example (next page) creates a folder, moves it, and then deletes it:

<%
'   Create an instance of the FileSystemObject object
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
'   Create a new folder
MyFileObject.CreateFolder "c:\newfolder"
'   Move the folder
MyFileObject.MoveFolder "c:\newfolder", "c:\oldfolder"
'   Delete the folder
MyFileObject.DeleteFolder "c:\oldfolder"
%>

The methods and properties of the Folder object can also be used to manipulate folders.  Here's a brief explanation of the properties and methods of the Folder object:

  • CopyFolder newcopy, [Overwrite].  Copies the current folder to a new location.  If Overwrite is set to FALSE, an error occurs if the folder already exists.
  • DeleteFolder.  Deletes the current folder and any of its contents.
  • Files.  Returns the collection of Files contained in the folder.  Hidden Files are not revealed.
  • ISRootFolder.  Returns TRUE if the folder is a root folder.
  • MoveFolder FolderSpecifier. Moves the folder from one location to another.
  • Name.  Returns the name of the folder.
  • ParentFolder.  Returns the parent folder.
  • size.  Returns the size of a folder and all its subfolders in bytes.
  • SubFolders.  Returns the collection of subfolders of the current folder.

To use any of these methods, you need to first create an instance of the Folder object.  This example returns a list of all the subfolders of the folder with the path c:\myfolder:

<%
'   Create an instance of the FileSystemObject object
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
'   Create an instance of the Folder object
Set MyFolder=MyFileObject.GetFolder("c:\myfolder")
FOR EACH thing IN MyFolder.SubFolders
Response.Write(thing)
NEXT
%>

Summary

This chapter described how to use the objects of the File Access component.  You learned how to read and write to text files and how to work with the methods and properties of the File object.  Finally, you learned how to use the Folder and Drive objects to manipulate folders and retrieve information about the drives on your server.