如何检查目录中的新文件
How to check for new files in the directory?
给定:
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();
}
正如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
相关文章:
- 从对象文件(.o)在macos上制作归档文件(.a文件)
- UE4 - Visual Studio在我从编辑器添加新的c ++文件后无法打开任何源文件 - UBT_COMPILED
- 有没有办法读取C++中的文件文件夹?
- 预处理器定义/添加新配置文件
- 错误C1083:无法打开包含文件.文件正确包含在代码中
- NDK 无法看到头文件 .h 文件
- 在 XCode 中快速创建和运行新的C++文件
- 在包含新CPP文件后的链接过程中,MSVC 错误
- 文件文件夹关联或归属
- Xcode-在项目导航器中创建新的C++文件-Order.h和.cpp-Order
- 如何以阻止的方式删除文件/文件夹
- 如何使用替代数据流添加新的文件属性
- 没有 /resetsettings 就无法运行 VS 2015 社区;在打开新项目时获取 CANTLOAD 库,并在新.cpp文件上崩溃
- 添加新头文件时出现C++错误LNK2005
- 在MFC中序列化为Unicode文本文件-文件头错误
- (Qt C++)如何从QTableView将所选文件/文件夹打印到文本文件中
- c++新手头文件问题
- 如何创建一个新的文件夹,如果它已经存在于c++中,则增加其索引
- 创建新文本文件C++时出现问题
- 为什么我无法更改新创建文件的"上次写入时间"?