Modify

Ticket #5897 (new Feature Requests)

Opened 3 years ago

Last modified 9 months ago

Make path relative function.

Reported by: jmckesson@… Owned by: bemandawes
Milestone: To Be Determined Component: filesystem
Version: Boost 1.47.0 Severity: Problem
Keywords: Cc:

Description

It is often useful to take a path that is absolute and make it relative to some other path. For example, if you're copying from directory A to directory B using a recursive_directory_iterator, it is often the case that you will do the following:

for_each(bfs::recursive_directory_iterator(directoryA), bfs::recursive_directory_iterator(), [=](const bfs::directory_entry &entry)
{
  if(bfs::is_file(entry.status()))
  {
    bfs::path dstPath = directoryB / make_relative(directoryA, entry.path());
    bfs::copy_file(entry.path(), dstPath);
  }
});

The make_relative function would iterate through the path components of the two arguments, removing entries until it found different ones. Then, it would add ".." to the beginning of the second path argument, until it ran out of directories in the first path. What you are left with is the second path relative to the first one.

Obviously, this would not work if both paths are absolute. But this is such a common operation that there has to be some way to make it work.

Attachments

Change History

comment:1 Changed 2 years ago by anonymous

Great idea! I was looking for that exact function just today.

comment:2 Changed 2 years ago by anonymous

I've added an example for 1.46.1 ( probably works for trunk as well, haven't tried ). For details take a look at  https://svn.boost.org/trac/boost/ticket/6249 .

comment:3 Changed 9 months ago by anonymous

Please consider this feature, it is required rather often -  http://stackoverflow.com/questions/10167382/boostfilesystem-get-relative-path

comment:4 Changed 9 months ago by anonymous

This would be really helpful, I'd consider it one of the basic functions you want to do with paths.

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as new
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.