Re: relative path from one absolute path to another? [message #49642] |
Wed, 09 August 2006 11:02  |
JD Smith
Messages: 850 Registered: December 1999
|
Senior Member |
|
|
On Wed, 09 Aug 2006 10:58:35 -0700, JD Smith wrote:
> On Wed, 09 Aug 2006 12:52:15 -0400, Craig Markwardt wrote:
>
>>
>> "born.in.sssr@gmail.com" <born.in.sssr@gmail.com> writes:
>>> 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
>>
>> Sigh, a simple search on Google ("idl relative path") would have
>> picked up a routine I posted on my website two years ago, and saved
>> everybody a lot of work and trouble :-)
I forgot to point out one advantage of my formulation: since it uses
FILE_SAME(), you can get minimum relative paths even for path structures
which include arbitrary links.
JD
|
|
|
|
Re: relative path from one absolute path to another? [message #49647 is a reply to message #49643] |
Wed, 09 August 2006 09:52   |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
"born.in.sssr@gmail.com" <born.in.sssr@gmail.com> writes:
> 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
Sigh, a simple search on Google ("idl relative path") would have
picked up a routine I posted on my website two years ago, and saved
everybody a lot of work and trouble :-)
Craig
http://cow.physics.wisc.edu/~craigm/idl/io.html
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|
Re: relative path from one absolute path to another? [message #49660 is a reply to message #49647] |
Tue, 08 August 2006 14:22   |
born.in.sssr@gmail.co
Messages: 3 Registered: August 2006
|
Junior Member |
|
|
Thank you all!
Here is my function, which is closer to greg michael idea but also uses
some interesting constructs from JD Smith:
;+
; NAME:
; FILE_RELPATH
; PURPOSE:
; Relative path from one absolute path to another
; CALLING SEQUENCE:
; rel_path = FILE_RELPATH(file, source_path)
; INPUTS:
; file, source_path: absolute path names
; if source_path is not given, then the current directory is
; taken as the source_path
; OUTPUTS:
; Relative path from source_path to file
; RESTRICTIONS:
; file and source_path should be reachable by file system
; MODIFICATION HISTORY:
; 08/2006 Maxim [Yossarian] Neumann
;-
function FILE_RELPATH, file, source
sep = path_sep()
if n_elements(source) eq 0 then source = '.'
to = strsplit(file_search(file, /FULLY_QUALIFY_PATH),sep,/EXTRACT)
from = strsplit(file_search(source,/FULLY_QUALIFY_PATH),sep,/EXTRAC T)
n_to = n_elements(to) & n_from = n_elements(from)
if n_from eq 1 && from[0] eq '' then n_from=0
ind = where(from ne to, cnt)
same = cnt eq 0 ? n_from<n_to : ind[0]
if same eq n_from then res = './' $
else res = strjoin(replicate('..'+sep,n_from-same))
if same ne n_to then res += strjoin(to[same:*],sep)
return, res
end
|
|
|
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
|
|
|
|
|
Re: relative path from one absolute path to another? [message #49733 is a reply to message #49647] |
Thu, 10 August 2006 01:21  |
born.in.sssr@gmail.co
Messages: 3 Registered: August 2006
|
Junior Member |
|
|
> Sigh, a simple search on Google ("idl relative path") would have
> picked up a routine I posted on my website two years ago, and saved
> everybody a lot of work and trouble :-)
yes, if you use the right keywords from the beginning... i tried it
with "idl retrieve relative pathnames" without much succes on google
and in the group. and spent half an hour to click through some pages.
as usuall, it is faster to write it by yourself; but you learn a lot if
you see, how other people would do it!
Maxim
|
|
|
Re: relative path from one absolute path to another? [message #49741 is a reply to message #49643] |
Wed, 09 August 2006 12:17  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
JD Smith <jdsmith@as.arizona.edu> writes:
> On Wed, 09 Aug 2006 12:52:15 -0400, Craig Markwardt wrote:
>
>>
>> "born.in.sssr@gmail.com" <born.in.sssr@gmail.com> writes:
>>> 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
>>
>> Sigh, a simple search on Google ("idl relative path") would have
>> picked up a routine I posted on my website two years ago, and saved
>> everybody a lot of work and trouble :-)
>
> One can't underestimate the trade-off of having to check someone else's
> code vs. writing it yourself for "small" problems like this. Does it
> work for Windows paths (with c:\foo or \\host\foo)?
Sure. Of course, if the drive letter is different, there is no
possible relative path.
Regarding links and FILE_SAME(), I didn't assume the directories
actually exist on disk. In my case, sometimes the relative path is
devised before the tree exists, so that assumption would have failed.
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|