<p>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.</p>

<p>(This was posted on Experts Echange for a month with no answer. Let's see how fast SO answers it )</p>

[{'answer_id': 47856, 'author': 'VolkerK', 'author_id': 4833, 'author_profile': 'https://Stackoverflow.com/users/4833', 'pm_score': 1, 'selected': False, 'text': '<p>You can use the WMI query language for that.\nTake a look at <a href="http://msdn.microsoft.com/en-us/library/aa394592(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa394592(VS.85).aspx</a> for examples.\nThe information you are looking for is available e.g. through the property VolumeName of the Win32_LogicalDisk class, <a href="http://msdn.microsoft.com/en-us/library/aa394173(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa394173(VS.85).aspx</a></p>\n\n<p><code>SELECT * FROM Win32_LogicalDisk WHERE VolumeName="MyLabel"</code></p>\n'}, {'answer_id': 47860, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 2, 'selected': True, 'text': '<p>This bat file will give you the drive letter from a drive label:</p>\n\n<pre><code>Option Explicit\nDim num, args, objWMIService, objItem, colItems\n\nset args = WScript.Arguments\nnum = args.Count\n\nif num &lt;&gt; 1 then\n   WScript.Echo "Usage: CScript DriveFromLabel.vbs &lt;label&gt;"\n   WScript.Quit 1\nend if\n\nSet objWMIService = GetObject("winmgmts:\\\\.\\root\\cimv2")\nSet colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk")\n\nFor Each objItem in colItems\n  If strcomp(objItem.VolumeName, args.Item(0), 1) = 0 Then\n    Wscript.Echo objItem.Name\n  End If\nNext\n\nWScript.Quit 0\n</code></pre>\n\n<p>Run it as:</p>\n\n<pre><code> cscript /nologo DriveFromLabel.vbs label\n</code></pre>\n'}, {'answer_id': 69542, 'author': 'Philibert Perusse', 'author_id': 7984, 'author_profile': 'https://Stackoverflow.com/users/7984', 'pm_score': 1, 'selected': False, 'text': '<p>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".</p>\n\n<pre><code>@echo off\nsetlocal\n\n:: Initial variables\nset TMPFILE=%~dp0getdrive.tmp\nset driveletters=abcdefghijklmnopqrstuvwxyz\nset MatchLabel_res=\n\nfor /L %%g in (2,1,25) do call :MatchLabel %%g %*\n\nif not "%MatchLabel_res%"=="" echo %MatchLabel_res%\n\ngoto :END\n\n\n:: Function to match a label with a drive letter. \n::\n:: The first parameter is an integer from 1..26 that needs to be \n:: converted in a letter. It is easier looping on a number\n:: than looping on letters.\n::\n:: The second parameter is the volume name passed-on to the script\n:MatchLabel\n\n:: result already found, just do nothing \n:: (necessary because there is no break for for loops)\nif not "%MatchLabel_res%"=="" goto :eof\n\n:: get the proper drive letter\ncall set dl=%%driveletters:~%1,1%%\n\n:: strip-off the " in the volume name to be able to add them again further\nset volname=%2\nset volname=%volname:"=%\n\n:: get the volume information on that disk\nvol %dl%: &gt; "%TMPFILE%" 2&gt;&amp;1\n\n:: Drive/Volume does not exist, just quit\nif not "%ERRORLEVEL%"=="0" goto :eof\n\nset found=0\nfor /F "usebackq tokens=3 delims=:" %%g in (`find /C /I "%volname%" "%TMPFILE%"`) do set found=%%g\n\n:: trick to stip any whitespaces\nset /A found=%found% + 0\n\n\nif not "%found%"=="0" set MatchLabel_res=%dl%:\ngoto :eof\n\n\n\n\n\n\n\n\n:END\n\nif exist "%TMPFILE%" del "%TMPFILE%"\nendlocal\n</code></pre>\n'}, {'answer_id': 9066694, 'author': 'dbenham', 'author_id': 1012053, 'author_profile': 'https://Stackoverflow.com/users/1012053', 'pm_score': 3, 'selected': False, 'text': '<p>The previous answers seem either overly complicated, and/or not particularly suited to a batch file.</p>\n\n<p>This simple one liner should place the desired drive letter in variable myDrive. Obviously change "My Label" to your actual label.</p>\n\n<pre><code>for /f %%D in (\'wmic volume get DriveLetter^, Label ^| find "My Label"\') do set myDrive=%%D\n</code></pre>\n\n<p>If run from the command line (not in a batch file), then %%D must be changed to %D in both places.</p>\n\n<p>Once the variable is set, you can refer to the drive using <code>%myDrive%</code>. For example</p>\n\n<pre><code>dir %myDrive%\\someFolder\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/47849', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4906/']