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
Solved

Generate a relative path from two physical paths

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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