questions about recursing a directory tree

I am just learning C++ and for a first project I would like to write a program which goes recursively through a directory tree and returns the newest and the oldest file date. I have been following the documentation, but I couldn't really find a working example for a recursive algorithm. Shell I start by using this code from the v2 documentation? ---------------------------------- bool find_file( const path & dir_path, // in this directory, const std::string & file_name, // search for this name, path & path_found ) // placing path here if found { if ( !exists( dir_path ) ) return false; directory_iterator end_itr; // default construction yields past-the-end for ( directory_iterator itr( dir_path ); itr != end_itr; ++itr ) { if ( is_directory(itr->status()) ) { if ( find_file( itr->path(), file_name, path_found ) ) return true; } else if ( itr->path().filename() == file_name ) // see below { path_found = itr->path(); return true; } } return false; } ------------------------------- The other way I figured out is with recursive_directory_iterator: ------------------------------ #include "boost/filesystem.hpp" #include <iostream> namespace fs = boost::filesystem; fs::recursive_directory_iterator end; fs::recursive_directory_iterator dir("e:\\docs"); void main() { for ( ; dir != end; ++dir ) { std::cout << *dir << std::endl; } } ------------------------------- Is it safe to use recursive_directory_iterator on a really big directory tree? I mean is it going to copy the whole tree into memory? Does it walk through the whole directory tree the moment it's called, or it only accesses the disk on a ++ operation one-by-one? I still haven't looked into how to compare two files' date, but could you give a hint about which function shell I use? Is it possible to use just a simple < or > operation on file dates, or I need to write manually if year1 > year2 and if month1 > month2, etc? Thanks in advance, Zsolt

OK, I figured out my way, it seems that I could solve most of the
problems. My big problem now is that the recursive_directory_iterator
goes through a file which has a really long name. I think it's more
than 256 characters. It says:
boost::filesystem::last_write_time: The system cannot find the path specified:
Is there a fix or something for boost to handle more than 256
characters in windows path? I am using MSVC2010 + latest Boostpro
installer.
Zsolt
p.s. so far here is my code. If you have any idea about what I am
doing wrong, please don't hesitate to comment:
#define BOOST_FILESYSTEM_VERSION 3
#include "boost/filesystem.hpp"
#include <iostream>
#include <ctime>
using namespace std;
namespace fs = boost::filesystem;
int main() {
fs::recursive_directory_iterator it_end;
fs::recursive_directory_iterator it_dir("e:\\");
fs::path p;
time_t oldest( time(NULL) );
time_t newest(0);
try {
for ( ; it_dir != it_end; ++it_dir ) {
p = *it_dir;
try {
time_t t( last_write_time(p) );
if (t

OK, this is the easiest example which produces the "access is denied"
error, I can come up with:
int main() {
fs::recursive_directory_iterator it_end;
fs::recursive_directory_iterator it_dir("e:\\");
for ( ; it_dir != it_end; ++it_dir ) {
//something here
}
}
Can someone tell me any solution which would catch a "access is denied"
kind of error _inside_ the for loop? For every other error it works if
I just do a try-catch _inside_ the for loop, but a not accessible
directory produces error outside the loop?
Or if it's not possible to catch the error inside, how could I start
the for cycle again after it has been caught?
Zsolt
On Tue, Nov 23, 2010 at 12:48 PM, Zsolt Ero
OK, I figured out my way, it seems that I could solve most of the problems. My big problem now is that the recursive_directory_iterator goes through a file which has a really long name. I think it's more than 256 characters. It says:
boost::filesystem::last_write_time: The system cannot find the path specified:
Is there a fix or something for boost to handle more than 256 characters in windows path? I am using MSVC2010 + latest Boostpro installer.
Zsolt
p.s. so far here is my code. If you have any idea about what I am doing wrong, please don't hesitate to comment:
#define BOOST_FILESYSTEM_VERSION 3
#include "boost/filesystem.hpp" #include <iostream> #include <ctime>
using namespace std; namespace fs = boost::filesystem;
int main() { fs::recursive_directory_iterator it_end; fs::recursive_directory_iterator it_dir("e:\\"); fs::path p;
time_t oldest( time(NULL) ); time_t newest(0);
try { for ( ; it_dir != it_end; ++it_dir ) { p = *it_dir; try { time_t t( last_write_time(p) ); if (t
newest) newest=t; //if (fs::is_directory(p)) cout << (p) << " " << t << endl; } catch (const fs::filesystem_error& ex) { cout << "\n" << ex.what() << "\n"; } } }
catch (const fs::filesystem_error& ex) { cout << "\n" << ex.what() << "\n"; }
cout << "\nOldest: " << ctime(&oldest); cout << "Newest: " << ctime(&newest) << endl;
return 0; }
participants (1)
-
Zsolt Ero