You are currently offline, waiting for your internet to reconnect

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

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:.
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.
#DEFINE CRLF	       CHR(13)+CHR(10)#DEFINE TAB	       CHR(9)#DEFINE lcNetworkShare   '\\Servr\Share'ON ERROR DO errorhandler WITH ERROR(),LINENO(),MESSAGE()LOCAL  colDrives, strMsgPRIVATE strDrive,strShare,WSHNetworkWSHNetwork = CREATEOBJECT("WScript.Network")colDrives = WSHNetwork.EnumNetworkDrivesstrMsg = ''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]        ENDIF    ENDFOR    =MESSAGEBOX(strMsg)ENDIFstrDrive = 'Z:'strShare = lcNetworkShare*!*	Note:  If drive letter is already mapped, or cannot find the path, it will throw first errorWSHNetwork.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 StrPasswordPROCEDURE 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				
REFERENCES
For the latest information about the Windows Script Host, visit the following Microsoft Developer Network (MSDN) Web site:
Properties

Article ID: 244676 - Last Review: 08/08/2007 06:48:10 - Revision: 3.5

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

  • kbcodesnippet kbhowto kbmisctools KB244676
Feedback
r varAutoFirePV = 1; var varClickTracking = 1; var varCustomerTracking = 1; var Route = "76500"; var Ctrl = ""; document.write(" anguage-'+language.locale}}" href="" ng-click="setLanguage(language);" class="ng-binding" id="language-es-uy">Uruguay - Español
대한민국 - 한국어
España - Español
Paraguay - Español
Venezuela - Español
://c1.microsoft.com/c.gif?DI=4050&did=1&t="> var varCustomerTracking = 1; var Route = "76500"; var Ctrl = ""; document.write(" ')[0].appendChild(m);" onload="var m=document.createElement('meta');m.name='ms.dqp0';m.content='false';document.getElementsByTagName('head')[0].appendChild(m);" src="http://c1.microsoft.com/c.gif?"> ne; " src="https://c1.microsoft.com/c.gif?DI=4050&did=1&t=">