c++ - 如何检查目录中是否有新文件?

标签 c++ file boost time boost-filesystem

给定:

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

如何确定哪些文件是在 oldTimenow 之间的时间段内创建的?

此类“新鲜”文件的名称应传输至 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 = secondary_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();
}

最佳答案

正如 Emile Cormier 所说,您将无法在 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 Reference , boost::ptime Reference

关于c++ - 如何检查目录中是否有新文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7056882/

相关文章:

c++ - 被未定义的 C++ 移位运算符行为和包装混淆 "pattern space"

c++ - 使用 Boost::Fiber 的多个共享工作池

c++ - 从 C++ 中的成员函数返回指针

c++ - 尝试在 cpp 中打印 n 维数组

c++ - 未实现的基类函数是否应该为 0?

php - 带有 enctype 的 Tomcat 6 和 PHP - 没有返回输入数据

android - 从线程将bmp图像保存到SD卡

C++ 和文件输出

C++ 错误 : Sleep was not declared in this scope

c++ - 使用 boost::multi_index reverse_iterators