windows 仅根据标签参考/选择驱动器?(即,不是驱动器号)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47849/
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
Refer to/select a drive based only on its label? (i.e., not the drive letter)
提问by Clay Nichols
I'm trying to refer to a drive whose letter may change. I'd like to refer to it by its label (e.g., MyLabel (v:) within a Batch File. It can be referred to by V:\ . I'd like to refer to it by MyLabel.
我试图引用一个字母可能会改变的驱动器。我想通过它的标签来引用它(例如,批处理文件中的 MyLabel (v:)。它可以通过 V:\ 引用。我想通过 MyLabel 引用它。
(This was posted on Experts Echange for a month with no answer. Let's see how fast SO answers it )
(这在 Experts Echange 上发布了一个月没有答案。让我们看看 SO 的答案有多快)
采纳答案by Rob Walker
This bat file will give you the drive letter from a drive label:
此 bat 文件将为您提供驱动器标签中的驱动器号:
Option Explicit
Dim num, args, objWMIService, objItem, colItems
set args = WScript.Arguments
num = args.Count
if num <> 1 then
WScript.Echo "Usage: CScript DriveFromLabel.vbs <label>"
WScript.Quit 1
end if
Set objWMIService = GetObject("winmgmts:\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk")
For Each objItem in colItems
If strcomp(objItem.VolumeName, args.Item(0), 1) = 0 Then
Wscript.Echo objItem.Name
End If
Next
WScript.Quit 0
Run it as:
运行它:
cscript /nologo DriveFromLabel.vbs label
回答by dbenham
The previous answers seem either overly complicated, and/or not particularly suited to a batch file.
以前的答案似乎过于复杂,和/或不太适合批处理文件。
This simple one liner should place the desired drive letter in variable myDrive. Obviously change "My Label" to your actual label.
这个简单的单行应该将所需的驱动器号放在变量 myDrive 中。显然将“我的标签”更改为您的实际标签。
for /f %%D in ('wmic volume get DriveLetter^, Label ^| find "My Label"') do set myDrive=%%D
If run from the command line (not in a batch file), then %%D must be changed to %D in both places.
如果从命令行(不在批处理文件中)运行,则必须在两个位置将 %%D 更改为 %D。
Once the variable is set, you can refer to the drive using %myDrive%
. For example
设置变量后,您可以使用%myDrive%
. 例如
dir %myDrive%\someFolder
回答by VolkerK
You can use the WMI query language for that. Take a look at http://msdn.microsoft.com/en-us/library/aa394592(VS.85).aspxfor examples. The information you are looking for is available e.g. through the property VolumeName of the Win32_LogicalDisk class, http://msdn.microsoft.com/en-us/library/aa394173(VS.85).aspx
您可以为此使用 WMI 查询语言。看看http://msdn.microsoft.com/en-us/library/aa394592(VS.85).aspx的例子。您正在寻找的信息可通过例如 Win32_LogicalDisk 类的属性 VolumeName 获得,http://msdn.microsoft.com/en-us/library/aa394173(VS.85) .aspx
SELECT * FROM Win32_LogicalDisk WHERE VolumeName="MyLabel"
SELECT * FROM Win32_LogicalDisk WHERE VolumeName="MyLabel"
回答by Philibert Perusse
Here is a simple batch script getdrive.cmd to find a drive letter from a volume label. Just call "getdrive MyLabel" or getdrive "My Label".
这是一个简单的批处理脚本 getdrive.cmd 从卷标中查找驱动器号。只需调用“getdrive MyLabel”或getdrive“My Label”。
@echo off
setlocal
:: Initial variables
set TMPFILE=%~dp0getdrive.tmp
set driveletters=abcdefghijklmnopqrstuvwxyz
set MatchLabel_res=
for /L %%g in (2,1,25) do call :MatchLabel %%g %*
if not "%MatchLabel_res%"=="" echo %MatchLabel_res%
goto :END
:: Function to match a label with a drive letter.
::
:: The first parameter is an integer from 1..26 that needs to be
:: converted in a letter. It is easier looping on a number
:: than looping on letters.
::
:: The second parameter is the volume name passed-on to the script
:MatchLabel
:: result already found, just do nothing
:: (necessary because there is no break for for loops)
if not "%MatchLabel_res%"=="" goto :eof
:: get the proper drive letter
call set dl=%%driveletters:~%1,1%%
:: strip-off the " in the volume name to be able to add them again further
set volname=%2
set volname=%volname:"=%
:: get the volume information on that disk
vol %dl%: > "%TMPFILE%" 2>&1
:: Drive/Volume does not exist, just quit
if not "%ERRORLEVEL%"=="0" goto :eof
set found=0
for /F "usebackq tokens=3 delims=:" %%g in (`find /C /I "%volname%" "%TMPFILE%"`) do set found=%%g
:: trick to stip any whitespaces
set /A found=%found% + 0
if not "%found%"=="0" set MatchLabel_res=%dl%:
goto :eof
:END
if exist "%TMPFILE%" del "%TMPFILE%"
endlocal