Solved

Retrieving relative path, not UNC path

Posted on 2006-06-14
8
447 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel object stays open 19 71
Updates not working for MS Windows 7 12 152
passing a value with stream reader AFTER a ";" 3 58
Passing a Text Box name to a Sub 6 74
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now