Consulte/selecione uma unidade com base apenas em seu rótulo?(ou seja, não a letra da unidade)

StackOverflow https://stackoverflow.com/questions/47849

  •  09-06-2019
  •  | 
  •  

Pergunta

Estou tentando me referir a uma unidade cuja letra pode mudar.Gostaria de me referir a ele pelo rótulo (por exemplo, MyLabel (v :) em um arquivo em lote.Pode ser referido por V:\ .Eu gostaria de me referir a ele por MyLabel.

(Isso foi postado no Experts Echange por um mês sem resposta.Vamos ver com que rapidez o SO responde)

Foi útil?

Solução

Este arquivo bat fornecerá a letra da unidade de um rótulo de unidade:

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

Execute-o como:

 cscript /nologo DriveFromLabel.vbs label

Outras dicas

As respostas anteriores parecem excessivamente complicadas e/ou não são particularmente adequadas para um arquivo em lote.

Este liner simples deve colocar a letra da unidade desejada na variável myDrive.Obviamente, mude "My Label" para o seu rótulo real.

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

Se for executado a partir da linha de comando (não em um arquivo em lote), %%D deverá ser alterado para %D em ambos os locais.

Depois que a variável for definida, você poderá consultar a unidade usando %myDrive%.Por exemplo

dir %myDrive%\someFolder

Você pode usar a linguagem de consulta WMI para isso.Dê uma olhada http://msdn.microsoft.com/en-us/library/aa394592(VS.85).aspx por exemplo.A informação que você procura está disponível, por ex.através da propriedade VolumeName da classe Win32_LogicalDisk, http://msdn.microsoft.com/en-us/library/aa394173(VS.85).aspx

SELECT * FROM Win32_LogicalDisk WHERE VolumeName="MyLabel"

Aqui está um script em lote simples getdrive.cmd para encontrar uma letra de unidade em um rótulo de volume.Basta chamar "getdrive MyLabel" ou 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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top