#1 17 Oct 2006 20:11

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,013

Faster drive mappings

I recently noticed the robust drivemap.vbs script (http://ss64.com/vb/syntax-drivemap.html) can still be a little slow when you map multiple drives, by slow I mean a second or two.

The reason for this is that calling the script so:

CSCRIPT drivemap.vbs <drive1>
CSCRIPT drivemap.vbs <drive2>

will involve loading CSCRIPT several times.

Below is a slight rewrite using a SUB to map multiple drives in a single Cscript instance. This runs pretty much instantanously.

The one disadvantage is that by including all the mapping details in the script it becomes that much less Generic and reusable, perhaps someone can suggest a better approach?

' DriveMaps.vbs
' Map network drives

' Usage
'    cscript DriveMap.vbs //NoLogo
' This script will remove any existing drive map to the same drive letter
' including persistent or remembered connections (Q303209)

Option Explicit

call DriveMap ("U:","\\MyServer\users\JSMITH")
call DriveMap ("S:","\\MyServer\Shared Area")
call DriveMap ("W:","\\MyServer\workgroups")

Sub DriveMap (drive,share)

Dim objNetwork, objDrives, objReg, i
Dim strLocalName, strRemoteName, strShareConnected
Dim bolFoundExisting, bolFoundRemembered
Const HKCU = &H80000001

strLocalName = drive
strRemoteName = share
bolFoundExisting = False

' Check parameters make sense
If Right(strLocalName, 1) <> ":" OR Left(strRemoteName, 2) <> "\\" Then
 wscript.echo "Syntax error - specify the drive letter and UNC path to share"
End If

wscript.echo " - Mapping: " + strLocalName + " to " + strRemoteName

Set objNetwork = WScript.CreateObject("WScript.Network")

' Loop through the network drive connections and disconnect any that match strLocalName
Set objDrives = objNetwork.EnumNetworkDrives
If objDrives.Count > 0 Then
  For i = 0 To objDrives.Count-1 Step 2
    If objDrives.Item(i) = strLocalName Then
      strShareConnected = objDrives.Item(i+1)
      objNetwork.RemoveNetworkDrive strLocalName, True, True
      bolFoundExisting = True
    End If
End If

' If there's a remembered location (persistent mapping) delete the associated HKCU registry key
If bolFoundExisting <> True Then
  Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  objReg.GetStringValue HKCU, "Network\" & Left(strLocalName, 1), "RemotePath", strShareConnected
  If strShareConnected <> "" Then
    objReg.DeleteKey HKCU, "Network\" & Left(strLocalName, 1)
    Set objReg = Nothing
    bolFoundRemembered = True
  End If
End If

'Now actually do the drive map (not persistent)
On Error Resume Next
objNetwork.MapNetworkDrive strLocalName, strRemoteName, False

'Error traps
If Err <> 0 Then
  Select Case Err.Number
    Case -2147023694
      'Persistent connection so try a second time
      On Error Goto 0
      objNetwork.RemoveNetworkDrive strLocalName, True, True
      objNetwork.MapNetworkDrive strLocalName, strRemoteName, False
      WScript.Echo "Second attempt to map drive " & strLocalName & " to " & strRemoteName
    Case Else
      On Error GoTo 0
      WScript.Echo " - ERROR: Failed to map drive " & strLocalName & " to " & strRemoteName
  End Select
End If

Set objNetwork = Nothing

End Sub

' Simon Sheppard, SS64.com, Oct 2006
' Credit (for the difficult parts) to 
' Kenneth MacDonald, Edinburgh University Computing Services.

' You are free to use or modify this script: Creative Commons Attribution 2.5 License.


#2 02 Jan 2007 17:19

From: UK
Registered: 29 Dec 2006
Posts: 1,077

Re: Faster drive mappings

Why not just modify the script so that it can receive multiple shares and/or the location of a data file as a parameter, which would be parsed and processed accordingly?

For example:

cscript drivemap.vbs "c:\MyDrives.dat"

Above would process mappings as listed in c:\MyDrives.dat

- OR -

To process mappings specified at the command line, one could use:

cscript drivemap.vbs "U:\\MyServer\users\JSMITH"

The implication being that the paths are parsed such that "\\" following a colon will instruct the script that this is a share to map, rather than a file to process.  Thus, for maximum flexibility, one could, with a bit of code trickery, process any combination of multiple share locations and data files given at the command line.

My VB skills ain't much cop (truth be told, they're essentially non-existent) ... so I can't offer any code, but if it's of any consolation (I'm sure it's not), I could do this as described above without too much problem in a .cmd script smile


Last edited by bluesxman (02 Jan 2007 17:21)

cmd | *sh | Ruby | Chef


#3 26 Jan 2007 16:34

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,013

Re: Faster drive mappings

Yes, as you say it just needs a bit more manipulation in the VB script, I've not had much time to play with this yet, and for now it's quicker to just to hard code the values into the VBS.

i.e. instead of separate text files listing the mappings for different groups, I just have separate copies of drivemap.vbs as above

Passing all the long filenames as parameters could be ideal, but may possibly run into command line limits if you had a lot of drives to map:
Under Windows 2000, the command line is limited to 2046 characters.
Under Windows XP, the command line is limited to 8190 characters.


Board footer

Powered by FluxBB