find empty drives and map them (VB6)

steveh
SilverLounger
Posts: 1952
Joined: 26 Jan 2010, 12:46
Location: Nr. Heathrow Airport

find empty drives and map them (VB6)

Post by steveh »

Hi all

Can somebody help me with some code please to find any 2 empty drives on a host PC and then map them (this is not for me but I can get more information if required)
Steve
http://www.freightpro-uk.com" onclick="window.open(this.href);return false;
“Tell me and I forget, teach me and I may remember, involve me and I learn.”
― Benjamin Franklin

User avatar
HansV
Administrator
Posts: 78527
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: find empty drives and map them (VB6)

Post by HansV »

Copy the following procedure into a module:

Code: Select all

Sub MapDrive(Path As String)
    Dim fso As Object
    Dim c As Long
    Set fso = CreateObject("Scripting.FileSystemObject")
    For c = 4 To 26
        If fso.DriveExists(Chr(64 + c) & ":") = False Then
            Exit For
        End If
    Next c
    If c = 27 Then
        MsgBox "No drive letter available!", vbExclamation
        Exit Sub
    End If
    CreateObject("WScript.Network").MapNetworkDrive _
        Chr(64 + c) & ":", Path
End Sub
This code will find the first unused drive letter starting at D: and map it to the path supplied.

You can use it like this:

Code: Select all

Sub AssignDrives
    MapDrive "\\Server1\Share1\Folder1"
    MapDrive "\\Server2\Share2\Folder2\Subfolder2"
End Sub
Best wishes,
Hans