Using filesystem::remove with wildcards

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.

On 01/20/2012 05:13 AM, toran wrote:
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... 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#r... Norbert
participants (2)
-
Norbert Wenzel
-
toran