Referring to a drive with a label?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Ville
Newbie
Posts: 2
Joined: Mon May 17, 2010 7:00 pm
Location: North Texas, USA
Contact:

Referring to a drive with a label?

Post by Ville » Mon May 17, 2010 7:08 pm

Is there an [easy] way to refer to a drive by its label? I'm mostly thinking this in the context of external/removable drives where I'd like a script to take a periodic action (like write into a file) on an external drive if it's present. However, since removable drives may easily be reassigned a different drive letter at different times, such writes are unreliable with simple drive letter reference.

There's an older post that provides a list of active drives, but I was wondering if there's a simpler way, like MYDRIVELABEL:\somedir\somefile.txt (but that didn't work :)).

Thanks for any insights on this!

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue May 18, 2010 2:32 pm

I'm never absolutely certain about anything. However, the info on This Microsoft Website makes me think you can NOT access a drive using a label.

Specifically:
Microsoft wrote:A volume mount point is any user-mode path that can be used to access a volume. There are three types of volume mount points:

* A drive letter, for example, C:\.
* A volume GUID path, for example, \\?\Volume{26a21bda-a627-11d7-9931-806e6f6e6963}\.
* A mounted folder, for example, C:\MountD\.
Note that using a label is not mentioned as a way to access a "mount point" which is what we think of as "drives".


I think a method you could use would be to put a file with a unique name at the root of the removable drive. Then create a loop and use IfFileExists> to search each potential drive letter to find the unique file. When you find the file, you have your drive.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue May 18, 2010 2:59 pm

Use a version of the post you linked to to list through all available drives, look at their VolumeName (which I guess is what you mean by Label) and if it matches, get the drive letter. Then you can use that in your file operations.

E.g:

Code: Select all

VBSTART
Function GetLetterFromName(VolName)
  on error resume next
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set oDrive = fso.Drives ' Get Drives collection.
  For Each curDrive In oDrive ' All drive objects
    if curDrive.VolumeName = VolName Then
      GetLetterFromName = curDrive.DriveLetter
    End if
  Next  
End Function
VBEND

VBEval>GetLetterFromName("Data"),drive_letter

WriteLn>%drive_letter%:\myfolder\myfile.txt,res,New Line
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue May 18, 2010 4:19 pm

A Volume Label is not necessarily unique. I have several Patriot brand USB memory sticks and every one of them has "Patriot" as the volume label. Although I could change the volume label, I think its easier to place a zero byte file on the root of the drive and test for that file's existence. Also according to the previously mentioned Microsoft page:
Microsoft wrote:Several factors can make it difficult to identify specific volumes using only drive letters and labels. One is that a volume is not required to have a drive letter or a label. Another is that two different volumes can have the same label, which makes them indistinguishable except by drive letter. A third factor is that drive letter assignments can change as volumes are added to and removed from the computer.
Therefore it is not possible to be certain you have the correct drive simply by looking at the volume label.

The following will report all drives that contain a file located at the drive root named to your specification.

Code: Select all

VBSTART
VBEND

Let>filename=uniquefile.txt
Let>drive=
//Start at 65 to remove A:\ drive from the search
//Start at 66 to remove A:\ and B:\ from the search
//Etc.
Let>kk=64
Repeat>kk
  Add>kk,1
  VBEval>chr(%kk%),res
  IfFileExists>%res%:\%filename%
    Concat>drive,%res%%CRLF%
  EndIf
Until>kk,90

MDL>Drive

Ville
Newbie
Posts: 2
Joined: Mon May 17, 2010 7:00 pm
Location: North Texas, USA
Contact:

Post by Ville » Wed May 19, 2010 8:34 am

Thanks for the responses! Yes, Label, or VolumeName may not be unique. While in this particular environment I can be sure that unique volume names are used as a trigger, I like the idea of using a zero byte identifier file on the drive; it seems to be pretty straightforward to implement and guarantees the selected drive is indeed the one the script should access.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed May 19, 2010 8:39 am

Maybe you could use a combination of both methods.

My understanding was also that you are wanting to read or write to a file on the drive. If that is the case a further check would be that the file you expect to read is there. You'd have to do that anyway.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts