如何在批处理文件中读取驱动器或卷的标签?

3

我试图编写一个批处理文件,将我的DVD光盘转存到硬盘。我希望文件名为DVD的卷标,但我无法确定如何在批处理文件中读取磁盘的标签。

有没有一种方法可以在批处理文件中检索驱动器的卷标,以便我可以将其用作文件名?

3个回答

4
更加详细。作为子程序编写,我们有:
@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

来自原始网址:http://www.netikka.net/tsneti/info/tscmd101.htm#label

强大的一个。喜欢它 :-) - Kawi42
注意:子例程“IsDeviceReady”在空驱动器上将返回false(这是不正确的)。请用“vol”替换“dir“%〜1”> nul 2>&1”,以解决此问题。 - Kawi42

3

试试这个:

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

这将正确地将%vol%设置为当前音量的标签。只需在调用它之前切换到DVD驱动器即可。 - jColeson
@jColeson - 是的,你需要更改到你的 DVD 驱动器,只需在批处理文件顶部添加 cd <驱动器字母>: 即可轻松完成。 - Cyclonecode
1
您可以直接在vol命令中插入驱动器:VOL D: - Aacini
这个解决方案中有一个小错误:它总是在 %vol% 后面添加一个空格。这可能会混淆后续的脚本处理。 应该去掉 & 周围的空格以消除它。即:set vol=%%6&goto done - Marcel

0

vol 命令将在 MS SHELL 中返回卷名。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接