Use WshNetwork and FileSystemObject to list, map, and disconnect mapped drives
| Downloads |
|---|
| 129966.zip |
A Universal Naming Convention (UNC) path uniquely identifies a resource on a network. It describes the location of a volume, directory, or file, using the format \\server\volume\directory\file. However, most people prefer using a mapped Windows drive letter instead of a UNC path because a mapped drive letter is easier to remember. Therefore, your scripts will likely have to deal with mapped drives and mapped resources from time to time. It's at these times that you'll want to use the WshNetwork and FileSystemObject objects.
The WshNetwork and FileSystemObject objects are a dynamic duo when you need to work with mapped drives in Windows Script Host (WSH) scripts. Besides providing the same capabilities as Windows Management Instrumentation’s (WMI’s) Win32_MappedLogicalDisk class, the WshNetwork object has additional methods for creating and removing drive mappings. The FileSystemObject object provides methods for retrieving information about system drives. With the WshNetwork and FileSystemObject objects, you can easily perform a variety of mapping tasks, as the following examples demonstrate.
Determining Whether a Share Is Mapped
The IsDriveMapped function in Listing 1 demonstrates how you can use the WshNetwork object to determine whether a share is mapped. Here’s how this function works. It first uses WshNetwork’s EnumNetworkDrives method to return a list of the mapped network drives on a computer as a collection. This collection contains a pair of items for each mapped network drive: the drive’s local name and its associated UNC path. The collection is zero-indexed so that the even-numbered items in the collection are the drive names and the odd-numbered items are the associated UNC paths. Therefore, the function iterates through every second item in the collection for comparison to the specified share. If there’s a match, the function’s return value is set to true. Otherwise, a value of Nothing is returned, which evaluates to false in Boolean comparisons.
You can download the IsDriveMapped function’s code (and the code in the other listings) by going to the top of this page and click the Download the Code Here button. To use the IsDriveMapped function in a script, you call it using code such as
' Do something.
or
' Create a new drive mapping.
Finding the Next Available Drive
Typically, you don't want to map a resource or share to an arbitrary drive letter. It makes more sense to find the next available one. Although the WshNetwork’s object's EnumNetworkDrives method is great for listing mapped drives, it's not much help in finding available drives. A better approach is to use the FileSystemObject's Drives collection, as it contains information about both local and network drives in a system.
The GetNextDrive function in Listing 2 finds the next available drive. The function begins by enumerating the ASCII codes representing the possible drive letters. With this approach, the function’s loop counter can easily convert the ASCII codes into drive letters by applying the Chr() function and appending a colon (:). For example, the ASCII code of 65, which represents the uppercase letter A, is transformed into A:.
GetNextDrive transforms one ASCII code at a time. Once transformed, the function uses the FileSystemObject object’s DriveExists method to determine whether that drive already exists. The DriveExists method returns a value of true when the drive exists and a value of false when it doesn’t exist. If the drive doesn’t exist, the function’s return value is set to that drive letter.
To use the GetNextDrive function, you need to customize the starting and ending loop indexes in the line
Const A_DRIVE = 65, Z_DRIVE = 90 so that they correspond to the available drive letter range in your system. For example, if the A and B drives are reserved for removable devices and the C drive is reserved for the first hard disk partition, you’d change this line to
Const D_DRIVE = 68, Z_DRIVE = 90 so that the function starts from the D drive. You can find a list of the ASCII codes at ASCII Code.
You can use the GetNextDrive function in code such as
strDriveLetter = GetNextDrive()<p class="Code">If Not IsEmpty(strDriveLetter) Then</p><p class="Code"> Set objNetwork = _</p><p class="Code"> WScript.CreateObject("WScript.Network")</p><p class="Code"> objNetwork.MapNetworkDrive _</p><p class="Code"> strDriveLetter, "\\Svr1\Electronic\"</p> This code uses the GetNextDrive function to find the next available drive, then maps that drive’s letter to the \\Svr1\Electronic\ share. To map the drive, the code uses the WshNetwork object’s MapNetworkDrive method, which I’ll discuss shortly.



