Re: relative path from one absolute path to another? [message #49668 is a reply to message #49660] |
Tue, 08 August 2006 11:39   |
JD Smith
Messages: 850 Registered: December 1999
|
Senior Member |
|
|
On Tue, 08 Aug 2006 07:38:06 -0700, greg michael wrote:
>
> I'd be surprised if you can do it without programming, but it shouldn't
> be hard. Split the path using strsplit() and path_sep(), count how many
> first elements are the same, add a chain of '..'+path_sep() to equal
> the remaining elements in your from-path, and tack on the remaining
> elements of the to-path.
>
> regards,
> Greg
>
>
>
> born.in.sssr@gmail.com wrote:
>> Hi all,
>> is there an easy way (without string scripting) to get the relative
>> path from one absolute path to another (platform independent:
>> unix+win)?
>> In the kind of:
>> rel_path = get_rel_path(from_path, to_path)
>> Thanks, Maxim
I actually had to solve this recently. I don't know if Windows likes
it. Do Windows paths contain the drive letter? If so, it will
probably be unhappy, since even FILE_DIRNAME cannot remove those
obnoxious C:'s.
JD
;+
; NAME:
;
; MAKE_FILENAME_RELATIVE
;
; PURPOSE:
;
; Given a filename and a path, make the former relative to the latter.
;
; CATEGORY:
;
; Filenames
;
; CALLING SEQUENCE:
;
; new=make_filename_relative(file,root)
;
; INPUTS:
;
; file: The absolute filename to turn into a relative filename.
;
; root: The path of the root directory from which to make a relative
; path to file.
;
; OUTPUTS:
;
; new: The new relative filename.
;
;
; MODIFICATION HISTORY:
;
; 2006-07-05 (JDS): Written
;
;-
; $Id: make_filename_relative.pro,v 1.3 2006/07/06 00:51:06 jdsmith Exp $
;########################################################### ###################
;
; LICENSE
;
; Copyright (C) 2006 J.D. Smith
;
; This file is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published
; by the Free Software Foundation; either version 2, or (at your
; option) any later version.
;
; This file is distributed in the hope that it will be useful, but
; WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
; General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this file; see the file COPYING. If not, write to the
; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
; Boston, MA 02110-1301, USA.
;
;########################################################### ###################
function make_filename_relative,path,root_in
up=0L
sep=path_sep()
if strmid(path,0,1) eq '.' then return,path
root=root_in
while root ne sep do begin
match=path
while match ne sep do begin
match=file_dirname(match)
if file_same(match,root) then begin
;; replace match by ./ or ../
return,(up eq 0L?'.':strjoin(replicate(path_sep(/PARENT),up),sep))+ $
strmid(path,strlen(match))
endif
endwhile
root=file_dirname(root)
up++
endwhile
return, '.'+path
end
|
|
|