kxo
kxo

Reputation: 3

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 :)

Upvotes: 0

Views: 143

Answers (4)

Compo
Compo

Reputation: 38718

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

Upvotes: 1

kxo
kxo

Reputation: 3

Thank you all, really nice to see willing folks to help.

I decided to go this route. Tested and joy ---

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

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

IF "%SOURCE%"=="" GOTO notfound
Mountvol %SystemReserved% /d

:notfound

exit /b 0

Upvotes: 0

Stephan
Stephan

Reputation: 56238

why remove the colon, when you can get the value without the colon in the first place? Just add : to the delimiters:

FOR /F "tokens=2 usebackq delims=:=" %i in (`wmic volume where "Label='System Reserved'" get DriveLetter /value`) do  set "SystemReserved=%i"

(Note: for use in a batch script, use %%i instead of %i. (all instances))

(usage of /value instead of /format:list is personal preference - both are perfectly fine)

Upvotes: 0

cup
cup

Reputation: 8299

You just need a substring of %SystemReserved%

source=%SystemReserved:~0,1%
diskpart remove letter=%source%

You may need to

setlocal enabledelayedexpansion

before the script and

endlocal

at the end of the script

Upvotes: 0

Related Questions