首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按磁盘标签选择磁盘部件

按磁盘标签选择磁盘部件
EN

Stack Overflow用户
提问于 2014-11-15 15:25:01
回答 1查看 5.2K关注 0票数 2

我需要使用DISKPART by label在WinPE中选择一个磁盘。我找到了这个,但我认为它已经过时了:

代码语言:javascript
复制
@Echo %dbg%Off
::
:: Find External drive and set it active
:: Lists Disk Information using Diskpart
::
SetLocal EnableDelayedExpansion
:: Type the frst 11 characters of the label of the drive here. 
Set _FindLabel=Recovery
Call :_InitVars "%_FindLabel%"
"%SystemRoot%\system32\FSUTIL.exe">Nul 2>&1||Goto _NotAdmin
Echo.
Echo.Please wait, gathering info on the installed drives
Echo.
>"%_Dscr1%" Echo.List disk
For /F "Tokens=2" %%I In ('Diskpart /S "%_Dscr1%"^|Findstr /I /R /C:"Disk [0-9]"') Do (
(Echo.Select Disk %%I
Echo.Detail Disk)>>"%_OFile1%"
)
For /F "Tokens=1,2,3*" %%I In ('Diskpart /S "%_OFile1%"^|Findstr /I /R /C:"Disk             [0-9]" /C:"Volume [0-9]"') Do (
  If /I %%I==Disk (
Set _Tmp=%%J:
) Else (
Set _Label=%%L
Set _Label=!_Label:~,11!
>>"%_OFile2%" Echo.!_Tmp!%%J:!_Label!
))
If Exist "%_OFile1%" Del "%_OFile1%"
For /F "Usebackq Tokens=1-3 Delims=:" %%I In ("%_OFile2%") Do (
Set _Label=%%K
Set _Label=!_Label:~,11!
If "!_Label!"=="%_FindLabel%" (Set _Disk=%%I) & (Set _Label=%%K) & Goto _FDisk
>>"%_OFile1%" Echo.Volume %%J on Disk %%I has the Label %%K
)
Echo.
Echo.There is no drive connected that has the label of "%_FindLabel%"
Echo.These are the currently connected volumes:
Type "%_OFile1%"
:_Exit
Echo.
Pause
Goto _Cleanup
:_FDisk
(Echo.Select disk %_Disk%
Echo.Select Partition 1
Echo.Active)>"%_Dscr1%"
Diskpart /S "%_Dscr1%"
:_Cleanup
For %%I In ("%_Dscr1%" "%_OFile1%" "%_OFile2%") Do Del %%I>Nul 2>&1
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
::           Subroutines
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_NotAdmin
Ver|Findstr /I /C:"Version 5">Nul
If %Errorlevel%==0 (Set _Tmp1=5) & (Set _Tmp=a Computer Administrator account)
Ver|Findstr /I /C:"Version 6">Nul
If %Errorlevel%==0 (Set _Tmp1=6) & (Set _Tmp=an Elevated Command Prompt)
Echo.
Echo.This program must be run from %_Tmp%.
If %_Tmp1%==6 Echo.Please Right click the file, then click Run as Administrator
Echo.Exiting program
Goto _Exit
:_InitVars
For /F "Tokens=1 Delims==" %%I In ('Set _ 2^>Nul') Do Set %%I=
Set _Dscr1=%temp%\dpscr1.txt
Set _OFile1=%temp%\_OFile1.txt
Set _OFile2=%temp%\_OFile2.txt
Set _FindLabel=%~1
Set _FindLabel=%_FindLabel:~,20%
Call :_Cleanup

当我运行它时,它返回:

代码语言:javascript
复制
Please wait, gathering info on the installed drives

There is no drive connected that has the label of "Recovery"
These are the currently connected volumes:
Volume 1 on Disk 0 has the Label NTFS   Part
Volume 2 on Disk 0 has the Label NTFS   Part
Volume 3 on Disk 1 has the Label WINPE

它显示的是Fs (文件系统),而不是标签。

Diskpart输出(列表卷)

代码语言:javascript
复制
Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
----------  ---  -----------  -----  ----------  -------  ---------  --------
Volume 0     D   USBBOOT      UDF    DVD-ROM      329 MB  Healthy
Volume 1         Windows      NTFS   Partition     40 GB  Healthy
Volume 2         Recovery     NTFS   Partition     19 GB  Healthy
Volume 3     Z   WINPE        FAT32  Removable   7632 MB  Healthy

不确定如何修改此选项以使其选择标签列。

EN

回答 1

Stack Overflow用户

发布于 2014-11-16 22:40:40

由于Diskpart输出的某些行(list volumeltr列)中缺少有效的驱动器号,因此第19行中的标记

代码语言:javascript
复制
For /F "Tokens=1,2,3*" %%I In ('Diskpart /S "%_OFile1%"^|Findstr /I /R /C:"Disk [0-9]" /C:"Volume [0-9]"') Do (

如下所示(我们现在找到了罪魁祸首):

代码语言:javascript
复制
%%I    %%J  %%K      %%L
Volume 0    D        USBBOOT      UDF    DVD-ROM      329 MB ...
Volume 1    Windows  NTFS   Partition     40 GB  Healthy
Volume 2    Recovery NTFS   Partition     19 GB  Healthy
Volume 3    Z        WINPE        FAT32  Removable   7632 MB ...

因此,您可以在脚本中使用下一段代码(而不是第19行..26行):

代码语言:javascript
复制
For /F "Tokens=1*" %%I In ('Diskpart /S "%_OFile1%"^|Findstr /I /R /C:"Disk [0-9]" /C:"Volume [0-9]"') Do (
  If /I %%I==Disk (
    Set _Tmp=%%J:
) Else (
    Set _Label=%%J
    Set _Label=!_Label:~10,11!
>>"%_OFile2%" Echo.!_Tmp!%%J:!_Label!
))

注意此更改会干扰_Tmp变量!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26943631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档