Since i am looking for a programmatic answer, ill post this question on SO, though its borderline as one can see on this related and migrated question.
I am trying to automate the removal of all assigned disk letters in a system booted to Windows PE. This requires the solution to be batch (instead of Powershell).
For this i inteded to use diskpart in scriptmode (as suggested elsewhere) which features a noerr
option and states
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.
There are multiple approaches which just loop over all possible drive names and call diskpart
multiple times.
However this introduces some 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
My approach was to ignore disks totally (since selecting a volume also selects the appropriate disk) and loop over all letters to generate the 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
Unfortunately, the select volume
command does not have a noerr
switch so the script execution will halt after the first non-existing volume. So it is probably required to find out all existing volumes first and only operate on them.
So my question now is, how to remove all possibly existing drive letters over all possibly existing disks with one diskpart.exe
call.
As an idea, why not generate only drive letters for the mounted volumes, minus that assigned to the PE disk, (current drive)?. This would mean that you're only working with existing volumes.
Example:
@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