I am trying to write a batch file to remove drive letters that are assigned to partitions with no file system. I cannot use wmi since it is being used in a WinPE recovery en
I may have a much simpler solution for you. Just grab the whole line and use batch sub-strings to pull out the right pieces
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
for /f "delims=" %%i in ('^
echo list volume ^|^
diskpart ^|^
findstr Volume ^|^
findstr /v ^
/c:"Volume ### Ltr Label Fs Type Size Status Info"^
') do (
set "line=%%i"
set letter=!line:~15,1!
set fs=!line:~32,7!
if not " "=="!fs!" (
if not " "=="!letter!" (
call :removeVol !letter!
)
)
)
endlocal
exit /b
:removeVol
(
echo select volume %1
echo remove letter %1
) | diskpart
exit /b
The for
statement produces of listing of volumes without the column headers or separators.
The do
statement does the sub-string operations.
Ensure that you have admin privilege for the diskpart command. I am a bit curious about your WinRE environment. Most of the ones I've worked in did have a minimal WMI instance running, as well as WSH which would make this code much cleaner.