2011-08-14 14 views
1

は考える:oldTimenowの間の期間に作成されたファイルディレクトリ内の新しいファイルをチェックする方法は?

  • filesystem::path toDir("./");
  • ptime oldTime;
  • ptime now(second_clock::local_time());

どうすれば確認できますか?

このような「新鮮な」ファイルの名前は、coutにストリーミングする必要があります。更新

:(それを修正する方法

=

#include <sstream> 
#include <stdio.h> 
#include <time.h> 
#include <boost/filesystem.hpp> 
#include <boost/thread.hpp> 
#include <boost/timer.hpp> 
#include <boost/date_time.hpp> 
#include <boost/date_time/posix_time/posix_time.hpp> 
#include <boost/date_time/posix_time/posix_time_io.hpp> 

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<string>(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 << "File: " << a << 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 >= oldTime && lastAccessTime <= nowTime) 
    { 
     std::string a = file_service_get_dif_path(file_service_default_path, p); 
     std::cout << "File: " << a << " is new to us." << std::endl; 
    } 
} 

void is_dir(path dir) 
{ 
    boost::filesystem::directory_iterator dirIter(dir), dirIterEnd; 
    while (dirIter != dirIterEnd) 
    { 
     if (boost::filesystem::exists(*dirIter) && !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 << spendedTimeFame << std::endl; 
     if (spendedTimeFame < 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(); 
} 

しかし、すべての新しいファイルを表示しないようだ? まあ与えられた回答に基づいて私は小さなプログラムの開発を行いました更新2:

解決済みnowTime = second_clock::universal_time();

アップデート3: 固定コード:エミール・コーミアが述べたように

#include <sstream> 
#include <stdio.h> 
#include <time.h> 
#include <boost/filesystem.hpp> 
#include <boost/thread.hpp> 
#include <boost/timer.hpp> 
#include <boost/date_time.hpp> 
#include <boost/date_time/posix_time/posix_time.hpp> 
#include <boost/date_time/posix_time/posix_time_io.hpp> 

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<string>(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 << "File: " << a << 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 >= oldTime && lastAccessTime <= nowTime) 
    { 
     std::string a = file_service_get_dif_path(file_service_default_path, p); 
     std::cout << "File: " << a << " is new to us." << 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) && !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 << spendedTimeFame << std::endl; 
     if (spendedTimeFame < 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(); 
} 
+1

。ファイルの変更と最終アクセス時刻のみ。 –

+0

関連:http://stackoverflow.com/questions/4279164/cboost-file-system-to-return-a-list-of-files-older-than-a-specific-time –

答えて

4

、あなたはUnixの内のファイルの作成時間を取得することができません。しかし、最後のアクセス時間を調べることで、 '新鮮な'の定義に応じて、まだ新鮮なファイルを探すことができます。あなたは、あなたの質問に状態として「./」つまり、あなたはその後、boost::filesystem::directory_iteratorを見れば

boost::filesystem::path p("somefile"); 
std::time_t t = boost::filesystem::last_write_time(p); 
boost::ptime lastAccessTime = boost::ptime::from_time_t(t); 

は、だから、ディレクトリを反復処理することができ、かつで与えられたlastAccessTimeを比較します。これは、次のようにコールすることによって達成されますあなたの質問で指定された範囲で上記の例をご覧ください。

次のようにだから私はこれを行うような方法は、oldTimeは、以前に定義されていると仮定すると、次のとおりです。

boost::filesystem::path dir("./"); 
boost::ptime nowTime(boost::second_clock::local_time()); 

boost::filesystem::directory_iterator dirIter(dir), dirIterEnd; 
while (dirIter != dirIterEnd) 
{ 
    if (boost::filesystem::exists(*dirIter) && !boost::filesystem::is_directory(*dirIter)) 
    { 
     std::time_t t = boost::filesystem::last_write_time(*dirIter); 
     boost::ptime lastAccessTime = boost::ptime::from_time_t(t); 
     if (lastAccessTime >= oldTime && lastAccessTime <= nowTime) 
     { 
      std::cout << "File meets criteria: " << (*dirIter).filename() << std::endl; 
     }  
    } 
    ++dirIter; 
} 

は、この情報がお役に立てば幸い!

最後にチェックしたディレクトリおよび/または最後にチェックしたときにディレクトリに存在するファイルの最後のリストを永続的に保存し、これを代わりに比較のポイントとして使用することで、これを改良できます。 の意図に関する詳細情報がない場合私はそれ以上のガイダンスを提供することはできません。

関連ドキュメント: boost::filesystem Reference、Unixのは、どこでもファイルの作成時刻が格納されていないboost::ptime Reference

+0

私が作成した小さなプログラムをご覧くださいあなたのコードに。何が間違っていますか?なぜそれがうまくいかないのか(私はこれまでWindowsでしか試していなかった...) – Rella

関連する問題