The Microsoft Windows Script Host is a language-independent scripting host for 32-bit Windows operating system platforms. Windows Script Host is integrated into Microsoft Windows 98, Windows 2000, Windows ME, Windows XP, Windows 2003 Server, and Microsoft Internet Information Server 4.0. The Windows Script Host is included with Microsoft Windows NT 4.0 Option Pack. To download it, visit the following Microsoft Developer Network (MSDN) Web site:
This program demonstrates how to map a network drive and then enumerate through the mapped drives. The program deletes a mapped drive if it exists. To run this example, copy the code below into a new program file, change the third #DEFINE to a valid network share on your network, and then run the program.
#DEFINE CRLF CHR(13)+CHR(10)
#DEFINE TAB CHR(9)
#DEFINE lcNetworkShare '\\Servr\Share'
ON ERROR DO errorhandler WITH ERROR(),LINENO(),MESSAGE()
LOCAL colDrives, strMsg
WSHNetwork = CREATEOBJECT("WScript.Network")
colDrives = WSHNetwork.EnumNetworkDrives
strMsg = ''
IF colDrives.COUNT > 0
FOR i = 0 TO colDrives.COUNT - 1 STEP 2
*!* Windows 2000 adds "Network Places" to list
IF !EMPTY(colDrives.ITEM[i]) && drive letter is mapped
strMsg = strMsg + CRLF + colDrives.ITEM[i] + TAB + colDrives.ITEM[i + 1]
strDrive = 'Z:'
strShare = lcNetworkShare
*!*Note: If drive letter is already mapped, or cannot find the path, it will throw first error
WSHNetwork.MapNetworkDrive( strDrive, strShare)
*!* There are 3 other parameters that are Optional- bUpdateProfile,strUser,strPassword
*!* bUpdateProfile-Update the user profile
*!* strUser and strPassword-If you need credentials of another user for the share, specify strUser and StrPassword
LPARAMETERS nError, nLine, cmessage
IF nError=1429 &&error mapping drive?
CASE "network path was not found" $ cmessage &&common error so handle it if you want
MESSAGEBOX( "Network path not found" )
CASE "local device name is already in use" $ cmessage &&common error so handle it if you want
MESSAGEBOX( "Local device name is already in use - I am going to delete it!" )
lforce = .T. && force removal even if resource is in use
lupdateprofile = .T. && Update the profile
Article ID: 244676 - Last Review: Jun 20, 2008 - Revision: 1