Thursday, January 04, 2007

FileSystemObject::Working With Drives And Folders

If you are writing scripts for active server pages or Window scripting Host, and if you will be manipulating files and drives, then you will find yourself dealing with the filesystemobject, which enables you to carry out tasks like writing, reading and deleting of files. With the filesystemobject you also have access to drives and their properties.

I will succinctly highlight here, how to work with this object.

One thing though, since FSO is contained in the scripting type library, you must make sure that scrrun.dll is available in your working directory.

So having said that let us start. This post will cover the Object under the following subtopics:

Working with Drives
Working with Folders

Working with files will be dealt with in another post.


Working With Drives.

The first thing to do, since we want to work with FSO is to create the filesystemobject. It’s ActiveXobject functionality, so the syntax to create it is:

var myfso = new ActiveXobject("Scripting.FileSystemObject");

Since we want to work with the drive, the next thing is to get a handle on the drive we want to work on. Use the ‘GetDrive()’ method of the filesystemobject to do that. That is:

myfso.GetDrive('D:');

If I want to work with the drive D:

Having gotten a hold on the drive you want to manipulate, i.e. by writing:
var mydrive = myfso.GetDrive('D:');

you can make use of the following properties:
  • mydrive.TotalSize //gives you the total size of the drive in bytes
  • mydrive.FreeSpace //to get the free space in drive. myfso.AvailableSpace will also work.
  • mydrive.DriveLetter //to get letter assigned to drive i.e the drive without the ':'
  • mydrive.DriveType //this properties returns integers that indicates the kind of drive it is. for example, 1 is for removable, 2 is for fixed, while 4 is for CD-Rom
  • mydrive.SerialNumber //serial number of the drive
  • mydrive.IsReady //if the drive is available for use. Returns 'true' if it is
    available and 'false'if otherwise.
  • mydrive.ShareName //this returns the shared name for the drive
  • mydrive.VolumeName //gives you the volume name

Working With Folders

There are to ways to get a handle on a folder you want to work with. The first method is to create it, that is:

var myfolder = myfso.createFolder('c://geekabyte');

will create a folder named geekabyte in the root drive 'c:'

The second method is to get an handle on an existing folder, that is:

var myfolder = myfso.GetFolder('c://geekabyte');

Having had a hold on the folder, you have the availability of the following methods and properties.

  • myfso.DeleteFolder(handler to the folder);
    For example if you want to delete an existing folder named 'deathstar', first thing is to get to the folder. You do that by:

    var thefolder = myfso.getFolder('E://deathstar');

    now to delete it,just write:

    myfso.DeleteFolder(thefolder);

    or instead of going through the process of using the .getFolder() method, just write:

    myfso.DeleteFolder('E://deathstar');

  • Retrieve the name of a folder
    To get the name of a folder instead of the the path that getFolder() returns, use the '.name' property. It looks like this:
    //first get an handle on the folder
    var folder = myfso.getFolder('c://geekabyte');
    //to retrieve name...
    var name_of_folder = folder.name;

  • Finding out if a folder exist.
    You might want to check whether or not a folder exist.To do this, you use the .FolderExist() method of the filesystemobject.
    example.

    var returnval = myfso.FolderExists('c://geekabyte');

    if the folder exists, the method returns 'true' otherwise, 'false'

  • Finding out the name of a parents folder
    If you have a folder and you want to get the name of that parents folder, use:

    var parent_folder = myfso.GetParentFolderName('c://geekabyte/blog');

    This expression will assign 'geekabyte' to parent_folder.

No comments: