Как проверить наличие новых файлов в каталоге? - PullRequest
2 голосов
/ 14 августа 2011

Дано:

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

Как определить, какие файлыбыли созданы в период времени между oldTime и now?

Имена таких "свежих" файлов должны быть переданы в 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 Ответ

4 голосов
/ 14 августа 2011

Как заявил Эмиль Кормиер, вы не сможете узнать время создания файла в 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 :: Fileystem Reference , boost :: ptime Ссылка

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...