Solved

Retrieving relative path, not UNC path

Posted on 2006-06-14
8
446 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

706 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

17 Experts available now in Live!

Get 1:1 Help Now