通过 diskpart.exe 删除所有驱动器号
Remove all drive letters via diskpart.exe
因为我正在寻找一个程序化的答案,所以我 post 这个问题是关于 SO 的,尽管它的边界正如人们在这个 related and migrated 问题上看到的那样。
我正在尝试自动删除引导到 Windows PE 的系统中所有分配的磁盘盘符。这要求解决方案是批处理的(而不是 Powershell)。
为此,我打算使用 diskpart in scriptmode (as suggested elsewhere),它具有 noerr
选项并声明
By default, if DiskPart encounters an error while attempting to perform a scripted task, DiskPart stops processing the script and displays an error code (unless you specified the noerr parameter).
...
The noerr parameter enables you to perform useful tasks such as using a single script to delete all partitions on all disks regardless of the total number of disks.
有 multiple approaches which just loop 遍及所有可能的驱动器名称并调用 diskpart
多次。
但是这引入了一些 unpleasant overhead (along with having to wait 15 seconds between each) as also stated here
You can run consecutive DiskPart scripts, but you must allow at least 15 seconds between each script for a complete shutdown of the previous execution before running the DiskPart command again in successive scripts
我的方法是完全忽略磁盘(自 selecting a volume also selects the appropriate disk 以来)并遍历所有字母以生成 diskpartscript.txt
like
for %%l in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
echo select volume %%l >> diskpartscript.txt
echo remove all noerr >> diskpartscript.txt
)
:: call diskpart.exe once with generated script file
diskpart.exe /s diskpartscript.txt
不幸的是,select volume
命令 does not have 一个 noerr
开关,因此脚本执行将在第一个不存在的卷之后停止。所以可能需要先找出所有存在的卷,然后才对其进行操作。
所以我现在的问题是,如何通过一次 diskpart.exe
调用删除所有可能存在的磁盘上所有可能存在的驱动器号。
作为一个想法,为什么不只为已安装的卷生成驱动器号,减去分配给 PE 磁盘的驱动器号,(当前驱动器)?。这意味着您只能使用现有卷。
示例:
@Echo Off
SetLocal EnableDelayedExpansion
Set "Ltrs="
For /F "Delims=: " %%A In ('MountVol^|Find ":\"'
) Do If /I Not "%%A"=="%CD:~,1%" Set "Ltrs=%%A !Ltrs!"
If Not Defined Ltrs Exit /B
Rem Test
For %%A In (%Ltrs%) Do Echo Select Volume %%A
Pause
因为我正在寻找一个程序化的答案,所以我 post 这个问题是关于 SO 的,尽管它的边界正如人们在这个 related and migrated 问题上看到的那样。
我正在尝试自动删除引导到 Windows PE 的系统中所有分配的磁盘盘符。这要求解决方案是批处理的(而不是 Powershell)。
为此,我打算使用 diskpart in scriptmode (as suggested elsewhere),它具有 noerr
选项并声明
By default, if DiskPart encounters an error while attempting to perform a scripted task, DiskPart stops processing the script and displays an error code (unless you specified the noerr parameter). ...
The noerr parameter enables you to perform useful tasks such as using a single script to delete all partitions on all disks regardless of the total number of disks.
有 multiple approaches which just loop 遍及所有可能的驱动器名称并调用 diskpart
多次。
但是这引入了一些 unpleasant overhead (along with having to wait 15 seconds between each) as also stated here
You can run consecutive DiskPart scripts, but you must allow at least 15 seconds between each script for a complete shutdown of the previous execution before running the DiskPart command again in successive scripts
我的方法是完全忽略磁盘(自 selecting a volume also selects the appropriate disk 以来)并遍历所有字母以生成 diskpartscript.txt
like
for %%l in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
echo select volume %%l >> diskpartscript.txt
echo remove all noerr >> diskpartscript.txt
)
:: call diskpart.exe once with generated script file
diskpart.exe /s diskpartscript.txt
不幸的是,select volume
命令 does not have 一个 noerr
开关,因此脚本执行将在第一个不存在的卷之后停止。所以可能需要先找出所有存在的卷,然后才对其进行操作。
所以我现在的问题是,如何通过一次 diskpart.exe
调用删除所有可能存在的磁盘上所有可能存在的驱动器号。
作为一个想法,为什么不只为已安装的卷生成驱动器号,减去分配给 PE 磁盘的驱动器号,(当前驱动器)?。这意味着您只能使用现有卷。
示例:
@Echo Off
SetLocal EnableDelayedExpansion
Set "Ltrs="
For /F "Delims=: " %%A In ('MountVol^|Find ":\"'
) Do If /I Not "%%A"=="%CD:~,1%" Set "Ltrs=%%A !Ltrs!"
If Not Defined Ltrs Exit /B
Rem Test
For %%A In (%Ltrs%) Do Echo Select Volume %%A
Pause