2011-08-14 21 views
1

考慮:如何檢查目錄中的新文件?

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

我怎麼能確定oldTimenow之間的時間段創建哪些文件?

這些「新鮮」文件的名稱應該流式傳輸到cout

更新: 那麼根據給定的答案,我做了一個小PROGRAMM:

#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

Unix的不存儲文件的創建時間,任何地點。只有文件修改和上次訪問時間。 –

+0

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

回答

4

由於埃米爾·科米爾說,你將無法獲得在Unix上文件的創建時間。但是,通過查看上次訪問時間,您仍然可以查找「新鮮」文件,具體取決於您對「新鮮」的定義。這是通過調用如下實現:

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

所以,如果你再看看boost::filesystem::directory_iterator你可以遍歷目錄,即「./」當你在你的問題的狀態,比較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 Referenceboost::ptime Reference

+0

請參閱我製作的小程序在你的代碼上。我做錯了什麼?爲什麼它不起作用(我只在Windows上嘗試過......) – Rella

相關問題