Search code examples
cmdwindows-10

How to set Batch variable to the volume label of specific drive letter?


I want to write a .BAT file (on Win 10) which gets the volume label of the optical disk mounted at E: and assigns it to a variable so that I can then create a folder on another drive which has the same name as the volume label.

The vol command returns 2 lines of text (with the volume label at the end of the first line). Is there a command which will return the volume label only?


Solution

  • FOR /F "tokens=6" %a IN ('vol c:') DO SET var=%a
    

    This should do. When you run echo %var% it will show the volume label only

    Make sure to change the vol c: to your desired volume!

    EDIT:

    If the Volume label has 2 words, use FOR /F "tokens=6-7" %a IN ('vol c:') DO SET var=%a

    If it has 3 words Just change the "tokens=6" to "tokens=6-7,8"

    Hope this helps! 😇