Solved

Reference volume by label

Posted on 2011-09-27
18
886 Views
Last Modified: 2012-05-12
Is there a way to reference a volume by label instead of drive letter in a Windows path specification? For example, if I have a D: drive labeled "MY_DRIVE", how would I specify the following folder using "MY_DRIVE" instead of "D:\"?

    D:\FolderName\FileName
0
Comment
Question by:alconlabs
  • 5
  • 4
  • 4
  • +3
18 Comments
 
LVL 3

Expert Comment

by:RushB
ID: 36713844
Yes, what I think you may be looking for is mountvol?

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/mountvol.mspx?mfr=true

You can use it to mount volumes using a name., as long as it's not the drive you are booting from? You just need to get the GUID of the drive.

Find it here using regedit->
HKLM\System\MountedDevices

0
 

Author Comment

by:alconlabs
ID: 36713897
Well, I don't have ready access to the GUID, just the volume name. (I'm writing a script.)
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36716500
Can you try the following command from a command prompt and see if it returns the drive letter you are looking for.  If it does then we can include this in your BAT file to get the drive letter that corresponds to the label name you know, and then you can use that drive letter for file processing.  Change "data" below to the volume label you know.

wmic logicaldisk where volumename='data' get name

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36717124
I have one here for you:

http://scripts.dragon-it.co.uk/links/batch-find-drive-by-name

which I guess is whre Bill was leading?

If you are running the script FROM the drive you can get the path of the script too using %~0 which you can get the drive from with %~d0 etc. if that helps too.

Steve
@echo off 
set drive=
set lookfor=MY_DRIVE
for /f "tokens=1" %%a in ('WMIC logicaldisk where VolumeName^="%lookfor%" get caption') do set drive=%%a

if not "%drive%"=="" (
  echo Found drive at %drive%
) ELSE (
  echo No drive named %lookfor% found.  Logical drives are:
  WMIC logicaldisk get caption, volumename, description, size, freespace
)

Open in new window

0
 
LVL 38

Expert Comment

by:BillDL
ID: 36895772
Try this and see what results you get.  Ignore "Audio CD" as it is not a Volume Label.  If it reports the drive volume labels correctly, then the theory can be used in another batch file for whatever other purpose you would like.
Drive-Names.cmd
0
 

Author Comment

by:alconlabs
ID: 36907457
Billprew's solution fails to find the specified drive.

Dragon-it lists the drives and volumes correctly, but fails to find the specified drive.

BillDL's solution correctly lists the volume names and corresponding drive letters, but doesn't indicate whether a volume can be referenced directly by name instead of letter. My conclusion based on these answers is that it cannot.

Thanks
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36907481
All of these approaches follow the same approach.  It is true that you cannot directly access folders and files on a disk purely based on the volume name.  You need to find out the drive letter that has been assigned, and that is what these approaches try to accomplish.

If you don't want to go down this path, getting the drive letter and then using that to access the drive, then none of these solutions will help.

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36907990
agreed with bills coments there.  if you arent getting a drive back from my slution then please remove the echo off from the top and copy / paste the results here.

What are you trying to find?  the drive letter of a backup drive, usb key, the drive the batch is run from, or something else?

Steve
0
 
LVL 38

Accepted Solution

by:
BillDL earned 500 total points
ID: 36911508
Hi alconlabs

I won't reiterate what Bill Prew and Steve have said above, but my mind went in the same direction the first time I read your question.  Why exactly do you want or need to access one or more drives using their Volume Label rather than just the Drive Letter?

The only way to get your Volume Label and then use it in a script (eg. batch file) is to run a query and then assign the Volume Label as a reusable variable.  You cannot just look at Windows Explorer, see that your External USB Hard Drive has the volume label "DOCBACKUP" and then open a Command ("DOS") window and do a directory listing like this:
DIR DOCBACKUP:\*.*

You forst have to get that volume label, assign it as a variable, and then use that in the command.  A Variable is just a container for some other data, whether it is a fole path, a text string, a numeric value, or whatever.  There are a number of built-in System Variables that you can see by typing the command     SET    with no parameters in a command window.  Take the example of the line that will show as:
USERPROFILE=C:\Documents and Settings\Bill
The variable named USERPROFILE is just storing a text string retrieved from the registry, and in a command window you can get it to show the value of that text string at any time with the command:
echo %USERPROFILE%
or you can use it for example to copy a file from somewhere to your Desktop like this:
COPY "C:\SomeFolder\FileName.txt" "%USERPROFILE%\Desktop\BNewName.txt"

It just saves some typing when used like this, and the variable name is reusable whenever needed.

There are few reasons that I can think of where you would need to know the Volume Label of a drive and use that rather than just the drive letter.  Steve hit on one of them, ie. a USB Flash Drive that might be assigned a different drive letter if other removable drives are connected at the same time, or perhaps if the Flash Drive was inserted into another computer.  In that case it would make sense to have a universal batch file that picks out the volume label of the Flash Drive, but you still need to run a command to find it and assign its Volume Label to a Variable.

For example, say you need a batch file that you run regularly to copy the same *.DAT file from an application folder to the same Flash Drive, and you obviously know the Volume Label.

First off, the following attached batch file will list drives like this:
C,SYSTEM
D,DATA
E,STORAGE
F,Audio CD
I,FREECOM
L,CRUCIAL
 Drive-Names.cmd
The following attached batch file will list drives like this (it disregards the drive letters that would return "X: - No such Root Directory"):
C,Fixed Drive
D,Fixed Drive
E,Fixed Drive
F,CD-ROM Drive
G,CD-ROM Drive
I,Fixed Drive
L,Removable Drive
 Drive-Types.cmd

Knowing the Volume Label makes it easy to get the currently assigned Drive Letter and use that.  Let's say I want to use my USB Flash Drive with the Volume Label "CRUCIAL", which is currently assigned as L: on this computer.

The following attached batch file searches for the Volume Label for your USB Flash Drive that you specify in the batch file, it matches this to the currently assigned Drive Letter, and then checks if that Drive Letter is a Removable Drive.  You can place whatever commands you want in the IF/Else tests, but it's probably best to precede those commands with an echo until you are satisfied that it will do what you want.  It is pretty failsafe, but I cannot guarantee that there wouldn't be any unforseen glitches.  This was tested on a Windows XP system.
 Find-Flash-Drive.cmd

There are loads of ways to skin a cat.  I just chose the FSUTIL command for my examples.  You can get the Volume Label by parsing the results of a DIR command just the same, and there are probably a few other commands that check the Drive Type.

Bill
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 38

Expert Comment

by:BillDL
ID: 36911545
Hmmm. Some spoiling mistakes in there ("forst", "fole path").  I sound like Oi'm from Donegal, Oirland.  To keep the accent you could rename that last batch foil to "Foind-Flash-Droive.cmd" ;-)
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36911553
:-)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36912300
to be sure.
0
 

Author Closing Comment

by:alconlabs
ID: 36912634
I'm trying to write a batch file in WinPE 3.0 to deploy images on systems that come in various hardware configurations, including attached removable USB drives. The drive letter assignments vary, but the volume names are (somewhat) consistent. I'm not expert in writing batch files, so the idea was to find a way to reference volumes by name rather than letter. So the short answer is I can't do that directly, but there are alternatives (as described above).
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36913186
I wonder why we bother sometimes....
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36913435
+1
0
 
LVL 38

Expert Comment

by:BillDL
ID: 36925310
Thank you alconlabs.
0
 

Expert Comment

by:RoyDBaird
ID: 37024130
Bill, bill and dragon-it,
THANKS!!!!  I was looking for a solution to get the drive letter of a USB volume so I can run a program from a link in a CMH file.  

You guys Rock!!!

thanks again,
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37025028
No problem, glad it helped.

Steve
0

Featured Post

Do email signature updates give you a headache?

Do you feel like you are constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Join & Write a Comment

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now