Solved

Generate a relative path from two physical paths

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now