Solved

Generate a relative path from two physical paths

Posted on 2004-03-27
1
329 Views
Last Modified: 2010-08-05
How can I get the relative path for a file from another physical path?

ie if I have one physical path of e:\1lg.com\htdocs\newspages  and a second physical path of e:\1lg.com\htdocs\images\news\129\test.jpg  how can I get the path of the file relative to the first path?  I need a vbscript function I can call, supplying the two paths which returns the relative path for the file.

Cheers,

GP
0
Comment
Question by:GPneedsHelp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 12

Accepted Solution

by:
monosodiumg earned 125 total points
ID: 10696048
The fn below does it. You might want to make it more robust by replacing any "/" in the paths with "\", checking for existing "..\", handling the presence of a terminating \ in either or both paths properly.


Function RelPath(sRefPath, sPath) As String
 Dim aRelPath As Variant
 Dim aPath As Variant

  aRelPath = Split(sRefPath, "\")
  aPath = Split(sPath, "\")
 
 Dim nCommonCount As Integer
 
 'nShorterPathLen is the maximum possible common length i.e. the lenght of the shorter path
 Dim nShorterPathLen As Integer
 If UBound(aRelPath) > UBound(aPath) Then
   nShorterPathLen = UBound(aPath)
 Else
   nShorterPathLen = UBound(aRelPath)
 End If
 
 
While nCommonCount <= nShorterPathLen And aRelPath(nCommonCount) = aPath(nCommonCount)
  nCommonCount = nCommonCount + 1
Wend

Dim sFinalPath As String
sFinalPath = ""
Dim nIdx As Integer

'The number of "..\" elements required is the Ubound(aRelPath) - nShorterPathLen
For nIdx = 0 To UBound(aRelPath) - nCommonCount
  sFinalPath = sFinalPath + "..\"
Next nIdx

'The number of tail elements to pick up from aPath is Ubound(aPath) -  - nShorterPathLen
For nIdx = nShorterPathLen To UBound(aPath)
  sFinalPath = sFinalPath + aPath(nIdx) + "\"
Next nIdx

'Strip of the extra trailing \:
sFinalPath = Left(sFinalPath, Len(sFinalPath) - 1)

RelPath = sFinalPath
End Function
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Html Table looping 4 42
JQuery Autocomplete Tag AJAX (Need nice script) 11 76
How to post data to an API using ASP Classic 3 51
JQuery on multiple lines 3 49
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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