Batch: list available hard drives and work with chosen option

白昼怎懂夜的黑 提交于 2019-12-09 04:28:24

Here's a function that'll let you create an array of drives that are not of type 3 (fixed):

rem // populates arrayname, arrayname.length, and arrayname.ubound
:getRemovableDrives <arrayname>
rem // unset array if exists
for /f "delims==" %%I in ('2^>NUL set %~1') do set "%%~I="
setlocal enabledelayedexpansion

set /a %~1.length = 0, %~1.ubound = -1

rem // note: nested for /f loops convert UCS-2 encoded WMI results to ANSI
for /f "skip=2 delims=" %%# in (
    'wmic logicaldisk where "DriveType <> 3" get caption^,volumename /format:csv'
) do for /f "tokens=2,3 delims=," %%I in ("%%~#") do (
    set "%~1[!%~1.length!].caption=%%~I"
    set "%~1[!%~1.length!].volumename=%%~J"
    set /a %~1.ubound = %~1.length, %~1.length += 1
)

rem // Trick to make private variables public
for /F "delims=" %%I in ('set %~1') do (
    if defined %~1.ubound endlocal
    set "%%~I"
)
exit /b

Here's a full example illustrating how to use the function:

@echo off & setlocal enabledelayedexpansion

:begin
call :getRemovableDrives drives

if %drives.length% equ 0 (
    echo No removable drives found.
    exit /b 1
)

set choices=
echo Removable drives:
echo;
for /L %%I in (0, 1, %drives.ubound%) do (
    set "choices=!choices!%%I"
    echo(%%I^) !drives[%%I].caption! (!drives[%%I].volumename!^)
)
echo(X^) exit
set "choices=%choices%x"
echo;
choice /C %choices% /N /M "Press a number (or X to quit): "
set /a choice = %ERRORLEVEL% - 1

if not defined drives[%choice%].caption exit /b 0

echo You chose !drives[%choice%].caption! (!drives[%choice%].volumename!^)
goto :begin

goto :EOF

rem // populates arrayname, arrayname.length, and arrayname.ubound
:getRemovableDrives <arrayname>
rem // unset array if exists
for /f "delims==" %%I in ('2^>NUL set %~1') do set "%%~I="
setlocal enabledelayedexpansion

set /a %~1.length = 0, %~1.ubound = -1

rem // note: nested for /f loops convert UCS-2 encoded WMI results to ANSI
for /f "skip=2 delims=" %%# in (
    'wmic logicaldisk where "DriveType <> 3" get caption^,volumename /format:csv'
) do for /f "tokens=2,3 delims=," %%I in ("%%~#") do (
    set "%~1[!%~1.length!].caption=%%~I"
    set "%~1[!%~1.length!].volumename=%%~J"
    set /a %~1.ubound = %~1.length, %~1.length += 1
)

rem // Trick to make private variables public
for /F "delims=" %%I in ('set %~1') do (
    if defined %~1.ubound endlocal
    set "%%~I"
)
exit /b

Hopefully you can use this to get you started. In case I guessed incorrectly about the drive type detection, see this page, Ctrl + F and find DriveType on the page.

This might get you started. It is in PowerShell. It gets a list of all removable (non-floppy) drives and presents a list for the user to chose from. If there is only one drive, it does not present the menu.

There is much to be done. There is no range or error checking on the user's input. And, of course, it is not stated what you want to do with the drive.

$drivelist = @(Get-WMIObject Win32_LogicalDisk -Filter "MediaType = 11")

if ($drivelist.Count -eq 0) { Write-Host 'There are no removable drives.'
} elseif ($drivelist.Count -eq 1) { $thedrive = $drivelist[0]
} else {
    Write-Host 'Removable drives'
    $i = 1
    foreach ($drive in $drivelist) {
        Write-Host $('{0}. {1} {2}' -f $i, $drive.DeviceId, $drive.VolumeName)
        $i += 1
    }

    $dn = Read-Host -Prompt 'Enter the drive number.'
    $thedrive = $drivelist[$dn - 1]
}

# At this point, $thedrive is a System.Management.ManagementObject#root\cimv2\Win32_LogicalDisk
# ready to be used for something.

$thedrive | Format-List * -Force
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!