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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Map a network drive on the fly

Status
Not open for further replies.

baltman

Technical User
Mar 5, 2002
1,578
US
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.

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
 
If you use scripting you could use :
Code:
oNet = CREATEOBJECT("Wscript.Network")
oNet.MapNetworkDrive(m.cShareLetterAvailable+":", tcShare , .F.)
instead of running NET USE

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Hi Borislav,

Absolutely right, but I'd need an error catching program or at least CATCH...TRY logic then wouldn't I?

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top