BAT/CMD file to remove drive letters using DISKPART

后端 未结 1 1496
感动是毒
感动是毒 2021-01-12 18:00

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

相关标签:
1条回答
  • 2021-01-12 18:43

    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.

    0 讨论(0)
提交回复
热议问题