Parsing a UNC path

J

Joe Williams

How can a parse a UNC path to get just the hostname of the UNC path? for
instance, \\servername\sharename\foldername, I just want servername with no
\\.

It also has to be intelligent enough to deal with UNC paths of varying
lengths, for intance one time it might be \\servername\sharename or another
time it might be \\servername\sharename\foldername\foldername. In either
case I just need the servername returned.

Or is there a function that will simply just return the host name (NOT full
path) of a given drive letter?

Thanks!

Joe
 
J

Joe Williams

I was playing around and came up with this:

ParseUNC = Mid(strUNC, 3, InStr(3, strUNC, "\") - 3)

it seems to work although I am assuming the the hostname will ALWAYS start
on the 3rd character (following two "\\"s. Is that a safe assumption that
will work in all cases?

As you can see, I also have to start on the 3rd character with the instr
function and then back it off the end so the "\\"s at the beginning aren't
read.

Any other more elegant way to get it done?

Thanks

joe
 
A

Albert D. Kallal

Any other more elegant way to get it done?

Hum, what you have is not bad, you could use:

ParseUNC = split(split(strUNC,"\\")(1),"\")(0)


Not much cleaner then what you have.........

or, perahps

ParseUNC = mid(strUNC,3)
ParseUNC = split(ParseUNC,"\")(0)
 
C

Chuck Grimsby

Or is there a function that will simply just return the host name (NOT full
path) of a given drive letter?

The functions (and API calls) below should help you out.


Private Const ERROR_NONE = 0
Private Declare Function WNetGetConnection _
Lib "mpr.dll" _
Alias "WNetGetConnectionA" _
(ByVal lpszLocalName As String, _
ByVal lpszRemoteName As String, _
cbRemoteName As Long) As Long
Private Declare Function PathIsNetworkPath _
Lib "shlwapi" _
Alias "PathIsNetworkPathA" _
(ByVal pszPath As String) _
As Long

Public Function GetUNCDriveMapping _
(ByVal strLocalDrive As String) _
As String
Dim strRemoteDrive As String
Const lenBufLen As Long = 256
Dim lenResult As Long
strLocalDrive = strLocalDrive & " "
strLocalDrive = UCase$(Left$(strLocalDrive, 2))
If Right$(strLocalDrive, 1) = ":" Then
GetUNCDriveMapping = ""
strRemoteDrive = String$(lenBufLen, 0)
lenResult = WNetGetConnection(strLocalDrive, _
strRemoteDrive, _
lenBufLen)
If lResult = ERROR_NONE Then
GetUNCDriveMapping = Left$(strRemoteDrive, _
InStr(strRemoteDrive, _
vbNullChar) - 1)
End If
End If
End Function

Public Function GetUNCServerName(ByVal sPath As String) As String
' assumes path passed is in UNC format (\\server\share.....)
If IsPathNetPath(sPath) Then
GetUNCServerName = Mid$(sPath, 3, InStr(3, sPath, "\") - 3)
End If
End Function
Public Function IsPathNetPath(ByVal sPath As String) As Boolean
'Determines whether a path string
'represents a network resource. Returns
'True if the string represents a
'network resource, or False otherwise.
'PathIsNetwork interprets two types of
'paths as network paths:
' Paths that begin with two backslash
' (\) characters are interpreted as UNC paths.
' Paths that begin with a letter followed
' by a colon :)) are interpreted as a mounted
' network drive.
'The path is not checked to see if it refers
'to an actual network server.
IsPathNetPath = PathIsNetworkPath(sPath) = 1
End Function
 
D

David W. Fenton

If lResult = ERROR_NONE Then

That should be lenResult, according to the variable you Dim'd and
set. I'd call it lngResult myself, but that would require changing
all uses of the variable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top