windowsbatch-filecmddrive-letter

Retrieve only the drive letter, (without the colon)


I spent a few hours trying to figure this out. Please kindly provide a little help:)

I'm using this to capture the drive letter:

FOR /F "tokens=2 usebackq delims==" %i IN (`wmic volume where "Label='System Reserved'" get DriveLetter /format:list`) DO @echo %i

Works perfectly, but it returns driveletter:

I need the : to be gone, so I can pass the alphabet character only as a variable into diskpart

This is what I have:

SET SOURCE=SystemReserved
FOR /F "tokens=2 usebackq delims==" %i IN (`wmic volume where "Label='System Reserved'" get DriveLetter /format:list`) DO set SystemReserved=%1

IF "%SystemReserved%"=="" GOTO notfound
Diskpart remove letter=$Source%
:notfound
Exit

But that darn : is killing me :)


Solution

  • Here are some answers to compliment my initial comment. In them I added the additional security of ensuring that there is a drive letter allocated to the volume with that label, and that it is identified as a system volume too.

    If you wanted to continue using diskpart.exe, then it could be achieved in a single line, by using the unwanted colon as a delimiter:

    For /F Tokens^=6^ Delims^=^": %G In ('%SystemRoot%\System32\wbem\WMIC.exe Volume Where "Not DriveLetter Is NULL And Label='System Reserved' And SystemVolume=TRUE" Get DriveLetter /Format:MOF 2^>NUL') Do @%SystemRoot%\System32\diskpart.exe Remove letter=%G
    

    As you have now changed to using mountvol.exe you could simply adjust to exclude the : delimiter:

    For /F Tokens^=6^ Delims^=^" %G In ('%SystemRoot%\System32\wbem\WMIC.exe Volume Where "Not DriveLetter Is NULL And Label='System Reserved' And SystemVolume=TRUE" Get DriveLetter /Format:MOF 2^>NUL') Do @%SystemRoot%\System32\mountvol.exe %G /d
    

    Note: The above answers use direct entry in , as did your own. If you wish to run them from a instead they would look more like these:

    DiskPart:

    @For /F Tokens^=6^ Delims^=^": %%G In ('%SystemRoot%\System32\wbem\WMIC.exe Volume Where "Not DriveLetter Is NULL And Label='System Reserved' And SystemVolume=TRUE" Get DriveLetter /Format:MOF 2^>NUL') Do @%SystemRoot%\System32\diskpart.exe Remove letter=%%G
    

    MountVol:

    @For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe Volume Where "Not DriveLetter Is NULL And Label='System Reserved' And SystemVolume=TRUE" Get DriveLetter /Format:MOF 2^>NUL') Do @%SystemRoot%\System32\mountvol.exe %%G /d