Microsoft KB Archive/244676

= How to map, delete, and enumerate network drives using the Windows Script Host =

Article ID: 244676

Article Last Modified on 8/8/2007

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 3.0b Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition
 * Microsoft Visual FoxPro 7.0 Professional Edition
 * Microsoft Visual FoxPro 8.0 Professional Edition
 * Microsoft Visual FoxPro 9.0 Professional Edition

-



This article was previously published under Q244676



SUMMARY
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:

http://msdn2.microsoft.com/en-us/library/ms950396.aspx

.



MORE INFORMATION
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.
 * 1) DEFINE CRLF          CHR(13)+CHR(10)
 * 2) DEFINE TAB       CHR(9)
 * 3) DEFINE lcNetworkShare  '\\Servr\Share'

ON ERROR DO errorhandler WITH ERROR,LINENO,MESSAGE LOCAL colDrives, strMsg PRIVATE strDrive,strShare,WSHNetwork WSHNetwork = CREATEOBJECT("WScript.Network") colDrives = WSHNetwork.EnumNetworkDrives strMsg = ''

IF colDrives.COUNT > 0 FOR i = 0 TO colDrives.COUNT - 1 STEP 2 IF !EMPTY(colDrives.ITEM[i]) && drive letter is mapped strMsg = strMsg + CRLF + colDrives.ITEM[i] + TAB + colDrives.ITEM[i + 1] ENDIF ENDFOR =MESSAGEBOX(strMsg) ENDIF
 * !* Windows 2000 adds "Network Places" to list

strDrive = 'Z:' strShare = lcNetworkShare WSHNetwork.MapNetworkDrive( strDrive, strShare)
 * !* Note: If drive letter is already mapped, or cannot find the path, it will throw first error
 * !* 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

PROCEDURE errorhandler LPARAMETERS nError, nLine, cmessage IF nError=1429 &&error mapping drive? DO CASE 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 WSHNetwork.RemoveNetworkDrive(strDrive,lforce,lupdateprofile) OTHERWISE MESSAGEBOX(MESSAGE) ENDCASE ENDIF

