Re: In VB Find First Available Unassigned Drive Letter

Tech-Archive recommends: Fix windows errors by optimizing your registry




<cc77lemon@xxxxxxxxx> wrote in message
news:33851705-0e1b-4ccd-a79b-463690147aac@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On May 9, 3:48 pm, "Pegasus \(MVP\)" <I....@xxxxxxxxxx> wrote:
<cc77le...@xxxxxxxxx> wrote in message

news:b1c9ed6f-81a6-4dc5-b655-b37fffdfaff0@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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.


.



Relevant Pages