Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I convert a mapped drive to UNC path or determine type of drive?

Networking

How can I convert a mapped drive to UNC path or determine type of drive?

by  dbMark  Posted    (Edited  )
This FAQ was inspired by faq184-3137 when I was looking for ways to identify my network mapped drives as well as other drives that I might have connected. In browsing I found a MS article describing how various types of drives can be identified. The result is below.

If the program determines it is not a network mapped drive, then it drills deeper to distinguish between floppy/flash, hard drive, network, optical, RAM or other drive.
Code:
FUNCTION MappedDriveToUNC(cDrivePath)
	* support.microsoft.com/kb/135818 -- How to List Available Drives A-Z from Visual FoxPro
	LOCAL lcDrive, lcPath, lcUNC, lnUNCMaxSize, lnResult, lcRtnVal
	IF VARTYPE(cDrivePath)="C" AND ISALPHA(cDrivePath) AND IIF(LEN(cDrivePath)<=2 OR SUBSTR(cDrivePath,2,2)=":\",.T.,.F.)
		lnResult = -1
		lcDrive = UPPER(LEFT(cDrivePath,1))+":"
		lcDrvType = DRIVETYPE(lcDrive)
		lcPath = SUBSTR(cDrivePath,3)
		IF lcDrvType = 4  && network or removable drive
			lnUNCMaxSize = 400
			lcUNC = SPACE(lnUNCMaxSize)
			* lnResult values:
			* #DEFINE NO_ERROR                     0
			* #DEFINE ERROR_BAD_DEVICE          1200
			* #DEFINE ERROR_CONNECTION_UNAVAIL  1201
			* #DEFINE ERROR_EXTENDED_ERROR      1208
			* #DEFINE ERROR_MORE_DATA            234
			* #DEFINE ERROR_NOT_SUPPORTED         50
			* #DEFINE ERROR_NO_NET_OR_BAD_PATH  1203
			* #DEFINE ERROR_NO_NETWORK          1222
			* #DEFINE ERROR_NOT_CONNECTED       2250 <-- typical error for non-UNC drive mappings
			* Declare, call and clear the DLL
			DECLARE INTEGER WNetGetConnection IN WIN32API AS ConvDrvToUNC STRING @lcDrive, STRING @lcUNC, INTEGER @lnUNCMaxSize
			lnResult = ConvDrvToUNC(@lcDrive, @lcUNC, @lnUNCMaxSize)
			CLEAR DLLS "ConvDrvToUNC"
		ENDIF
		DO CASE
			CASE EMPTY(lnResult)
				lcRtnVal = ALLTRIM(LEFT(lcUNC,AT(CHR(0), lcUNC)-1)) + lcPath  && ALLTRIM() may not be needed
			CASE lcDrvType <> 4 OR lnResult = 2250
				* Invalid network resource
				lcRtnVal = "Drive "+lcDrive+" "+ ;
							ICASE(lcDrvType=0,"does not exist",lcDrvType=1, "is not mapped or has no root directory", ;
									lcDrvType=2,"is a floppy or flash disk", lcDrvType=3,"is a hard disk", ;
									lcDrvType=4,"is a network or removable drive", lcDrvType=5,"is a CD-ROM drive", ;
									lcDrvType=6,"is a RAM disk", "is an unexpected drive type")+"."
			OTHERWISE
				lcRtnVal = "Invalid system response. Win32Api Error #"+LTRIM(STR(lnResult))
		ENDCASE
	ELSE
		lcRtnVal = IIF(LEN(cDrivePath)<3,"Invalid drive letter.","Invalid path - Path must contain initial backslash.")
	ENDIF
	RETURN lcRtnVal
ENDFUNC
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top