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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8649934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:44:51  来源:igfitidea点击:

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

windowsbatch-file

提问by jColeson

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.

我正在尝试编写一个批处理文件来将我的 DVD 翻录到硬盘上。我希望文件名成为 DVD 的卷标,但我无法确定读取批处理文件中磁盘标签的方法。

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?

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

采纳答案by Cyclonecode

Try this:

尝试这个:

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

回答by Timo Salmi

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

最初来自http://www.netikka.net/tsneti/info/tscmd101.htm#label

回答by Wilmer

The volcommand will give you the volume name in a MS SHELL

命令会给你在MS SHELL卷名