I have a third party application that I call and have to pass paramaters to including a path. This particular application chokes on a UNC even though the user's behavior may cause the UNC to be passed. Additionally, there is no garantee that the share is mapped to a drive letter.
The following script converts a UNC to a drive letter based full path and automatically maps the drive to the first available drive letter after "D" if it isn't already mapped.
The following script converts a UNC to a drive letter based full path and automatically maps the drive to the first available drive letter after "D" if it isn't already mapped.
Code:
LPARAMETERS tcShare
LOCAL i, oWS, oDrives, cShareLetterAvailable, lcCmd
IF VARTYPE(m.tcShare)<>"C" OR LEFT(m.tcShare,2)<>"\\"
RETURN "Failure to Identify a server."
ENDIF
m.tcShare = ALLTRIM(LOWER(m.tcShare))
m.cShareLetterAvailable = ""
FOR m.i = 69 TO 90
m.cShareLetterAvailable = m.cShareLetterAvailable + CHR(m.i)
ENDFOR
oWS = Createobject("WScript.Network")
oDrives = oWS.EnumNetworkDrives
FOR m.i = 0 To oDrives.Count - 1 Step 2
IF ALLTRIM(LOWER(oDrives.Item(m.i+1))) == m.tcShare
WAIT WINDOW NOWAIT m.tcShare + " Mapped To " + (oDrives.Item(m.i))
RETURN (oDrives.Item(m.i))
ELSE
m.cShareLetterAvailable = CHRTRAN(m.cShareLetterAvailable,LEFT((oDrives.Item(m.i)),1),"")
ENDIF
ENDFOR
m.cShareLetterAvailable = LEFT(m.cShareLetterAvailable,1)
WAIT WINDOW NOWAIT "Failure to Identify Mapping. Attempting to map share to " + m.cShareLetterAvailable
m.lcCmd = Getenv("ComSpec") +" /C Net Use " + m.cShareLetterAvailable + ": " + CHR(34) + tcShare +CHR(34) + ;
" > " + ADDBS(GETENV("TEMP"))+"MapResult.txt"
oWS = Createobject("wscript.shell")
oWS.Run(m.lcCmd, 0, .T.)
IF FILETOSTR(ADDBS(GETENV("TEMP"))+"MapResult.txt")="The command completed successfully."
*great
WAIT WINDOW NOWAIT "Share Mapped to: " + m.cShareLetterAvailable
RETURN m.cShareLetterAvailable+":"
ELSE
MESSAGEBOX("all hope is lost")
ENDIF