[Boost.FileSystem] File or Wildcards in Path? How-to do this?

Here's a simple example. Is there a way to do achieve this effect? std::string folderName = "C:\\*.sys"; boost::filesystem::path fsDirectoryPath = boost::filesystem::system_complete( boost::filesystem::path(folderName, boost::filesystem::native)); boost::filesystem::directory_iterator end_iter; for (boost::filesystem::directory_iterator dir_itr(fsDirectoryPath); dir_itr != end_iter; ++dir_itr) // throws exception { std::cout << "Contains the following file: " << dir_itr->string() << std::endl; } // end for When I do this, I get the following exception: boost::filesystem::basic_directory_iterator constructor I don't see any documentation for how to narrow your searches with wildcards or how to only find files that match a particular file name. Am I missing something? Obviously you can refine your search later in the loop, but that is inefficient. Please let me know. Thanks, Lawrence

Lawrence Spector wrote:
std::string folderName = "C:\\*.sys"; boost::filesystem::path fsDirectoryPath = boost::filesystem::system_complete( boost::filesystem::path(folderName, boost::filesystem::native)); boost::filesystem::directory_iterator end_iter;
for (boost::filesystem::directory_iterator dir_itr(fsDirectoryPath); dir_itr != end_iter; ++dir_itr) // throws exception { std::cout << "Contains the following file: " << dir_itr->string() << std::endl; } // end for
When I do this, I get the following exception:
boost::filesystem::basic_directory_iterator constructor
Are you using Boost 1.34? If not, it might be worth trying. If I'm right about the exception you're getting, I believe it's been eliminated in 1.34. If you're locked into a previous version of Boost, try passing boost::filesystem::no_check instead of boost::filesystem::native.

We are using Boost 1.34.0. This is something in the current version of Boost, which seems to only allow Directory paths to be used in a boost::filesystem::directory_iterator. So, the question I present would be whether or not there's another construct for doing this? My coworker found something in boost/regex/v4/fileiter.hpp called file_iterator, which seemed to achieve the correct result, but why isn't this part of Boost.Filesystem? Or is it and I'm just missing something? Thanks, Lawrence -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Nat Goodspeed Sent: Tuesday, October 02, 2007 4:16 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [Boost.FileSystem] File or Wildcards in Path? How-to do this? Lawrence Spector wrote:
std::string folderName = "C:\\*.sys"; boost::filesystem::path fsDirectoryPath = boost::filesystem::system_complete( boost::filesystem::path(folderName, boost::filesystem::native)); boost::filesystem::directory_iterator end_iter;
for (boost::filesystem::directory_iterator dir_itr(fsDirectoryPath); dir_itr != end_iter; ++dir_itr) // throws exception { std::cout << "Contains the following file: " << dir_itr->string() << std::endl; } // end for
When I do this, I get the following exception:
boost::filesystem::basic_directory_iterator constructor
Are you using Boost 1.34? If not, it might be worth trying. If I'm right about the exception you're getting, I believe it's been eliminated in 1.34. If you're locked into a previous version of Boost, try passing boost::filesystem::no_check instead of boost::filesystem::native. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Lawrence Spector wrote:
We are using Boost 1.34.0. This is something in the current version of Boost, which seems to only allow Directory paths to be used in a boost::filesystem::directory_iterator. So, the question I present would be whether or not there's another construct for doing this? My coworker found something in boost/regex/v4/fileiter.hpp called file_iterator, which seemed to achieve the correct result, but why isn't this part of Boost.Filesystem? Or is it and I'm just missing something?
Ah. I mistook the problem you were having, sorry. So to try to clarify: the problem you're actually experiencing is that you get an exception when you pass a wildcard string to directory_iterator? While I can sympathize with your request, I guess I find it defensible that wildcard matching would need the regex library.

Correct. That is the issue. It's not just wildcards, it also seems that directory_iterator won't handle a single file either (e.g.: C:\config.sys). The file_iterator in regex just seems like it's in the wrong place. I also think this is common functionality that one can't reasonably be expected to know exists without deep digging (my coworker found it by suspecting that it would likely call FindFirstFileA in its implementation and searching on that). Also, the file_iterator in regex doesn't seem to precisely follow the directory_iterator's style. For example, you can't pass a boost::filesystem::path object to the file_iterator. Another issue is that there's no wide version of the file_iterator. I do think this needs some cleaning up, if for no other reason than it seems inconsistent. Thanks, Lawrence -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Nat Goodspeed Sent: Wednesday, October 03, 2007 10:52 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [Boost.FileSystem] File or Wildcards in Path? How-to do this? Lawrence Spector wrote:
We are using Boost 1.34.0. This is something in the current version of Boost, which seems to only allow Directory paths to be used in a boost::filesystem::directory_iterator. So, the question I present would be whether or not there's another construct for doing this? My coworker found something in boost/regex/v4/fileiter.hpp called file_iterator, which seemed to achieve the correct result, but why isn't this part of Boost.Filesystem? Or is it and I'm just missing something?
Ah. I mistook the problem you were having, sorry. So to try to clarify: the problem you're actually experiencing is that you get an exception when you pass a wildcard string to directory_iterator? While I can sympathize with your request, I guess I find it defensible that wildcard matching would need the regex library. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Lawrence Spector wrote:
Correct. That is the issue. It's not just wildcards, it also seems that directory_iterator won't handle a single file either (e.g.: C:\config.sys). The file_iterator in regex just seems like it's in the wrong place. I also think this is common functionality that one can't reasonably be expected to know exists without deep digging (my coworker found it by suspecting that it would likely call FindFirstFileA in its implementation and searching on that).
Also, the file_iterator in regex doesn't seem to precisely follow the directory_iterator's style. For example, you can't pass a boost::filesystem::path object to the file_iterator. Another issue is that there's no wide version of the file_iterator.
I do think this needs some cleaning up, if for no other reason than it seems inconsistent.
Well it's a common enough task that we really should do something about it: the file-iterators in Boost.Regex are old pre-boost code used in a now-deprecated part of the regex lib. I guess what we really need is a new library submission built on top of Boost.Filesystem. Any takers? John.

That would definitely be very good if it could be added for the next major version of Boost. It would be a relatively simple addition. The file_iterator from Regex/v4 would need a few minor modifications to match the style of directory_iterator from Boost.FileSystem. Or, in all honesty, the directory_iterator from Boost.Filesystem is very close, except that the code that takes action based on the assumption that it's a directory path adds '/*' to the end. If the directory_iterator was configurable or there was a version (perhaps called file_iterator) that didn't append '/*' to the end of the directory lookup, then it would serve this purpose. Thanks, Lawrence -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of John Maddock Sent: Thursday, October 04, 2007 4:53 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [Boost.FileSystem] File or Wildcards in Path? How-to do this? Lawrence Spector wrote:
Correct. That is the issue. It's not just wildcards, it also seems that directory_iterator won't handle a single file either (e.g.: C:\config.sys). The file_iterator in regex just seems like it's in the wrong place. I also think this is common functionality that one can't reasonably be expected to know exists without deep digging (my coworker found it by suspecting that it would likely call FindFirstFileA in its implementation and searching on that).
Also, the file_iterator in regex doesn't seem to precisely follow the directory_iterator's style. For example, you can't pass a boost::filesystem::path object to the file_iterator. Another issue is that there's no wide version of the file_iterator.
I do think this needs some cleaning up, if for no other reason than it seems inconsistent.
Well it's a common enough task that we really should do something about it: the file-iterators in Boost.Regex are old pre-boost code used in a now-deprecated part of the regex lib. I guess what we really need is a new library submission built on top of Boost.Filesystem. Any takers? John. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
John Maddock
-
Lawrence Spector
-
Nat Goodspeed