Re: In VB Find First Available Unassigned Drive Letter by Al
Al
Sun May 11 14:32:00 CDT 2008
"Pegasus (MVP)" <I.can@fly.com.oz> wrote in message
news:%23uXxHH5sIHA.524@TK2MSFTNGP05.phx.gbl...
>
> "Al Dunbar" <AlanDrub@hotmail.com.nospaam> wrote in message
> news:e5wBJ$4sIHA.5872@TK2MSFTNGP04.phx.gbl...
>>
>> "Pegasus (MVP)" <I.can@fly.com.oz> wrote in message
>> news:esPiWrasIHA.3716@TK2MSFTNGP04.phx.gbl...
>>>
>>> <cc77lemon@gmail.com> wrote in message
>>> news:33851705-0e1b-4ccd-a79b-463690147aac@v26g2000prm.googlegroups.com...
>>> On May 9, 3:48 pm, "Pegasus \(MVP\)" <I....@fly.com.oz> wrote:
>>>> <cc77le...@gmail.com> wrote in message
>>>>
>>>> news:b1c9ed6f-81a6-4dc5-b655-b37fffdfaff0@y18g2000pre.googlegroups.com...
>>>>
>>>> > Is there a quick and easy code in VB that will allow me to scan and
>>>> > find the first unassigned Drive Letter?
>>>>
>>>> > I need to use the SUBST command and want to apply it to an unassigned
>>>> > (available/free) drive letter that currently isn't in use.
>>>>
>>>> You could use this script:
>>>> Set objSWbemServices = GetObject("winmgmts:\\.")
>>>> Set colSWbemObjectSet =
>>>> objSWbemServices.InstancesOf("Win32_LogicalDisk")
>>>> Letters = ""
>>>>
>>>> For Each objSWbemObject In colSWbemObjectSet
>>>> Drive = "Drive " & objSWbemObject.DeviceID & "=" &
>>>> objSWbemObject.Description
>>>> Letters = Letters & Mid(Drive, 7, 1)
>>>> Next
>>>>
>>>> For i = Asc("D") To Asc("Z")
>>>> If InStr(Letters, Chr(i)) = 0 Then
>>>> WScript.Echo "First free letter is " & Chr(i) & ":"
>>>> Exit For
>>>> End If
>>>> Next
>>>>
>>>> Since you need the first free letter for subst.exe, a batch file
>>>> solution might simplify things:
>>>> @echo off
>>>> setlocal enabledelayedexpansion
>>>> set letter=
>>>> for %%a in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
>>>> dir %%a:\ 1>nul 2>nul || (set Letter=%%a&goto Exit)
>>>> )
>>>> :Exit
>>>> echo First free letter is %letter%:
>>>
>>> Thanks! That worked out perfect.
>>>
>>> ======
>>>
>>> Thanks for the feedback.
>>
>> Here is a slightly shorter version that might also do the trick:
>>
>> pushd %logonserver%\netlogon
>> set drive=%cd:~0,2%
>> popd
>> subst %drive% "C:\program files\zap"
>>
>> It will actually find the *last* available drive letter, or the first
>> looking from the end of the alphabet. It will also fail if no drive
>> letters are available.
>>
>> /Al
>>
>>
>
> Nice one! I would probably use %computername% instead of
> %logonserver% but the end result would be the same.
I had thought of that, however, our users are not workstation
administrators, and are unable to map to administrative shares such as "C$"
on workstations. The OP could, of course, create a share on all of his
workstations that was accessible to everyone, or he could live with the
additional time it would take to use the pushd trick with a domain
controller.
Of course, using the domain controller would fail if this is run on a
workstation before it is connected to the AD infrastructure through a VPN
connection, for example...
/Al