我想在批处理脚本中列出所有可用的可移动硬盘驱动器,并继续使用所选的选项。我知道有像
这样的选项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
感谢任何帮助!
答案 0 :(得分: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。
答案 1 :(得分:0)
这可能会让你开始。它在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#root\cimv2\Win32_LogicalDisk
# ready to be used for something.
$thedrive | Format-List * -Force