Creating relative directory path given fully-qualified path 
Author Message
 Creating relative directory path given fully-qualified path

Not wanting to reinvent the wheel, does anyone know of useful API functions
(or other code examples) for converting a fully-qualified directory path
string into a relative path string.  For example given the "reference path"
and "fully qualified path" shown below, I would like a function or algorithm
to create the "relative path":

  RefPath = "c:\sub1\sub2\sub3"     ' Reference path
  FullPath = "c:\sub1\dir2\dir3\myfile.doc"  ' Fully-qualified path
  RelPath = RelativePath(FullPath,RefPath)  ' Sets RelPath =
"..\..\dir2\dir3\myfile.doc"

Thanks in advance for any tips you may have.

Mark Cattelino
Communications & Power Industries, Inc.



Sun, 14 Apr 2002 03:00:00 GMT  
 Creating relative directory path given fully-qualified path
Mark,

Quote:
>Not wanting to reinvent the wheel, does anyone know of useful API functions
>(or other code examples) for converting a fully-qualified directory path
>string into a relative path string.

Something like this perhaps...

Public Const MAX_PATH = 260

Declare Function PathRelativePathToA Lib "shlwapi.dll" _
          (ByVal pszPath As String, _
          ByVal pszFrom As String, _
          ByVal dwAttrFrom As Long, _
          ByVal pszTo As String, _
          ByVal dwAttrTo As Long) As Boolean

  Dim RefPath As String
  Dim FullPath As String
  Dim sRelBuffer As String

  RefPath = "c:\sub1\sub2\sub3"     ' Reference path
  FullPath = "c:\sub1\dir2\dir3\myfile.doc"  ' Fully-qualified path
  sRelBuffer = String$(MAX_PATH, 0)
  If PathRelativePathTo(sRelBuffer, RefPath, vbDirectory, _
                                                  FullPath, vbDirectory) Then
    Debug.Print Left$(sRelBuffer, InStr(sRelBuffer, vbNullChar) - 1)
  End If

Do note that this and other shell helper function are only
available on IE4 and later installations, see:
http://msdn.microsoft.com/library/sdkdoc/shellcc/shell/shlwapi/path/p...

--
Brad Martinez, http://www.mvps.org
Please direct questions/replies to the newsgroup

Quote:

>Not wanting to reinvent the wheel, does anyone know of useful API functions
>(or other code examples) for converting a fully-qualified directory path
>string into a relative path string.  For example given the "reference path"
>and "fully qualified path" shown below, I would like a function or algorithm
>to create the "relative path":

>  RefPath = "c:\sub1\sub2\sub3"     ' Reference path
>  FullPath = "c:\sub1\dir2\dir3\myfile.doc"  ' Fully-qualified path
>  RelPath = RelativePath(FullPath,RefPath)  ' Sets RelPath =
>"..\..\dir2\dir3\myfile.doc"

>Thanks in advance for any tips you may have.

>Mark Cattelino
>Communications & Power Industries, Inc.



Sun, 14 Apr 2002 03:00:00 GMT  
 Creating relative directory path given fully-qualified path
Whoops, was a little careless there...

  If PathRelativePathToA(sRelBuffer, RefPath, vbDirectory, _
                                                    FullPath, vbNormal) Then

--
Brad Martinez, http://www.mvps.org
Please direct questions/replies to the newsgroup

Quote:

>Mark,

>>Not wanting to reinvent the wheel, does anyone know of useful API functions
>>(or other code examples) for converting a fully-qualified directory path
>>string into a relative path string.

>Something like this perhaps...

>Public Const MAX_PATH = 260

>Declare Function PathRelativePathToA Lib "shlwapi.dll" _
>          (ByVal pszPath As String, _
>          ByVal pszFrom As String, _
>          ByVal dwAttrFrom As Long, _
>          ByVal pszTo As String, _
>          ByVal dwAttrTo As Long) As Boolean

>  Dim RefPath As String
>  Dim FullPath As String
>  Dim sRelBuffer As String

>  RefPath = "c:\sub1\sub2\sub3"     ' Reference path
>  FullPath = "c:\sub1\dir2\dir3\myfile.doc"  ' Fully-qualified path
>  sRelBuffer = String$(MAX_PATH, 0)
>  If PathRelativePathTo(sRelBuffer, RefPath, vbDirectory, _
>                                                  FullPath, vbDirectory) Then
>    Debug.Print Left$(sRelBuffer, InStr(sRelBuffer, vbNullChar) - 1)
>  End If

>Do note that this and other shell helper function are only
>available on IE4 and later installations, see:
>http://msdn.microsoft.com/library/sdkdoc/shellcc/shell/shlwapi/path/p...

>--
>Brad Martinez, http://www.mvps.org
>Please direct questions/replies to the newsgroup


>>Not wanting to reinvent the wheel, does anyone know of useful API functions
>>(or other code examples) for converting a fully-qualified directory path
>>string into a relative path string.  For example given the "reference path"
>>and "fully qualified path" shown below, I would like a function or algorithm
>>to create the "relative path":

>>  RefPath = "c:\sub1\sub2\sub3"     ' Reference path
>>  FullPath = "c:\sub1\dir2\dir3\myfile.doc"  ' Fully-qualified path
>>  RelPath = RelativePath(FullPath,RefPath)  ' Sets RelPath =
>>"..\..\dir2\dir3\myfile.doc"

>>Thanks in advance for any tips you may have.

>>Mark Cattelino
>>Communications & Power Industries, Inc.



Sun, 14 Apr 2002 03:00:00 GMT  
 Creating relative directory path given fully-qualified path
Brad,

Thanks for the tips -- that's exactly what I needed.

Mark Cattelino
Communications & Power Industries, Inc.

Quote:

>Whoops, was a little careless there...

>  If PathRelativePathToA(sRelBuffer, RefPath, vbDirectory, _
>                                                    FullPath, vbNormal)

Then


Sun, 14 Apr 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Parsing an HTML Document - Change Relative paths to fully qualified paths

2. Getting filename out of fully qualified path

3. How to give the relative path

4. Best method of determining path, relative path and/or flie name

5. convert absolut path to relative path

6. Long Path Names give Runtime error 76 - Path not found

7. Populate combo box with directories from a given path

8. Paths -- Creating File Path with several levels -- VBScript

9. Flexible or relative path for import

10. Error trying to set relative path on linked powerpoint presentation

11. I want absolute path in hyperlinks and linked pitures (word makes them relative)

12. Relative Path to Access DB in VB.NET

 

 
Powered by phpBB® Forum Software