Szymon Gatner
Szymon Gatner

Reputation: 47

How to convert boost::filesystem::path with ".." (go up) components to proper path

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?

Upvotes: 2

Views: 1579

Answers (3)

Tomer L
Tomer L

Reputation: 21

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.

Upvotes: 1

Arne Mertz
Arne Mertz

Reputation: 24576

Short question, short answer:

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

Upvotes: 1

dwxw
dwxw

Reputation: 1099

Check out canonical from the Boost filesystem library.

Upvotes: 2

Related Questions