This is sort of silly, but today is the first day I'm using batch scripts, so bear with me. I'm using the following code to find a particular drive letter assigned to a volume.

set "volume=\Volume{8bc9f784-9f15-11e4-be58-a60f30d14122}"
set "drive="

for  %%D 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 (
mountvol %%D: /L | findstr "%volume%" >nul
if not errorlevel 1 set "drive=%%D"

Now I need to do the opposite. I need to save the volume id from a particular drive letter. I can easily do that using Mountvol X: /l, where X is the drive letter. But how do I assign the output of that command to a variable in batch scripting?

for /f %%a in ('mountvol X: /l') do set volname=%%a
echo %volname%
  • Thank you. I'm using it with tokens and delims, although it isn't necessary, I guess. – Mycroft Holmes May 8 '15 at 20:43
  • Not in this case because it's a single unbroken string with no spaces, but otherwise yes you'd need those. – Karan May 8 '15 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.