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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating the path length 1

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
GB
I need to calculate a path length, accounting for the fact that the path may be mapped to another drive.

For example, if I have the path "F:\Data\Docs" it is 12 characters on the face of it, but F might be mapped to "T:\Accounts\Local office" in which case there's another 20 or so characters to add on.

Is it possible to resolve a mapped drive letter in VB and get the full path back?

Thanks in advance,

- Andy.
 
Here's the hard way....
[tt]
Private Const RESOURCETYPE_ANY = &H0
Private Const RESOURCE_CONNECTED = &H1
Private Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As Long
lpRemoteName As Long
lpComment As Long
lpProvider As Long
End Type
Private Declare Function lstrlen Lib "kernel32" _
Alias "lstrlenA" (ByVal lpString As Any) As Long
Private Declare Function lstrcpy Lib "kernel32" _
Alias "lstrcpyA" (ByVal lpString1 As Any, _
ByVal lpString2 As Any) As Long
Private Declare Function WNetOpenEnum Lib "mpr.dll" _
Alias "WNetOpenEnumA" _
(ByVal dwScope As Long, ByVal dwType As Long, _
ByVal dwUsage As Long, lpNetResource As Any, _
lphEnum As Long) As Long
Private Declare Function WNetEnumResource Lib "mpr.dll" _
Alias "WNetEnumResourceA" _
(ByVal hEnum As Long, lpcCount As Long, _
lpBuffer As Any, lpBufferSize As Long) As Long
Private Declare Function WNetCloseEnum Lib "mpr.dll" _
(ByVal hEnum As Long) As Long

Function LetterToUNC(DriveLetter As String) As String
On Error Resume Next
Dim hEnum As Long
Dim NetInfo(1023) As NETRESOURCE
Dim entries As Long
Dim nStatus As Long
Dim LocalName As String
Dim UNCName As String
Dim i As Long
Dim r As Long
nStatus = WNetOpenEnum(RESOURCE_CONNECTED, _
RESOURCETYPE_ANY, 0&, ByVal 0&, hEnum)
LetterToUNC = DriveLetter
If ((nStatus = 0) And (hEnum <> 0)) Then
entries = 1024
nStatus = WNetEnumResource(hEnum, entries, _
NetInfo(0), CLng(Len(NetInfo(0))) * 1024)
If nStatus = 0 Then
For i = 0 To entries - 1
LocalName = &quot;&quot;
If NetInfo(i).lpLocalName <> 0 Then
LocalName = Space(lstrlen( _
NetInfo(i).lpLocalName _
) + 1)
r = lstrcpy( _
LocalName, NetInfo(i).lpLocalName _
)
End If
If Len(LocalName) <> 0 Then
LocalName = Left(LocalName, _
(Len(LocalName) - 1) _
)
End If

If UCase$(LocalName) = UCase$(DriveLetter) Then
UNCName = &quot;&quot;
If NetInfo(i).lpRemoteName <> 0 Then
UNCName = Space(lstrlen( _
NetInfo(i).lpRemoteName) + 1 _
)
r = lstrcpy( _
UNCName, NetInfo(i).lpRemoteName _
)
End If
If Len(UNCName) <> 0 Then
UNCName = Left( _
UNCName, (Len(UNCName) - 1) _
)
End If
LetterToUNC = UNCName
Exit For
End If
Next i
End If
End If
nStatus = WNetCloseEnum(hEnum)
End Function

Private Function PathLength(ShortPath$) As Integer
PathLength = Len(LetterToUNC(Left$(ShortPath$, 2)) _
& Right$(ShortPath$, Len(ShortPath$) - 2))
End Function
[/tt]
Get the length of the path....
[tt]
MyPath$ = &quot;I:\Vorpal Shield\FileInfo&quot;
MsgBox &quot;Length of path to &quot; _
& MyPath$ & &quot; = &quot; & PathLength(MyPath$)
[/tt]
Here's the easy way....
Place a Drive List Box on a form and check the items against the first two letters of the test path.
[tt]
MyPath$ = &quot;I:\Vorpal Shield\FileInfo&quot;
For Re = 1 To Drive1.ListCount - 1
TmpPath$ = Drive1.List(Re)
If UCase(Left(TmpPath$, 2)) = _
UCase(Left$(MyPath$, 2)) Then
If InStr(TmpPath$, &quot;\&quot;) > 0 Then 'Mapped Drive?
MyTmpPath$ = Mid$(TmpPath$, InStr( _
TmpPath$, &quot;[&quot;) + 1, _
Len( _
TmpPath$) - InStr(TmpPath$, &quot;[&quot; _
) _
)
LengthOfPath = Len(MyTmpPath$) + Len(MyPath$) - 3
Else
LengthOfPath = Len(MyPath$)
End If
Exit For
End If
Next
[/tt]
Both ways should produce similar results.
[tt]
MsgBox PathLength(MyPath$) & &quot;=&quot; & LengthOfPath
[/tt]
'====================================================================


VCA.gif
 
hi,

If its a network drive,check out thread222-14111

Sunaj
 
More help please!

I used the code above (the hard way) and it works fine in that if you pass it a path it will return the UNC name of the mapped drive. However, that's not really what I needed!

Having got the UNC name, is there any way of returning the local mapping name for that drive on the remote machine?

For example, if my local drive Z is mapped to \\SERVER1\DOCS, and on server1 the Docs folder is held as D:\Data\Docs, how do I get the D:\Data bit?

Alternatively, can you answer this question: VB is pretty stupid in that it allows you to copy a file into a folder which (if it's nested deeply within the filing tree) cannot subsequently be opened, renamed or deleted in Explorer because it exceeds the Windows limit of 255 characters for a filename. If the user is copying a file to a mapped drive, how can I ensure that filename will not exceed the limit?

- Andy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top