Results 1 to 11 of 11

Thread: [RESOLVED] Finding the relative path of file

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Resolved [RESOLVED] Finding the relative path of file

    I am starting a new thread on this because the last one I posted turned into a debate between randem and I.

    I was wondering if anyone knew of a class module/DLL out there that finds the relative path of a file in a reliable manner. For example, I would pass these strings to a method:

    "C:\Projects\My Project 1\Forms\MyForm.frm" <--- Path to make relative
    "C:\Projects\My Project 1\" <--- Path to use as absolute
    ... should return ...
    "\Forms\MyForm.frm"

    Here is another example:
    "C:\Common\By wossy\sandpaper.dll" <--- Path to make relative
    "C:\Projects\My Project 1\" <--- Path to use as absolute
    ... should return ...
    "..\..\Common\By wossy\sandpaper.dll"

    There should also be another sub to unfold the relative paths. For example:
    "D:\Another Guy\Coding Stuff\Project 132\" <--- Path to use as absolute
    "\Forms\MyForm.frm"<--- Relative Path
    ... should return ...
    "D:\Another Guy\Coding Stuff\Project 132\Forms\MyFrom.frm"

    Does anyone know of a way to do this reliably? I have a feeling that if I wrote my own code to do it I would miss a few big things. This has to have been done before.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Finding the relative path of file

    You might use the Right() and Len() function to kind of subtract the Absolute path from the path in question...
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  3. #3
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Finding the relative path of file

    probably u can use Split() by "\" and then append or delete the required part of the path!!!!
    Show Appreciation. Rate Posts.

  4. #4

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Finding the relative path of file

    I guess I will just write it then. I think I will split() it on "\" (after replacing any "/" with "\") and then loop through and compare and take action when necessary.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Finding the relative path of file

    Instr() will let you know if you have a folder or not. Don't use anything else first.

  6. #6

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Finding the relative path of file

    What do you mean don't use anything else first?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  7. #7

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Finding the relative path of file

    Okay here is what I came up with. Could you guys help me work out the bugs? I don't see any problems with it, but there must be some:
    VB Code:
    1. '---------------------------------------------------------------------------------------
    2. ' Module    : modRelativePaths
    3. ' DateTime  : 10/16/2005 02:19
    4. ' Author    : Jeremy Blanchard
    5. ' Purpose   :
    6. '---------------------------------------------------------------------------------------
    7.  
    8. Option Explicit
    9.  
    10. '---------------------------------------------------------------------------------------
    11. ' Procedure : MakePathRelative
    12. ' DateTime  : 10/16/2005 02:19
    13. ' Author    : Jeremy Blanchard
    14. ' Purpose   : Make sure to include either a \ at the end or a \filename.txt at the end
    15. '    of the paths
    16. '---------------------------------------------------------------------------------------
    17. '
    18. Public Function MakePathRelative( _
    19.         ByVal sToMakeRelative As String, _
    20.         ByVal sAbsolutePath As String) As String
    21.     Dim i       As Long
    22.     Dim j       As Long
    23.     Dim sRel()  As String
    24.     Dim sAbs()  As String
    25.     Dim sReturn As String
    26.    
    27.     sToMakeRelative = Replace$(sToMakeRelative, "/", "\", , , vbTextCompare)
    28.     sAbsolutePath = Replace$(sAbsolutePath, "/", "\", , , vbTextCompare)
    29.    
    30.     sRel = Split(sToMakeRelative, "\", , vbTextCompare)
    31.     sAbs = Split(sAbsolutePath, "\", , vbTextCompare)
    32.    
    33.     For i = 0 To UBound(sAbs) - 1
    34.         If sRel(i) = sAbs(i) Then
    35.             ' Do nothing but increment i
    36.         Else
    37.             For j = i To UBound(sAbs) - 1
    38.                 sReturn = sReturn & "..\"
    39.             Next j
    40.             Exit For
    41.         End If
    42.     Next i
    43.    
    44.     For i = i To UBound(sRel)
    45.         sReturn = sReturn & sRel(i)
    46.         If i <> UBound(sRel) Then _
    47.             sReturn = sReturn & "\"
    48.     Next i
    49.    
    50.     MakePathRelative = sReturn
    51.    
    52. End Function
    53.  
    54.  
    55. '---------------------------------------------------------------------------------------
    56. ' Procedure : MakePathAbsolute
    57. ' DateTime  : 10/16/2005 02:19
    58. ' Author    : Jeremy Blanchard
    59. ' Purpose   : Make sure to include either a \ at the end or a \filename.txt at the end
    60. '    of the paths
    61. '---------------------------------------------------------------------------------------
    62. '
    63. Public Function MakePathAbsolute( _
    64.         ByVal sRelativePath As String, _
    65.         ByVal sAbsolutePath As String) As String
    66.     Dim i           As Long
    67.     Dim j           As Long
    68.     Dim sRel()      As String
    69.     Dim sAbs()      As String
    70.     Dim sReturn     As String
    71.     Dim sPartial    As String
    72.    
    73.     sRelativePath = Replace$(sRelativePath, "/", "\", , , vbTextCompare)
    74.     sAbsolutePath = Replace$(sAbsolutePath, "/", "\", , , vbTextCompare)
    75.    
    76.     sRel = Split(sRelativePath, "\", , vbTextCompare)
    77.     sAbs = Split(sAbsolutePath, "\", , vbTextCompare)
    78.    
    79.     For i = 0 To UBound(sRel) - 1
    80.         If sRel(i) = ".." Then
    81.             ' Do nothing but increment i
    82.         Else
    83.             For j = i To UBound(sRel)
    84.                 sReturn = sReturn & sRel(j)
    85.                 If j <> UBound(sRel) Then _
    86.                     sReturn = sReturn & "\"
    87.             Next j
    88.             Exit For
    89.         End If
    90.     Next i
    91.    
    92.     For i = 0 To UBound(sAbs) - 1 - i
    93.         sPartial = sPartial & sAbs(i)
    94.         If i <> UBound(sAbs) - i Then _
    95.             sPartial = sPartial & "\"
    96.     Next i
    97.     sReturn = sPartial & sReturn
    98.    
    99.     MakePathAbsolute = sReturn
    100. End Function

    Attached is the sample project.
    Attached Files Attached Files
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

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

    Re: Finding the relative path of file

    I haven't tested your code, but don't you think it would be safer to use the correct API functions instead?
    VB Code:
    1. Private Declare Function PathRelativePathTo _
    2.  Lib "shlwapi.dll" Alias "PathRelativePathToA" ( _
    3.  ByVal pszPath As String, _
    4.  ByVal pszFrom As String, _
    5.  ByVal dwAttrFrom As Long, _
    6.  ByVal pszTo As String, _
    7.  ByVal dwAttrTo As Long) As Long
    8.  
    9. Private Declare Function PathCombine _
    10.  Lib "shlwapi.dll" Alias "PathCombineA" ( _
    11.  ByVal szDest As String, _
    12.  ByVal lpszDir As String, _
    13.  ByVal lpszFile As String) As Long
    14.  
    15. Public Function MakePathRelative( _
    16.  ByVal sFrom As String, _
    17.  ByVal sTo As String) As String
    18.     Const FILE_ATTRIBUTE_DIRECTORY As Long = &H10
    19.     Const MAX_PATH As Long = 260
    20.     Dim sRet As String
    21.     Dim nFlagFrom As Long, nFlagTo As Long
    22.    
    23.     If Right$(sFrom, 1) = "\" Then
    24.         nFlagFrom = FILE_ATTRIBUTE_DIRECTORY
    25.         sFrom = Left$(sFrom, Len(sFrom) - 1)
    26.     End If
    27.     If Right$(sTo, 1) = "\" Then
    28.         nFlagTo = FILE_ATTRIBUTE_DIRECTORY
    29.         sTo = Left$(sTo, Len(sTo) - 1)
    30.     End If
    31.     sRet = Space$(MAX_PATH)
    32.     If PathRelativePathTo(sRet, sFrom, nFlagFrom, sTo, nFlagTo) Then
    33.         MakePathRelative = Left$(sRet, InStr(sRet, vbNullChar) - 1)
    34.     End If
    35. End Function
    36.  
    37. Public Function MakePathAbs(ByVal sRel As String, ByVal sAbs As String)
    38.     'sAbs must be the path of a directory and NOT a file
    39.     'The relative path can be a relative path to a file
    40.     Const MAX_PATH As Long = 260
    41.     Dim sRet As String
    42.     sRet = Space$(MAX_PATH)
    43.     If PathCombine(sRet, sAbs, sRel) Then
    44.         MakePathAbs = Left$(sRet, InStr(sRet, vbNullChar) - 1)
    45.     End If
    46. End Function
    In this case I did as you and in MakePathRelative you must end the path strings with a backslash to indicate a directory, if not it assumes it is the path to a file. In MakePathAbs the absolute path must be to a directory, but it doesn't matter if it has a trailing backslash or not since a directory is assumed. The relative path could be either to a file or a directory.

  9. #9

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Finding the relative path of file

    Well now you tell me!

    That is what I had been looking for for the last few days when I started 2 threads on this subject. After I finally get my act together and write some code, you show me what I wanted the whole time.

    Thanks though! I'll take a look at it later today.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

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

    Re: [RESOLVED] Finding the relative path of file

    Well, I beg your pardon for not monitoring all the threads you start and immediatly reply to them . But the fact is that I didn't see this thread before you sent an e-mail to me about it

  11. #11

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [RESOLVED] Finding the relative path of file



    I was just giving you a bad time.

    Thanks a ton for the code though.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

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