S
S
Sigbjorn2015-09-22 15:59:15
System administration
Sigbjorn, 2015-09-22 15:59:15

How to automatically assign a free letter to a network drive in a VBS script?

Good day! There is a script for connecting network drives:

On Error Resume Next

Set objWshNetwork = CreateObject( "WScript.Network" )
Set objShell = CreateObject("Shell.Application")

MapDrive "V:", "\\ftech", "FTech"

Sub MapDrive(strDrive, strPath, strName)
   On Error Resume Next
   objWshNetwork.RemoveNetworkDrive strDrive, vbTrue, vbTrue
   objWshNetwork.MapNetworkDrive strDrive, strPath, vbTrue
   objShell.NameSpace(strDrive).Self.Name = strName
End Sub

How can I make it assign a free drive letter by itself, for example, when three drives are connected?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2015-09-22
@Sigbjorn

You will have to sort through all the letters until you find a free one.
Example :
strUNC = "\\server\share"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive AvailableDriveLetter, strUNC
'--------------------- ----------------------------
Function AvailableDriveLetter
With CreateObject("Scripting.FileSystemObject")
For i = Asc("D") To Asc ("Z")
If Not .DriveExists(Chr(i)) Then
AvailableDriveLetter = Chr(i) & ":"
Exit For
End If
Next
End With
End Function

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question