Question

How to convert a boost::filesystem::path in a form of:

root/subdir1/subdir2/../some.file

to:

root/subdir1/some.file

with possibly multiple "go level up" operators?

Was it helpful?

Solution

Check out canonical from the Boost filesystem library.

OTHER TIPS

Short question, short answer:

By subsequently simply erasing every /<dirname>/.. occurence from the path. You could easily use regex for that.

Why not use the branch_path() ? It returns the parent directory of a boost::filesystem::path

Example:

 boost::filesystem::path path("root/subdir1/subdir2/some.file");
 boost::filesystem::path parent = path.branch_path().branch_path(); // "root/subdir1"
 boost::filesystem::copy(path, parent);
 boost::filesystem::remove(path.branch_path());

You can use it whatever you like.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top