Results 1 to 6 of 6

Thread: Resolve Relative Path [Resolved]

  1. #1

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Resolved Resolve Relative Path [Resolved]

    Is there a way to resolve a relative path reference in VB 6? For example, if I have reference to the file "..\SomeFile.dat" (with the ..\ meaning that the file is located one level above the current directory), is there an API function or some other method to expand the ".."?
    Last edited by BruceG; Mar 6th, 2005 at 09:31 PM.
    "It's cold gin time again ..."

    Check out my website here.

  2. #2

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Resolve Relative Path

    Quote Originally Posted by RhinoBull
    Take a look at the Sample By Randy Birch . There are many other samples.
    That only returns true or false if you pass a relative path to it. The following code will "unfold" a relative path. However it doesn't actually checks to see if the path exists, so in your case you should pass CurDir & "..\SomeFile.dat" for it to create a correct path.
    VB Code:
    1. Private Declare Function PathCanonicalize _
    2.  Lib "shlwapi.dll" _
    3.  Alias "PathCanonicalizeA" ( _
    4.  ByVal pszBuf As String, _
    5.  ByVal pszPath As String) As Long
    6.  
    7. Public Function UnfoldRelativePath(ByVal sPath As String) As String
    8.     Dim sBuff As String
    9.    
    10.     sBuff = Space$(261)
    11.     If PathCanonicalize(sBuff, sPath) Then
    12.         UnfoldRelativePath = Left$(sBuff, InStr(sBuff, vbNullChar) - 1)
    13.     Else
    14.         UnfoldRelativePath = sPath
    15.     End If
    16. End Function

  4. #4

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: Resolve Relative Path

    Thanks, Joacim!
    It took a little playing around with, but this is what finally worked for me (the file I want to reference resides one level above where the app is running:
    MsgBox UnfoldRelativePath(App.Path & "\..") & "\Somefile.dat"
    "It's cold gin time again ..."

    Check out my website here.

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Resolve Relative Path [Resolved]

    Why wouldn't this work:
    MsgBox UnfoldRelativePath(App.Path & "\..\Somefile.dat")

  6. #6

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: Resolve Relative Path [Resolved]

    Actually, that does work. (As I mentioned, I was experimenting with various combinations until I saw the light; your example simplifies mine.)
    "It's cold gin time again ..."

    Check out my website here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width