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

Article translations Article translations
Article ID: 244676 - View products that this article applies to.
This article was previously published under Q244676
Expand all | Collapse all

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.
#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
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
*!* 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)
ENDIF

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

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
				

REFERENCES

For the latest information about the Windows Script Host, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/ms950396.aspx

Properties

Article ID: 244676 - Last Review: August 8, 2007 - Revision: 3.5
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
Keywords: 
kbcodesnippet kbhowto kbmisctools KB244676

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com