Solved

Retrieving relative path, not UNC path

Posted on 2006-06-14
8
450 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 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