Solved

How to check if a drive is mapped

Posted on 2002-04-23
15
332 Views
Last Modified: 2012-06-21
I want a function that will check if a particular drive is currently mapped and return the drive mapping if it is or return "" if it isn't.  Something I can add a Drive letter parameter to like:

Msgbox DriveMapping("K")

and get a message box that returns something like "\\odinput\psreports" if mapped or "" if not.
0
Comment
Question by:jbauer22
  • 7
  • 4
  • 2
  • +2
15 Comments
 
LVL 4

Expert Comment

by:RichW
ID: 6964040
'You need to have reference to Microsoft Scripting Runtime first.
'Place is listbox on your form
Private Sub cmdInfo_Click()
    '
    Dim fldr As Folder
    Dim fso As New FileSystemObject
    Dim drv As Drive
    '
    Set drv = fso.GetDrive(fso.GetDriveName("C:"))
    '
    With List1
        .AddItem "Available space: " & FormatNumber(drv.AvailableSpace / 1024, 0) & " BK"
        .AddItem "Drive letter: " & drv.DriveLetter
        .AddItem "Drive type: " & drv.DriveType
        .AddItem "Drive file system: " & drv.FileSystem
        .AddItem "Drive free space: " & FormatNumber(drv.FreeSpace / 1024, 0) & " BK"
        .AddItem "Drive is ready: " & drv.IsReady
        .AddItem "Drive path: " & drv.Path
        .AddItem "Root folder: " & drv.RootFolder
        .AddItem "Serial number: " & drv.SerialNumber
        .AddItem "Share name: " & drv.ShareName
        .AddItem "Total size: " & FormatNumber(drv.TotalSize / 1024, 0) & " BK"
        .AddItem "Volume  name : " & drv.VolumeName
    End With
    '
End Sub

RichW
0
 
LVL 2

Author Comment

by:jbauer22
ID: 6964069
Want a function, please
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6964099
I am not sure if i understood you:

Function IsLetterMapped(DriveLetter As String) As String
If Dir$(DriveLetter & ":\*.*", vbArchive) <> "" Then
    IsLetterMapped = DriveLetter & ":\"
Else
    IsLetterMapped = ""
End If

End Function


Private Sub Form_Load()
MsgBox IsLetterMapped("K")
End Sub
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 2

Author Comment

by:jbauer22
ID: 6964133
Richie,

If the Drive is not mapped then a Run-time error 68 will occur.  I tried this same method using the Dir function.  I suppose, I could use error handling to work this out.  I also wanted this function to return the current drive mapping if it was found.  Some string with the //Network...
0
 
LVL 20

Expert Comment

by:hes
ID: 6964187
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 50 total points
ID: 6964545
0
 
LVL 1

Expert Comment

by:hiranmaya
ID: 6965005
Option Explicit

Private Declare Function GetLogicalDriveStrings Lib "kernel32" _
   Alias "GetLogicalDriveStringsA" _
  (ByVal nBufferLength As Long, _
   ByVal lpBuffer As String) As Long
   
   
Private Sub Form_Load()

Debug.Print DriveExists("D:")
   
End Sub


Private Function DriveExists(SDrive As String) As Boolean

   Dim tmp As String
   Dim nBuffersize As Long
   
  'Call the API with a buffer size of 0.
  'The call fails, and the required size
  'is returned as the result.
   nBuffersize = GetLogicalDriveStrings(0&, tmp)

  'pad a string to hold the results
   tmp = Space$(nBuffersize)
   nBuffersize = Len(tmp)
   
  'and call again
   If GetLogicalDriveStrings(nBuffersize, tmp) Then
   
     'if the drive letter passed is in
     'the returned logical drive string,
     'return True. Use vbTextCompare for
     'a case-insensitive match (remembering
     'that when a compare method is specified,
     'the start position must also be specified.
      DriveExists = InStr(1, tmp, SDrive, vbTextCompare)

   End If
   
End Function

0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6965651
Hi hiranmaya, welcome to EE!
Please read guidelines regarding Comments vs. answers at bottom of page
Cheers and happy coding
0
 
LVL 2

Author Comment

by:jbauer22
ID: 6965680
Nice API but it doesn't return the network path. \\network\path
0
 
LVL 2

Author Comment

by:jbauer22
ID: 6965856
Nice API but it doesn't return the network path. \\network\path
0
 
LVL 2

Author Comment

by:jbauer22
ID: 6965858
Nice API but it doesn't return the network path. \\network\path
0
 
LVL 2

Author Comment

by:jbauer22
ID: 6965863
Wish I could figure out how to return the \\network\path given a drive letter.
0
 
LVL 4

Expert Comment

by:RichW
ID: 6965890
Set a reference to the Microsoft Scripting Runtime object

Private Sub Command1_Click()
Dim strShareName As String
strShareName = GetMapping("K")
End Sub


Public Function GetMapping(ByVal varDrive As String) As String
Dim fldr As Folder
Dim fso As New FileSystemObject
Dim drv As Drive
   
On Error GoTo err_mapping
    Set drv = fso.GetDrive(fso.GetDriveName(varDrive & ":"))
     GetMapping = drv.ShareName
Exit Function
err_mapping:
    MsgBox UCase(varDrive) & ":" & "drive Not mapped"

End Function
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6966150
:O
0
 
LVL 2

Author Comment

by:jbauer22
ID: 6966458
Thanks RichW.  Created a new question with 25 points for you to collect.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

813 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

17 Experts available now in Live!

Get 1:1 Help Now