Q. How can I return a drive letter for a given volume label or volume number?

Jerold Schulman

October 23, 2006

1 Min Read
ITPro Today logo in a gray background | ITPro Today

Using Diskpart.exe, I have scripted FindLabel.bat to return a drive letter for a given volume label or volume number.

The syntax for using FindLabel.bat is:

[call] FindLabel "Volume or Label" Drive

Where:

Volume or Label is the string to match against, like Volume 1 or ,                where  will be truncated to 11 characters.Drive           is a call directed environment variable that will contain the drive letter,                 like D:. If the Volume or Label is not found, Drive will contain 0:.

FindLabel.bat contains:

@echo offif {%2}=={} @echo Syntax FindLabel "Volume or Label" Drive&goto :EOFsetlocal ENABLEDELAYEDEXPANSIONset label=%1set label=%label:"=%set FL="%TEMP%dp.txt"echo list volume>%FL%echo exit>>%FL%set label11=%LABEL:~0,11%set DRV=0:for /f "Tokens=*" %%a in ('diskpart /s %FL%^|find /i "%label11%"') do ( set line=%%a set DRV=!line:~13,1!:)endlocal&set %2=%DRV%


Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like