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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了批处理:列出可用的硬盘驱动器并使用所选的选项相关的知识,希望对你有一定的参考价值。

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

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 page,Ctrl + F并在页面上找到DriveType。

另一答案

这可能会让你开始。它在PowerShell中。它获取所有可移动(非软盘)驱动器的列表,并显示供用户选择的列表。如果只有一个驱动器,则不显示菜单。

还有很多工作要做。用户输入没有范围或错误检查。当然,没有说明你想用驱动器做什么。

$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#rootcimv2Win32_LogicalDisk
# ready to be used for something.

$thedrive | Format-List * -Force

以上是关于批处理:列出可用的硬盘驱动器并使用所选的选项的主要内容,如果未能解决你的问题,请参考以下文章

根据所选选项显示输入字段

查看磁盘空间命令

复制并删除所选的列表框项目

VSAN为啥声明磁盘选项,我的没有可用磁盘

如何列出硬盘驱动器文件夹下的所有文件名?

硬盘变为raw是怎么回事?