批处理:列出可用的硬盘驱动器并使用所选选项

Batch: list available hard drives and work with chosen option

我想在批处理脚本中列出所有可用的可移动硬盘驱动器并继续使用所选选项。我知道有

这样的选项
wmic logicaldisk get caption,volumename

列出硬盘和

SET /P M=Type 1 or 2 then press ENTER:
IF %M%==1 GOTO One
IF %M%==2 GOTO Two

创建菜单。但是如何将卷存储在变量中并将它们列在菜单中?

类似于:

Choose from list:

1) D:\Harddrivename1
2) E:\Harddrivename2

Enter option: 2

感谢任何帮助!

这是一个函数,可让您创建非类型 3(固定)的驱动器数组:

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

下面是说明如何使用该函数的完整示例:

@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

希望您可以使用它来入门。如果我猜错了驱动器类型检测,see this pageCtrl + F 并在页面上找到 DriveType。

这可能会让您入门。它在 PowerShell 中。它获取所有可移动 (non-floppy) 驱动器的列表,并提供一个列表供用户选择。如果只有一个驱动器,则不显示菜单。

还有很多事情要做。没有对用户输入的范围或错误检查。当然,也没有说明您想对驱动器做什么。

$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