【问题标题】:Refer to/select a drive based only on its label? (i.e., not the drive letter)仅根据其标签参考/选择驱动器? (即,不是驱动器号)
【发布时间】:2010-09-08 01:13:58
【问题描述】:

我正在尝试引用其字母可能会更改的驱动器。我想通过它的标签来引用它(例如,批处理文件中的 MyLabel (v:)。它可以通过 V:\ 来引用。我想通过 MyLabel 来引用它。

(在 Experts Echange 上发布了一个月没有答案。让我们看看 SO 回复的速度有多快)

【问题讨论】:

    标签: windows batch-file


    【解决方案1】:

    之前的答案似乎过于复杂,和/或不是特别适合批处理文件。

    这个简单的一行应该将所需的驱动器号放在变量 myDrive 中。显然将“我的标签”更改为您的实际标签。

    for /f %%D in ('wmic volume get DriveLetter^, Label ^| find "My Label"') do set myDrive=%%D
    

    如果从命令行运行(不在批处理文件中),则必须在两个位置将 %%D 更改为 %D。

    设置变量后,您可以使用%myDrive% 引用驱动器。例如

    dir %myDrive%\someFolder
    

    【讨论】:

    • 这只是在cmd 中打印%%D was unexpected at this time.
    • @Ajedi32 - 您需要更仔细地阅读答案。如果直接从命令行运行(而不是从批处理脚本中),则必须将 %%D 更改为 %D
    • 哎呀,你是对的。对于那个很抱歉。听到 cmd 中的命令在控制台中的工作方式与在脚本中的工作方式不同,我真的很惊讶。哦,好吧,我想我下次应该多注意一下。
    • 另一方面,这种方法似乎需要管理员权限。 Failed to register mof file(s). Only the administrator group members can use WMIC.EXE. Reason:Win32 Error: Access is denied. 我想我必须想出一个使用 vol 的解决方案。
    • @Ajedi32 - 你还在 XP 上吗?自 Vista 以来,WMIC 无需管理员权限即可工作。
    【解决方案2】:

    这是一个简单的批处理脚本 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
    

    【讨论】:

      【解决方案3】:

      此 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
      

      运行如下:

       cscript /nologo DriveFromLabel.vbs label
      

      【讨论】:

      • 你能从 .bat 文件中调用 .vbs 脚本吗?
      【解决方案4】:

      您可以为此使用 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"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-01
        • 1970-01-01
        • 2011-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多