Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get USB drive's letter

Status
Not open for further replies.

arielap

Technical User
Aug 2, 2003
71
GB
This app reads from and backs up data files to a USB thumb drive.
The problem is that the USB drive is used with several machines and its allocated drive letter varies.

How can I determine the USB's current drive letter?
(VFP7 and XP)
 
This might be helpful.

From MSDN(


Function ShowDriveType(drvpath)
Dim fso, d, t
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(drvpath)
Select Case d.DriveType
Case 0: t = "Unknown"
Case 1: t = "Removable"
Case 2: t = "Fixed"
Case 3: t = "Network"
Case 4: t = "CD-ROM"
Case 5: t = "RAM Disk"
End Select
ShowDriveType = "Drive " & d.DriveLetter & ": - " & t
End Function
 
Dores the thunmb drive not have a volume name? And if not, can one be set?

If the answer to either of the above is 'yes', then you can use the FSO to iterate through the attached drives interrogating the VolumeName propery until you find the drive you want. Something like the following:
Code:
[blue]    Dim objDrive As Object
    
    With CreateObject("Scripting.FileSystemObject")
    For Each objDrive In .Drives
        If objDrive.IsReady Then ' skip unready drives such as empty CD and DVD drives
            If objDrive.VolumeName = "Example" Then
                MsgBox "Drive is " & objDrive.Path
                Exit For
            End If
        End If
    Next
    End With[/blue]
 
thanks - looks as though drivetype() will do the job
 
This sort of thing seems clever... until you run into cases where more than one removable drive is encountered.

People do use ReadyBoost for one thing, and some still have floppy drives.
 
Thanks; this should be OK though.It's for a dedicated netbook, running a VFP app and (for security reasons) using only data kept on a single protected USB stick.
 
>the USB drive is used with several machines and its allocated drive letter varies
>It's for a dedicated netbook

How do theses two statements tally?
 
How do theses two statements tally?

The netbook is used by an on-call technician to run a specific VFP app (tissue typing for heart/lung transplants) when needed out of lab hours.
The data files used are on a security locked USB key.
Each evening the USB key is updated from the server via one of the networked day-time PCs. Changed files are transferred back to the lab network next morning via any free PC.

And drivetype() does do the job thanks

 
I'm sure it does. But either

a) it is for a dedicated machine, in which case drive letters won't change, so drivetype() will work without issue - but is not actually needed

b) it is for multiple machines, in which case drive letters change and may change because they have different numbers of removeable drives in which case drivetype() won't work 100% of the time (dilettante's point)

All we are saying is be careful.
 
>> All we are saying is be careful.<<

Thanks - it'll be error trapped.
(I know xp can do odd things with external drive letter allocation)

 
Yes, what I was suggesting comes down to supplying a DriveListBox to let the user choose the correct drive. You could make your own selecting only removable drives and displaying the volume name in order to help the user make the correct selection.
 
Detecting a USB insertion (which isn't quite what the OP was about) in VB6 is substantially easier than that article indicates. And we've covered it here in thread222-1171051
 
thanks everyone - I don't know how this got into the VB forum .It's for a VFox application and I thought I was in that forum when I sent the original message.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top