[Boost-users] [boost::filesystem::path] removing components of a path

6 views
Skip to first unread message

Sean Farrow via Boost-users

unread,
Mar 7, 2019, 2:49:31 PM3/7/19
to boost...@lists.boost.org, Sean Farrow

Hi all,

 

I’m revisiting boost::filesystem::path after not using it for a while and wondered whether there is a way to remove parts of a path deeper than a specific directory?

 

By way of an example, assume I have the following path declared:

boost::filesystem::path path =”d:\\test1\\test2\\test3.txt”

 

what I want to do is determine whether test2 exists as part of the path and if it does remove everything after and including the found part of the path, so in this case \\test2\\test3.txt would be removed.

 

I could do it by converting the path to a string, but wondered whether boost::filesystem could do this natively in some way.

 

Any help appreciated.

 

Kind regards

Sean.

Michael Powell via Boost-users

unread,
Mar 7, 2019, 2:53:52 PM3/7/19
to boost...@lists.boost.org, Michael Powell

Natively, or via the FS API?

I do not have the API right in front of me, but couldn't you peel off
the children nodes to their parents until you found whether the
current node was the match?

I'm not positive, but I am pretty sure FS knows about relative v.
absolute paths, in this regard, as well.

> Any help appreciated.
>
> Kind regards
>
> Sean.
>

> _______________________________________________
> Boost-users mailing list
> Boost...@lists.boost.org
> https://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
https://lists.boost.org/mailman/listinfo.cgi/boost-users

Sean Farrow via Boost-users

unread,
Mar 7, 2019, 2:57:28 PM3/7/19
to boost...@lists.boost.org, Sean Farrow


-----Original Message-----
From: Boost-users <boost-use...@lists.boost.org> On Behalf Of Michael Powell via Boost-users
Sent: 07 March 2019 19:53
To: boost...@lists.boost.org
Cc: Michael Powell <mwpow...@gmail.com>
Subject: Re: [Boost-users] [boost::filesystem::path] removing components of a path

On Thu, Mar 7, 2019 at 2:49 PM Sean Farrow via Boost-users <boost...@lists.boost.org> wrote:
>
> Hi all,
>
>
>
> I’m revisiting boost::filesystem::path after not using it for a while and wondered whether there is a way to remove parts of a path deeper than a specific directory?
>
> By way of an example, assume I have the following path declared:
>
> boost::filesystem::path path =”d:\\test1\\test2\\test3.txt”
>
> what I want to do is determine whether test2 exists as part of the path and if it does remove everything after and including the found part of the path, so in this case \\test2\\test3.txt would be removed.
>
> I could do it by converting the path to a string, but wondered whether boost::filesystem could do this natively in some way.

Natively, or via the FS API?
Via the fs API, this needs to be cross-platform.

I do not have the API right in front of me, but couldn't you peel off the children nodes to their parents until you found whether the current node was the match?
I could iterate the path, but wondered whether path has a method to ask whether part of a path exists?

Michael Powell via Boost-users

unread,
Mar 7, 2019, 3:05:39 PM3/7/19
to boost...@lists.boost.org, Michael Powell
On Thu, Mar 7, 2019 at 2:57 PM Sean Farrow via Boost-users

<boost...@lists.boost.org> wrote:
>
>
>
> -----Original Message-----
> From: Boost-users <boost-use...@lists.boost.org> On Behalf Of Michael Powell via Boost-users
> Sent: 07 March 2019 19:53
> To: boost...@lists.boost.org
> Cc: Michael Powell <mwpow...@gmail.com>
> Subject: Re: [Boost-users] [boost::filesystem::path] removing components of a path
>
> On Thu, Mar 7, 2019 at 2:49 PM Sean Farrow via Boost-users <boost...@lists.boost.org> wrote:
> >
> > Hi all,
> >
> >
> >
> > I’m revisiting boost::filesystem::path after not using it for a while and wondered whether there is a way to remove parts of a path deeper than a specific directory?
> >
> > By way of an example, assume I have the following path declared:
> >
> > boost::filesystem::path path =”d:\\test1\\test2\\test3.txt”
> >
> > what I want to do is determine whether test2 exists as part of the path and if it does remove everything after and including the found part of the path, so in this case \\test2\\test3.txt would be removed.
> >
> > I could do it by converting the path to a string, but wondered whether boost::filesystem could do this natively in some way.
>
> Natively, or via the FS API?
> Via the fs API, this needs to be cross-platform.

Well, I asked, because you said natively. I thought that was the
point, FS exposes it generically.

> I do not have the API right in front of me, but couldn't you peel off the children nodes to their parents until you found whether the current node was the match?
> I could iterate the path, but wondered whether path has a method to ask whether part of a path exists?

You want it to be simpler than the FS API? Again, I do not have the
docs right in front of me. I would start there.

degski via Boost-users

unread,
Mar 8, 2019, 1:50:21 AM3/8/19
to boost...@lists.boost.org, degski
Doing this is no problem whatsoever, you'll have to use the functionality of the path-class. It has all the primitives to code this [and any other kind of manipulation of paths] your-self, coz that's what you'll have to do. See https://en.cppreference.com/w/cpp/filesystem/path/parent_path, f.e. but there might be a few other bits and bobs that are equally or more suited for coding the solution to your specific problem.

degski
--
"Big boys don't cry" - Eric Stewart, Graham Gouldman
Reply all
Reply to author
Forward
0 new messages