对于在Windows命令行界面下运行的备份脚本,我想获取某个硬盘卷的驱动器号(存储在变量%lookfor%中)。这是我到目前为止提出的代码。

set lookfor=yourvolume
for /f "tokens=1,2,*" %%D in ('wmic logicaldisk get VolumeName^, Caption^') Do (
 if %%F equ "" (
        @echo %%D has the VolumeName %%E
        if "%%E" equ "%lookfor%" (
            set letter=%%D
        )
    ) else (
        @echo %%D has the VolumeName %%E %%F
        if "%%F" equ "%lookfor%" (
        set letter=%%D
        )
    )
 )
echo The Volume %lookfor% has the driveletter %letter%

将驱动器号分配给变量驱动器号显然会失败。我该如何解决?

最佳答案

使用WMIC的where参数指定要查找的内容:

for /f %%a in ('
    wmic logicaldisk where "VolumeName='%lookfor%'" get Caption ^| find ":"
') do set letter=%%a

关于windows - 在Windows命令行中获取卷名的驱动器号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33801077/

10-12 21:01