title: How to check for new files in the directory?
body: <p>Given:</p>
  
  <ul>
  <li><code>filesystem::path toDir("./");</code></li>
  <li><code>ptime oldTime;</code></li>
  <li><code>ptime now(second_clock::local_time());</code></li>
  </ul>
  
  <p>How can I determine which files were created in the time period between <code>oldTime</code> and <code>now</code>?</p>
  
  <p>The names of such "fresh" files should be streamed to <code>cout</code>.</p>
  
  <p><em><strong>Update:</em></strong>
  Well based on given answer I made a small programm:</p>
  
  <pre><code>#include &lt;sstream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  #include &lt;boost/filesystem.hpp&gt;
  #include &lt;boost/thread.hpp&gt;
  #include &lt;boost/timer.hpp&gt;
  #include &lt;boost/date_time.hpp&gt;
  #include &lt;boost/date_time/posix_time/posix_time.hpp&gt;
  #include &lt;boost/date_time/posix_time/posix_time_io.hpp&gt;
  
  using namespace boost::filesystem;
  using namespace boost::posix_time;
  using namespace boost::local_time;
  using namespace boost::gregorian;
  using namespace std;
  
  path file_service_default_path;
  
  boost::timer timerFame;
  int64_t desiredTimeFame;
  int64_t spendedTimeFame;
  ptime oldTime;
  ptime nowTime;
  bool first_time;
  
  string file_service_get_dif_path(path base_path, path new_path)
  {
      path sdiffpath;
      path stmppath = new_path;
      while(stmppath != base_path) {
          sdiffpath = path(stmppath.stem().string() + stmppath.extension().string())/ sdiffpath;
          stmppath = stmppath.parent_path();
      }
      string diff_path =sdiffpath.string();// boost::lexical_cast&lt;string&gt;(sdiffpath);
      diff_path = diff_path.substr(0, (diff_path.length()));
      std::replace(diff_path.begin(), diff_path.end(), '\\', '/');
      return diff_path;
  }
  
  void is_file(path p)
  {
      std::string a =  file_service_get_dif_path(file_service_default_path, p);
      std::cout &lt;&lt; "File: " &lt;&lt;  a &lt;&lt; std::endl;
  }
  
  void is_new_file(path p)
  {
      std::time_t t = boost::filesystem::last_write_time( p );
      ptime lastAccessTime = from_time_t( t );
      if ( lastAccessTime &gt;= oldTime &amp;&amp; lastAccessTime &lt;= nowTime )
      {
          std::string a =  file_service_get_dif_path(file_service_default_path, p);
          std::cout &lt;&lt; "File: " &lt;&lt;  a &lt;&lt; " is new to us." &lt;&lt; std::endl;
      }   
  }
  
  void is_dir(path dir)
  {
      boost::filesystem::directory_iterator dirIter( dir ), dirIterEnd;
      while ( dirIter != dirIterEnd )
      {
          if ( boost::filesystem::exists( *dirIter ) &amp;&amp; !boost::filesystem::is_directory( *dirIter ) )
          {
              if (first_time)
              {
                  is_file((*dirIter));
              }
              else
              {
                  is_new_file((*dirIter));
              }
          }
          else
          {
              is_dir((*dirIter));
          }
          ++dirIter;
      }
  }
  
  void files_walker()
  {
      while(true)
      {
          timerFame.restart();
          oldTime = nowTime;
          nowTime = second_clock::local_time() ;
          file_service_default_path = file_service_default_path;
          is_dir(file_service_default_path);
          first_time = false;
          spendedTimeFame = (int64_t)timerFame.elapsed();
          cout &lt;&lt; spendedTimeFame &lt;&lt; std::endl;
          if (spendedTimeFame &lt; desiredTimeFame)
              boost::this_thread::sleep(boost::posix_time::milliseconds(desiredTimeFame - spendedTimeFame));
      }
  }
  
  int main()
  {
      desiredTimeFame = (int64_t)(5000.0f);
      first_time = true;
      file_service_default_path = "./new";
      boost::thread workerThread(files_walker);
      cin.get();
  }
  </code></pre>
  
  <p>But it seems not to show any new files=( how to fix it?</p>
  
  <p><em><strong>Update 2:</em></strong></p>
  
  <p>Solved with <code>nowTime = second_clock::universal_time();</code> </p>
  
  <p><em><strong>Update 3:</em></strong>
  fixed code:</p>
  
  <pre><code>#include &lt;sstream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  #include &lt;boost/filesystem.hpp&gt;
  #include &lt;boost/thread.hpp&gt;
  #include &lt;boost/timer.hpp&gt;
  #include &lt;boost/date_time.hpp&gt;
  #include &lt;boost/date_time/posix_time/posix_time.hpp&gt;
  #include &lt;boost/date_time/posix_time/posix_time_io.hpp&gt;
  
  using namespace boost::filesystem;
  using namespace boost::posix_time;
  using namespace boost::local_time;
  using namespace boost::gregorian;
  using namespace std;
  
  path file_service_default_path;
  
  boost::timer timerFame;
  int64_t desiredTimeFame;
  int64_t spendedTimeFame;
  ptime oldTime;
  ptime nowTime;
  bool first_time;
  
  string file_service_get_dif_path(path base_path, path new_path)
  {
      path sdiffpath;
      path stmppath = new_path;
      while(stmppath != base_path) {
          sdiffpath = path(stmppath.stem().string() + stmppath.extension().string())/ sdiffpath;
          stmppath = stmppath.parent_path();
      }
      string diff_path =sdiffpath.string();// boost::lexical_cast&lt;string&gt;(sdiffpath);
      diff_path = diff_path.substr(0, (diff_path.length()));
      std::replace(diff_path.begin(), diff_path.end(), '\\', '/');
      return diff_path;
  }
  
  void is_file(path p)
  {
      std::string a =  file_service_get_dif_path(file_service_default_path, p);
      std::cout &lt;&lt; "File: " &lt;&lt;  a &lt;&lt; std::endl;
  }
  
  void is_new_file(path p)
  {
      std::time_t t = boost::filesystem::last_write_time( p );
      ptime lastAccessTime = from_time_t( t );
      if ( lastAccessTime &gt;= oldTime &amp;&amp; lastAccessTime &lt;= nowTime )
      {
          std::string a =  file_service_get_dif_path(file_service_default_path, p);
          std::cout &lt;&lt; "File: " &lt;&lt;  a &lt;&lt; " is new to us." &lt;&lt; std::endl;
      }   
  }
  
  void is_dir(path dir)
  {
      if(!exists(dir))
      {
          return;
      }
      boost::filesystem::directory_iterator dirIter( dir );
      boost::filesystem::directory_iterator dirIterEnd;
      while ( dirIter != dirIterEnd )
      {
          if ( boost::filesystem::exists( *dirIter ) &amp;&amp; !boost::filesystem::is_directory( *dirIter ) )
          {
              if (first_time)
              {
                  is_file((*dirIter));
              }
              else
              {
                  is_new_file((*dirIter));
              }
          }
          else
          {
              is_dir((*dirIter));
          }
          ++dirIter;
      }
  }
  
  void files_walker()
  {
      while(true)
      {
          timerFame.restart();
          oldTime = nowTime;
          nowTime = second_clock::universal_time();
          is_dir(file_service_default_path);
          first_time = false;
          spendedTimeFame = (int64_t)timerFame.elapsed();
          cout &lt;&lt; spendedTimeFame &lt;&lt; std::endl;
          if (spendedTimeFame &lt; desiredTimeFame)
              boost::this_thread::sleep(boost::posix_time::milliseconds(desiredTimeFame - spendedTimeFame));
      }
  }
  
  int main()
  {
      desiredTimeFame = (int64_t)(5000.0f);
      first_time = true;
      file_service_default_path = "./new";
      boost::thread workerThread(files_walker);
      cin.get();
  }
  </code></pre>