jColeson
jColeson

Reputation: 971

How to read the label of a drive or volume in a batch file?

I'm trying to write a batch file to rip my dvds to the hard drive. I'd like the file names to be the volume label of the dvd, but I haven't been able to determine a way to read the label of a disk in a batch file.

Is there a way to retrieve the volume label of a drive in a batch file so I can use it as the file name?

Upvotes: 4

Views: 16940

Answers (3)

Timo Salmi
Timo Salmi

Reputation: 264

More fully. Programmed as a subroutine we have

@echo off & setlocal enableextensions
set target_=D:
::
call :IsDeviceReady %target_% isready_
echo Device %target_% ready: %isready_%
if /i "%isready_%"=="false" (endlocal & goto :EOF)
::
call :GetLabel %target_% label_
echo The label of Volume %target_% is %label_%
endlocal & goto :EOF
::
:IsDeviceReady
setlocal
set ready_=true
dir "%~1" > nul 2>&1
if %errorlevel% NEQ 0 set ready_=false
endlocal & set "%2=%ready_%" & goto :EOF
::
:GetLabel
setlocal
for /f "tokens=5*" %%a in (
  'vol "%~1"^|find "Volume in drive "') do (
    set label_=%%b)
endlocal & set "%2=%label_%" & goto :EOF

Originally from http://www.netikka.net/tsneti/info/tscmd101.htm#label

Upvotes: 5

Wilmer
Wilmer

Reputation: 1045

The vol command will give you the volume name in a MS SHELL

Upvotes: 0

Cyclonecode
Cyclonecode

Reputation: 29991

Try this:

for /f "tokens=1-5*" %%1 in ('vol') do (
   set vol=%%6 & goto done
)
:done
echo %vol%

Upvotes: 3

Related Questions