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.

Re: In VB Find First Available Unassigned Drive Letter by Pegasus

Pegasus
Fri May 09 01:48:43 CDT 2008


<cc77lemon@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%:



Re: In VB Find First Available Unassigned Drive Letter by cc77lemon

cc77lemon
Fri May 09 02:56:45 CDT 2008

On May 9, 3:48=A0pm, "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 =3D GetObject("winmgmts:\\.")
> Set colSWbemObjectSet =3D objSWbemServices.InstancesOf("Win32_LogicalDisk"=
)
> Letters =3D ""
>
> For Each objSWbemObject In colSWbemObjectSet
> =A0Drive =3D "Drive " & objSWbemObject.DeviceID & "=3D" &
> objSWbemObject.Description
> =A0Letters =3D Letters & Mid(Drive, 7, 1)
> Next
>
> For i =3D Asc("D") To Asc("Z")
> =A0If InStr(Letters, Chr(i)) =3D 0 Then
> =A0 WScript.Echo "First free letter is " & Chr(i) & ":"
> =A0 Exit For
> =A0End 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=3D
> 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 (
> =A0 dir %%a:\ 1>nul 2>nul || (set Letter=3D%%a&goto Exit)
> )
> :Exit
> echo First free letter is %letter%:

Thanks! That worked out perfect.

Re: In VB Find First Available Unassigned Drive Letter by Pegasus

Pegasus
Fri May 09 03:01:02 CDT 2008


<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.



Re: In VB Find First Available Unassigned Drive Letter by Al

Al
Sun May 11 12:52:25 CDT 2008


"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



Re: In VB Find First Available Unassigned Drive Letter by Pegasus

Pegasus
Sun May 11 13:06:45 CDT 2008


"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.



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



Re: In VB Find First Available Unassigned Drive Letter by Stefan

Stefan
Wed May 14 10:42:47 CDT 2008

"Al Dunbar" <AlanDrub@hotmail.com.nospaam> wrote:

Your mail address is wrong!

> Here is a slightly shorter version that might also do the trick:
>
> pushd %logonserver%\netlogon

But beware of MSKB 183495: %LOGONSERVER% Variable not Available After Logon Script

Stefan


Re: In VB Find First Available Unassigned Drive Letter by Alexander

Alexander
Wed May 14 16:35:02 CDT 2008

Al Dunbar schrieb:


> 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.

It also fails on standalone workstations.


CU,
Alex

Re: In VB Find First Available Unassigned Drive Letter by Alexander

Alexander
Wed May 14 16:38:07 CDT 2008

cc77lemon@gmail.com schrieb:
> 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.
>

Hi there

this one's quick and easy:


Set fs = CreateObject("Scripting.FileSystemObject")

For i = Asc("C") To Asc("Z")

drv = Chr(i)
If Not fs.DriveExists (drv & ":") Then Exit For
drv = "unknown"

Next

MsgBox "First unassigned drive letter is " & drv




CU,
Alex

Re: In VB Find First Available Unassigned Drive Letter by Al

Al
Sat May 17 19:37:26 CDT 2008


"Stefan Kanthak" <postmaster@[127.0.0.1]> wrote in message
news:uHEg4AftIHA.552@TK2MSFTNGP06.phx.gbl...
> "Al Dunbar" <AlanDrub@hotmail.com.nospaam> wrote:
>
> Your mail address is wrong!
>
>> Here is a slightly shorter version that might also do the trick:
>>
>> pushd %logonserver%\netlogon
>
> But beware of MSKB 183495: %LOGONSERVER% Variable not Available After
> Logon Script

Funny, I have never had that problem. I see that it does not mention 2003 as
one of the affected versions...

/Al



Re: In VB Find First Available Unassigned Drive Letter by Al

Al
Sat May 17 19:38:52 CDT 2008


"Alexander Mueller" <millerax@hotmail.com> wrote in message
news:482b5b06$0$6786$9b4e6d93@newsspool2.arcor-online.net...
> Al Dunbar schrieb:
>
>
>> 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.
>
> It also fails on standalone workstations.

DOH! thanks for reminding me that not everything is on a network.

/Al