KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to get a list of currently mapped drives into 4D on Windows?
PRODUCT: 4D Developer | VERSION: 11 | PLATFORM: Win
Published On: June 12, 2008

Windows operating systems have the ability to map network drives to logical drive letters. The following code shows an example of how to get a list of currently mapped drives into 4D. This particular example populates two arrays with the results. The UNCdrive_at array stores the drive letters and the UNCpath_at array stores the corresponding UNC path.



C_TEXT(outputStream;inputStream;errorStream;$sep_t;$CRLF;$noMore_t;$continue_t;$tmpTestThisChar_t)
C_LONGINT($firstOK_l;$firstNL_l;$lastPos_l;$tmpPosFound_l;$tmpLength_l;$tmpLengthFound_l)
C_LONGINT($tmpLengthOfUNC_l;$lastCharFound_l;$counter_l)
ARRAY TEXT(foundUNCs_at;0)
ARRAY TEXT(UNCdrive_at;0)
ARRAY TEXT(UNCpath_at;0)
$sep_t:="-------------------------------------------------------------------------------"
outputStream:=""
inputStream:=""
errorStream:=""
$CRLF:=Char(Carriage return )+Char(Line feed )
LAUNCH EXTERNAL PROCESS("NET USE";inputStream;outputStream;errorStream)
$noMore_t:=""
$lastPos_l:=1
$numFound_l:=0

 
 ` put each line starting with OK into an array element
Repeat
   $firstOK_l:=Position("OK";outputStream;$lastPos_l)
   $firstNL_l:=Position($CRLF;outputStream;$firstOK_l)
   If ($firstOK_l>$lastPos_l)
      $lastPos_l:=$firstNL_l
      APPEND TO ARRAY(foundUNCs_at;Substring(outputStream;$firstOK_l;($firstNL_l-$firstOK_l)))
      $numFound_l:=$numFound_l+1
   Else
      $noMore_t:="yup"
   End if
Until ($noMore_t="yup")

 
 ` stip everything before the drive letter (13 chars in)
For ($counter_l;1;Size of array(foundUNCs_at))
   $tmpLength_l:=Length(foundUNCs_at{$counter_l})-13
   foundUNCs_at{$counter_l}:=Substring(foundUNCs_at{$counter_l};14;$tmpLength_l)
End for

  ` Strip out "Microsoft Windows Network" from the array elements
For ($counter_l;1;Size of array(foundUNCs_at))
   $tmpPosFound_l:=Position("Microsoft Windows Network";foundUNCs_at{$counter_l};1;$tmpLengthFound_l)
   If ($tmpPosFound_l#0)
      foundUNCs_at{$counter_l}:=Substring(foundUNCs_at{$counter_l};1;$tmpPosFound_l-1)
   End if
End for


  ` Split in to 2 arrays of drive letters and UNC mappings
For ($counter_l;1;Size of array(foundUNCs_at))
   APPEND TO ARRAY(UNCdrive_at;Substring(foundUNCs_at{$counter_l};1;2))
   APPEND TO ARRAY(UNCpath_at;Substring(foundUNCs_at{$counter_l};11))
End for   

` strip out the trailing white spaces from the UNCpath_at array elements
For ($counter_l;1;Size of array(UNCpath_at))
   $continue_t:="yes"
   Repeat
      $tmpLengthOfUNC_l:=Length(UNCpath_at{$counter_l})
      $lastCharFound_l:=Position(" ";UNCpath_at{$counter_l};$tmpLengthOfUNC_l)
      $tmpTestThisChar_t:=Substring(UNCpath_at{$counter_l};$lastCharFound_l)
      If ($tmpTestThisChar_t=" ")
         UNCpath_at{$counter_l}:=Substring(UNCpath_at{$counter_l};1;$lastCharFound_l-1)
      Else
         $continue_t:="no"
      End if
   Until ($continue_t="no")
End for