Working with Drives and Folders

With the FileSystemObject (FSO) object model, you can work with drives and folders programmatically just as you can in the Windows Explorer interactively. You can copy and move folders, get information about drives and folders, and perform other operations on drives and folders.

Getting Information About Drives

The Drive Object enables you to obtain information about the various drives attached to a system, either physically or over a network. The following table shows the information that you can obtain by using its properties.

Information

Property

The total size of the drive in bytes.

TotalSize Property

The space available on the drive in bytes.

AvailableSpace Property or FreeSpace Property

The letter assigned to the drive.

DriveLetter Property

The type of drive, for example, removable, fixed, network, CD-ROM, or RAM disk.

DriveType Property

The drive's serial number.

SerialNumber Property

The type of file system the drive uses, such as FAT, FAT32,and NTFS.

FileSystem Property

Whether a drive is available for use.

IsReady Property

The name of the share or volume.

ShareName Property or VolumeName Property

The path or root folder of the drive.

Path Property (FileSystemObject) or RootFolder Property

For more information, see FileSystemObject Sample Code.

Example of Using the Drive Object

Use the Drive object to gather information about a drive.

The following example demonstrates how to use the Drive object. In this example, the GetDrive Method method obtains a reference to an existing Drive object (in this case, drv).

Sub ShowDriveInfo(path)
    Dim fso, drv, bytesPerGB, freeGB, totalGB, s

    s = ""
    bytesPerGB = 1024 * 1024 * 1024

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set drv = fso.GetDrive(fso.GetDriveName(path))

    s = s & drv.Path & " - "

    if drv.IsReady Then
         freeGB = drv.FreeSpace / bytesPerGB
         totalGB = drv.TotalSize / bytesPerGB

         s = s & FormatNumber(freeGB, 3) + " GB free of "
         s = s & FormatNumber(totalGB, 3) + " GB"
    Else
         s = s & "Not Ready"
    End If
    s = s & "<br />"

    document.write (s)
End Sub
function ShowDriveInfo(path) {
    var s = "";
    var bytesPerGB = 1024 * 1024 * 1024;

    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var drv = fso.GetDrive(fso.GetDriveName(path));

    s += drv.Path + " - ";

    if (drv.IsReady) {
        var freeGB = drv.FreeSpace / bytesPerGB;
        var totalGB = drv.TotalSize / bytesPerGB;

        s += freeGB.toFixed(3) + " GB free of ";
        s += totalGB.toFixed(3) + " GB";
    }
    else {
        s += "Not Ready";
    }

    s += "<br />";

    document.write(s);
}

Working with Folders

Common folder tasks and the methods for performing them are described in the following table.

Task

Method

Create a folder.

CreateFolder Method

Delete a folder.

Delete Method or DeleteFolder Method

Move a folder.

Move Method or MoveFolder Method

Copy a folder.

Copy Method (FileSystemObject) or CopyFolder Method

Determine whether a folder exists on a drive.

FolderExists Method

Get an instance of an existing Folder object.

GetFolder Method

Determine the name of a folder's parent folder.

GetParentFolderName Method

Determine the path of system folders.

GetSpecialFolder Method

For more information, see FileSystemObject Sample Code.

The following example demonstrates how to use the Folder Object and FileSystemObject Object to work with folders and obtain information about them.

Sub ShowFolderInfo()
    Dim fso, fldr, s, newLine, newFolderName
    newLine = "<br />"

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldr = fso.GetFolder("c:\")

    s = s & "Folder: " + fldr & newLine
    s = s & "Drive: " + fldr.Drive & newLine

    If fldr.IsRootFolder Then
        s = s & "Root Folder"
    Else
        s = s & "Parent Folder: " & fldr.ParentFolder
    End If
    s = s & newLine

    ' Create and delete a folder.
    newFolderName = "C:\TempFolder1"
    fso.CreateFolder(newFolderName)
    s = s & "Base Name of Added Folder: " & fso.GetBaseName(newFolderName)
    fso.DeleteFolder(newFolderName)

    s = s & newLine
    document.write(s)
End Sub
function ShowFolderInfo() {
    var s = "";
    var newLine = "<br />";

    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var fldr = fso.GetFolder("c:\\");

    s += "Folder: " + fldr + newLine;
    s += "Drive: " + fldr.Drive + newLine;

    if (fldr.IsRootFolder)
        s += "Is Root Folder";
    else
        s += "Parent Folder: " + fldr.ParentFolder

    s += newLine;

    // Create and delete a folder.
    var newFolderName = "C:\\TempFolder1";
    fso.CreateFolder(newFolderName);
    s += "Base Name of Added Folder: " + fso.GetBaseName(newFolderName)
    fso.DeleteFolder(newFolderName);

    s += newLine;
    document.write(s);
}

Change History

Date

History

Reason

September 2010

Modified examples.

Information enhancement.

September 2010

Changed references to links.

Customer feedback.