Solved

Retrieving relative path, not UNC path

Posted on 2006-06-14
8
449 Views
Last Modified: 2010-04-07
Hi,

I'm running an .exe from a Windows 2003 network share (\\myserver\DATA\Files\MyApp.exe).
When i run this code:

Msgbox App.Path

I will get this result: "\\myserver\DATA\Files"

On the computer in run this on, \\myserver\DATA\ is mapped to F:\

What i want, is to get "F:\Files" as a result, and NOT "\\myserver\DATA\Files"
How can i accomplish this ??

Thanks in advance for any help,
David.

0
Comment
Question by:DavidNL
  • 4
  • 2
  • 2
8 Comments
 
LVL 35

Accepted Solution

by:
[ fanpages ] earned 100 total points
ID: 16902342
Hi David,

How about...

MsgBox strFind_Mapped_Path_From_UNC_Path(App.Path)

?

Where strFind_Mapped_Path_From_UNC_Path is defined as...

Option Explicit
Public Function strFind_Mapped_Path_From_UNC_Path(ByVal strUNC_Path As String) As String

' ----------------------------------------------------------------------------------------------
' Experts Exchange Question:
' Home \ All Topics \ Programming \ Languages \ Visual Basic
' http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21885892.html
' Retrieving relative path, not UNC path
'
' Copyright (c) 2005 Clearlogic Concepts (UK) Limited
' N.Lee [ http://NigelLee.info ] - 14 June 2006
' ----------------------------------------------------------------------------

  Dim intLoop                                       As Integer
  Dim objWScript_Network                            As Object
  Dim objWScript_Network_EnumNetworkDrives          As Object
  Dim strReturn                                     As String
         
  On Error Resume Next
 
  strReturn = strUNC_Path                           ' Defaults to return original Path
 
  Set objWScript_Network = CreateObject("WScript.Network")
 
  If Not (objWScript_Network Is Nothing) Then
     Set objWScript_Network_EnumNetworkDrives = objWScript_Network.EnumNetworkDrives
 
     If Not (objWScript_Network_EnumNetworkDrives Is Nothing) Then
        For intLoop = 0 To objWScript_Network_EnumNetworkDrives.Count - 1 Step 2
            If Left$(strUNC_Path, Len(objWScript_Network_EnumNetworkDrives.Item(intLoop + 1))) = objWScript_Network_EnumNetworkDrives.Item(intLoop + 1) Then
               strReturn = Mid$(strUNC_Path, Len(objWScript_Network_EnumNetworkDrives.Item(intLoop + 1)) + 1)
               strReturn = objWScript_Network_EnumNetworkDrives.Item(intLoop) & _
                           IIf(Left$(strReturn, 1) = Application.PathSeparator, "", Application.PathSeparator) & _
                           strReturn
               Exit For
            End If
        Next intLoop
     End If
  End If
 
  Set objWScript_Network_EnumNetworkDrives = Nothing
  Set objWScript_Network = Nothing
 
Exit_strFind_Mapped_Path_From_UNC_Path:

  On Error Resume Next
 
  strFind_Mapped_Path_From_UNC_Path = strReturn
 
  Exit Function
 
Err_strFind_Mapped_Path_From_UNC_Path:

  On Error Resume Next
 
  strReturn = strUNC_Path                           ' Defaults to return original Path
   
  Resume Exit_strFind_Mapped_Path_From_UNC_Path

End Function


BFN,

fp.
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 16902574
Option Explicit

Private Declare Function GetModuleFileNameW Lib "kernel32" ( _
    ByVal hModule As Long, _
    ByVal lpFileName As Long, _
    ByVal nSize As Long) As Long
   
Public Function GetPath()
    Dim Location(512) As Byte
    GetModuleFileNameW 0&, ByVal VarPtr(Location(0)), 512
    GetPath = Left$(Location, InStr(Location, Chr$(0)) - 1)
    Erase Location
End Function

Private Sub Form_Load()
    MsgBox GetPath
End Sub
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 16903031
For my example above make sure you compile your exe to the location and run it, otherwise it will show the .exe location of the VB6.exe if you run it in the IDE
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 16903073
(My suggestion can be run from either the IDE or a .EXE and, of course, is not restricted to just the application path, you can pass any UNC path to the function)
0
 

Author Comment

by:DavidNL
ID: 16903408
Hi, first of all thanks for the quick reply.

fanpages, thanks for the nice code.
I've changed it a little, and it worked. Had to add Ucase and disabled some lines. Here's the changed lines:

If left$(UCase(strUNC_Path), Len(objWScript_Network_EnumNetworkDrives.Item(intLoop + 1))) = UCase(objWScript_Network_EnumNetworkDrives.Item(intLoop + 1)) Then
               strReturn = Mid$(strUNC_Path, Len(objWScript_Network_EnumNetworkDrives.Item(intLoop + 1)) + 1)
               strReturn = objWScript_Network_EnumNetworkDrives.Item(intLoop) & strReturn

'& _
'                           IIf(left$(strReturn, 1) = Application.PathSeparator, "", 'Application.PathSeparator) & _
'                           strReturn
'               Exit For
'            End If


As you can see i've commented the part where 'Application.PathSeparator' is used.
Its an Office property? What exactly is the use of this ?

Regards,
David.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 16907697
Hi David,

Sorry... yes, I took the code from Excel/VBA where Application.PathSeparator is defined (in a Windows environment) as "\".

Just replace occurences of Application.PathSeparator with "\".


Good suggestion with the UCase$() function.  Something I missed :)

If you need anything further, please write back.

Thanks.

BFN,

fp.
0
 

Author Comment

by:DavidNL
ID: 16909421
Thanks alot for your help fanpages ! Solved my problem :-)
FYI, here's the procedure that i'm using now:


Public Function FindMappedPathFromUNCPath(ByVal strUNC_Path As String) As String
Dim intLoop                                       As Integer
Dim objWScript_Network                            As Object
Dim objWScript_Network_EnumNetworkDrives          As Object
Dim strReturn                                     As String
' ----------------------------------------------------------------------------------------------
' Experts Exchange Question:
' Home \ All Topics \ Programming \ Languages \ Visual Basic
' http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21885892.html
' Retrieving relative path, not UNC path
'
' Copyright (c) 2005 Clearlogic Concepts (UK) Limited
' N.Lee [ http://NigelLee.info ] - 14 June 2006
' ----------------------------------------------------------------------------
  On Error Resume Next
 
  strReturn = strUNC_Path                           ' Defaults to return original Path
 
  Set objWScript_Network = CreateObject("WScript.Network")
 
  If Not (objWScript_Network Is Nothing) Then
     Set objWScript_Network_EnumNetworkDrives = objWScript_Network.EnumNetworkDrives
 
     If Not (objWScript_Network_EnumNetworkDrives Is Nothing) Then
        For intLoop = 0 To objWScript_Network_EnumNetworkDrives.count - 1 Step 2
            If left$(UCase(strUNC_Path), Len(objWScript_Network_EnumNetworkDrives.Item(intLoop + 1))) = UCase(objWScript_Network_EnumNetworkDrives.Item(intLoop + 1)) Then
               strReturn = Mid$(strUNC_Path, Len(objWScript_Network_EnumNetworkDrives.Item(intLoop + 1)) + 1)
               strReturn = objWScript_Network_EnumNetworkDrives.Item(intLoop) & IIf(left$(strReturn, 1) = "\", "", "\") & strReturn
               Exit For
            End If
        Next intLoop
     End If
  End If
 
  Set objWScript_Network_EnumNetworkDrives = Nothing
  Set objWScript_Network = Nothing
 
Exit_strFind_Mapped_Path_From_UNC_Path:

  On Error Resume Next
 
  FindMappedPathFromUNCPath = strReturn
 
  Exit Function
 
Err_strFind_Mapped_Path_From_UNC_Path:

  On Error Resume Next
 
  strReturn = strUNC_Path                           ' Defaults to return original Path
   
  Resume Exit_strFind_Mapped_Path_From_UNC_Path

End Function



Kind regards,
David.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 16910043
Thanks for coming back with your finished function, David.

And thanks for closing the question so promptly, too.

BFN,

fp.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

809 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