Discussion:
Using filesystem::remove with wildcards
toran
2012-01-20 04:13:56 UTC
Permalink
I am having trouble deleting files with boost::filesystem. Following code does
not work for me (and I can't find the doc that explains how to do it).

boost::filesystem::path pt("somepath");
pt /= ("*.data");
boost::filesystem::remove(pt);

pt variable points to the valid set of files, but removal does not happened.

I will appreciate an advise and may be pointing me to the right place in the
docs. Thanks.
Norbert Wenzel
2012-01-20 09:40:31 UTC
Permalink
Post by toran
I am having trouble deleting files with boost::filesystem. Following code does
not work for me (and I can't find the doc that explains how to do it).
boost::filesystem::path pt("somepath");
pt /= ("*.data");
boost::filesystem::remove(pt);
pt variable points to the valid set of files, but removal does not happened.
I will appreciate an advise and may be pointing me to the right place in the
docs. Thanks.
As far as I know there is no such thing as wildcard replacement in
filesystem. So unless your file is not actually named "*.data" it won't
be found.

If you're trying to delete every .data file in a directory, you'd need
to use the (recursive_)directory_iterator and the .extension() member
function of the path object. And be aware that the iterator is
invalidated when you remove a file.

See this similar question at Stackoverflow:
http://stackoverflow.com/questions/1257721/can-i-use-a-mask-to-iterate-files-in-a-directory-with-boost

And be aware that filesystem::remove() returns a bool indicating if the
file existed, so it'd be easy to check in your code. See
http://www.boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/reference.html#remove

Norbert

Loading...