Wise ASP Logo Surfer's Choice Member
   

Wise ASP - FileSystemObject Object

The FileSystemObject object provides access to the computer's file system, allowing us to manipulate text files, folders and drives from within our code. It's part of the Scripting Object. We can create an instance of the FileSystemObject in JScript using:
var fso = Server.CreateObject("Scripting.FileSystemObject") 

The Folder Object

  The Folder object provides access to the file system on a specified drive. You can also the Folder to traverse directories on a particular drive. After you create an instance of the FileSystemObject object, you need to declare a variable that will server as your Folder object. Then you need to use the GetFolder method to get your specified folder. Each directory object has a property called Files, which is a collection of File objects. This represents the list of files the directory specified by your variable folder.

Here is some JScript code to list all the files in a particular directory, called "c:\inetpub\wwwroot" :

var myPath = "c:\inetpub\wwwroot"; 
var fso = Server.CreateObject("Scripting.FileSystemObject"); 
var folder = fso.GetFolder(myPath); 
var fileCollection = folder.Files; 
var items = new Enumerator(fileCollection); 
while(!items.atEnd()) { 
  i = items.item() 
  Response.write(i.name + " -- "); 
  Response.write(i.DateLastModified + "<br>"); 
  items.moveNext(); 
} 

  

Each File object provides access to all the properties of a file.

PropertyDescription
AttributesReturns the attributes of the file.
DateCreatedReturns the date and time that the was created.
DateLastAccessedReturns the date and time that the was last accessed.
DateLastModifiedReturns the date and time that the was last modified.
DriveReturns the drive letter.
NameSets or returns the name of the file.
PathReturns the path.
SizeReturns the size of the file.
TypeReturns the information about the type of file.
 
MethodDescription
CopyCopies the specified folder from one location to another.
MoveDeletes a specified folder.
DeleteMoves a specified folder from one loation to another.

Drive Object

  The FileSystemObject object contains one property, Drives, which returns a collection consisting of all Drive objects available on the local machine. Here is the code in JScript which produces a list of drives on a server with drive types and letters and volume names:
var fso = Server.CreateObject("Scripting.FileSystemObject"); 
var drives = fso.drives; 
var items = new Enumerator(drives); 
while(!items.atEnd()) { 
  i = items.item() 
  Response.write("<tr>"); 
  Response.write("<td>" + i.DriveLetter +  "</td>"); 
  Response.write("<td>" + i.DriveType + "</td>"); 
  Response.write("<td>" + i.VolumeName + "</td>"); 
  Response.write("<tr>"); 
  items.moveNext(); 
} 
To see more on how to use the JScript Enumerator, please click here.

Please Note: If you don't have a disk in your disk drive, or a CD ROM in your CD Drive, then you will experience a run-time error "Disk Not Ready Error".   

Each item of the Drives collection is a Drive object. Here is a list of properties and methods of the Drive object:

PropertyDescription
AvailableSpaceReturns the amount of available on specified drive.
DriveLetterReturns the drive letter for the specified drive.
Drive TypeReturns the value indicating the type of drive specified.
0 Unknown
1 Removable
2 Fixed
3 Network
4 CD-ROM
5 RAM Disk
FileSystemReturns the type of file system for specified drive.
FreeSpaceReturns the amount of free space available on specified drive.
IsReadyReturns boolean value indicting if drive is ready or not.
PathReturns the path for specified drive.
RootFolderReturns the Folder object representing the root folder of the specified drive.
Serial NumberReturns a decimal serial number used to uniquely identify a disk volume.
ShareNameReturns the network share name for the specified drive.
TotalSizeReturns the total space in bytes of the specified drive.
VolumeNameReturns the volume name of the specified drive.

  That's the end. Feel free to cut and paste this code and use it yourself. Thanks for reading and feel free to jump around Wise ASP using the select box at the top of the screen.

Related Information

88x31cardsvisoranm