?
Solved

Retrieving relative path, not UNC path

Posted on 2006-06-14
8
Medium Priority
?
456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 35

Accepted Solution

by:
[ fanpages ] earned 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 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