Filesystem iterator suggestion

I have used the filesystem library for some time now and noticed that there is no portable way of iterating over files in a directory that matches a pattern. In my applications I often need to find all files in a directory that matches "*.txt" or "input.*". I think it would be a good idea to add another constructor to the directory_iterator that support some primitive wilcards. The support could be limited to "*" at the end or followed by a "." (e.g. "*", "a*.*", "*.txt", "a.*") for (path::directory_iterator itr = directory_iterator(path, "*.txt"); itr != directory_iterator(); ++itr) ... I assume this limited wildcard support could easily be ported to most operating systems. ------------------- Another small improvement that would make the directory_iterator easier to use is to somehow allow it to only list directories or files. In the current implementation you need to create a temporary to use an algorithm on all files in a directory (or use some lambda if construct). std::vector<path> tmp; std::remove_copy_if(directory_iterator(dir), directory_iterator(), back_inserter(tmp), std::ptr_fun(is_directory)) std::for_each(tmp.begin(), tmp.end(), func); instead of just std::for_each(directory_iterator(dir, files_only), directory_iterator(), func);

Martin wrote:
I have used the filesystem library for some time now and noticed that there is no portable way of iterating over files in a directory that matches a pattern.
In my applications I often need to find all files in a directory that matches "*.txt" or "input.*".
I think it would be a good idea to add another constructor to the directory_iterator that support some primitive wilcards. The support could be limited to "*" at the end or followed by a "." (e.g. "*", "a*.*", "*.txt", "a.*")
Well, supposing that one has a 'shell wildcard matcher' it should be possible to do: typedef filter_iterator<wildcard_matcher, directory_iterator> it; for(it i = it(wildcard_matcher("*.txt), directory_iterator("dir")), e; i != e; ++i) { } which is not altogether nice, but workable. So, we only need somebody write the wildcard_matcher, or better yet, function to convert from wildcard syntax into regex syntax. Actually, I'd love to have some better syntax. E.g. with views it would be possible to write: for_each(filter(files("dir"), wildcard_matcher("*.txt")), /* lambda */ ); which is good except for the need to use lambda -- which might not be good in all cases, so someone should push 'foreach' macro Or maybe, filter_iterator could forward arguments to constructors, like: for(it i = it("*.txt", "dir), e; i != e; ++i) But without a way to separate arguments to functor and to underlying iterator this will only work for a case of one argument for each. Well, I guess I should stop dreaming and get to work. - Volodya

Martin wrote:
I have used the filesystem library for some time now and noticed that there is no portable way of iterating over files in a directory that matches a pattern.
In my applications I often need to find all files in a directory that matches "*.txt" or "input.*".
There was a proposal (and some code) for a globbing iterator some time ago. http://aspn.activestate.com/ASPN/Mail/Message/1977422 The OP uploaded his proposal to Yahoo files. It's named "globiter.zip". HTH, Angus

On Monday, April 26, 2004, at 05:42 AM, Angus Leeming wrote:
Martin wrote:
I have used the filesystem library for some time now and noticed that there is no portable way of iterating over files in a directory that matches a pattern.
In my applications I often need to find all files in a directory that matches "*.txt" or "input.*".
There was a proposal (and some code) for a globbing iterator some time ago.
http://aspn.activestate.com/ASPN/Mail/Message/1977422
The OP uploaded his proposal to Yahoo files. It's named "globiter.zip".
HTH, Angus
I hope it helps as well. If you have any comments or suggestions, I'd love to hear them. [I've been MIA for a bit and am just getting caught up.] --rich

Thanks for your replies but maybe I wasn't clear about the problem. The problem is that only the filsystem knows if the file "a.TXT" matches the pattern "*.txt". There is no way for any kind of filter to find this out without using some non- portable native filesystem functions.
participants (4)
-
Angus Leeming
-
Martin
-
Rich Johnson
-
Vladimir Prus