Solved

Generate a relative path from two physical paths

Posted on 2004-03-27
1
328 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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Summernote and form validation 10 220
Select2 jquery help 9 100
Auto Submit on dropdown box 14 78
Sending ASP to server side 8 28
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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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