Fare riferimento/selezionare un'unità solo in base alla sua etichetta?(ovvero, non la lettera dell'unità)
-
09-06-2019 - |
Domanda
Sto cercando di fare riferimento a un'unità la cui lettera potrebbe cambiare.Mi piacerebbe fare riferimento ad esso con la sua etichetta (ad esempio, MyLabel (v:) all'interno di un file batch.Può essere indicato con V:\ .Mi piacerebbe fare riferimento ad esso tramite MyLabel.
(Questo è stato pubblicato su Experts Echange per un mese senza risposta.Vediamo quanto velocemente SO risponde)
Soluzione
Questo file bat ti fornirà la lettera di unità da un'etichetta di unità:
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
Eseguilo come:
cscript /nologo DriveFromLabel.vbs label
Altri suggerimenti
Le risposte precedenti sembrano eccessivamente complicate e/o non particolarmente adatte a un file batch.
Questo semplice rivestimento dovrebbe posizionare la lettera di unità desiderata nella variabile myDrive.Ovviamente cambia "La mia etichetta" con la tua etichetta attuale.
for /f %%D in ('wmic volume get DriveLetter^, Label ^| find "My Label"') do set myDrive=%%D
Se eseguito dalla riga di comando (non in un file batch), %%D deve essere modificato in %D in entrambe le posizioni.
Una volta impostata la variabile, è possibile fare riferimento all'unità utilizzando %myDrive%
.Per esempio
dir %myDrive%\someFolder
A questo scopo è possibile utilizzare il linguaggio di query WMI.Dare un'occhiata a http://msdn.microsoft.com/en-us/library/aa394592(VS.85).aspx per esempio.Le informazioni che stai cercando sono disponibili ad es.tramite la proprietà VolumeName della classe Win32_LogicalDisk, http://msdn.microsoft.com/en-us/library/aa394173(VS.85).aspx
SELECT * FROM Win32_LogicalDisk WHERE VolumeName="MyLabel"
Ecco un semplice script batch getdrive.cmd per trovare una lettera di unità da un'etichetta di volume.Basta chiamare "getdrive MyLabel" o 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