问题描述
假设:
-
文件系统路径:: toDir(./\");
-
分组时间古来;
-
现在的ptime(second_clock :: LOCAL_TIME());
我如何确定哪些文件是在该时间段古来之间建立
和现在
?
这样的新鲜的文件的名称应该被传输到 COUT
。
更新:的
那么根据给定的答案,我做了一个小PROGRAMM:
的#include< sstream>
#包括LT&;&stdio.h中GT;
#包括LT&;&time.h中GT;
#包括LT&;升压/ filesystem.hpp>
#包括LT&;升压/ thread.hpp>
#包括LT&;升压/ timer.hpp>
#包括LT&;升压/ date_time.hpp>
#包括LT&;升压/ DATE_TIME /了posix_time / posix_time.hpp>
#包括LT&;升压/ DATE_TIME /了posix_time / posix_time_io.hpp>使用空间boost ::文件系统;
使用空间boost ::了posix_time;
使用空间boost :: LOCAL_TIME;
使用空间boost ::格利高;
使用命名空间std;路径file_service_default_path;提高::计时器timerFame;
desiredTimeFame的int64_t;
spendedTimeFame的int64_t;
分组时间古来;
分组时间nowTime;
布尔FIRST_TIME;字符串file_service_get_dif_path(路径BASE_PATH,路径new_path)
{
路径sdiffpath;
路径stmppath = new_path;
而(stmppath!= BASE_PATH){
sdiffpath =路径(stmppath.stem()的字符串()+ stmppath.extension()字符串()。)/ sdiffpath;
stmppath = stmppath.parent_path();
}
字符串diff_path = sdiffpath.string(); //提高:: lexical_cast的<串GT;(sdiffpath);
diff_path = diff_path.substr(0,(diff_path.length()));
的std ::代替(diff_path.begin(),diff_path.end(),'\\\\','/');
返回diff_path;
}无效is_file(路径p)
{
性病::字符串= file_service_get_dif_path(file_service_default_path,P);
性病::法院LT&;< 文件:<< A<<的std :: ENDL;
}无效is_new_file(路径p)
{
的std :: time_t的T =的boost ::文件系统:: last_write_time(P);
分组时间的LastAccessTime = from_time_t(T);
如果(的LastAccessTime> =古来&放大器;&安培;&的LastAccessTime LT = nowTime)
{
性病::字符串= file_service_get_dif_path(file_service_default_path,P);
性病::法院LT&;< 文件:<< A<< 是新的给我们。 <<的std :: ENDL;
}
}无效is_dir(路径DIR)
{
提高::文件系统:: directory_iterator dirIter(DIR),dirIterEnd;
而(dirIter!= dirIterEnd)
{
如果(的boost ::文件系统::存在(* dirIter)及和放大器;!的boost ::文件系统:: is_directory(* dirIter))
{
如果(FIRST_TIME)
{
is_file((* dirIter));
}
其他
{
is_new_file((* dirIter));
}
}
其他
{
is_dir((* dirIter));
}
++ dirIter;
}
}无效files_walker()
{
而(真)
{
timerFame.restart();
古来= 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;
如果(spendedTimeFame< desiredTimeFame)
提高:: this_thread ::睡眠(升压::了posix_time ::毫秒(desiredTimeFame - spendedTimeFame));
}
}诠释的main()
{
desiredTimeFame =(的int64_t)(5000.0f);
FIRST_TIME = TRUE;
file_service_default_path =./new;
提高::线程的WorkerThread(files_walker);
cin.get();
}
但似乎不显示任何新文件=(如何解决它?
更新2:的
解决和 nowTime = second_clock :: universal_time();
更新3:的
固定code:
的#include< sstream>
#包括LT&;&stdio.h中GT;
#包括LT&;&time.h中GT;
#包括LT&;升压/ filesystem.hpp>
#包括LT&;升压/ thread.hpp>
#包括LT&;升压/ timer.hpp>
#包括LT&;升压/ date_time.hpp>
#包括LT&;升压/ DATE_TIME /了posix_time / posix_time.hpp>
#包括LT&;升压/ DATE_TIME /了posix_time / posix_time_io.hpp>使用空间boost ::文件系统;
使用空间boost ::了posix_time;
使用空间boost :: LOCAL_TIME;
使用空间boost ::格利高;
使用命名空间std;路径file_service_default_path;提高::计时器timerFame;
desiredTimeFame的int64_t;
spendedTimeFame的int64_t;
分组时间古来;
分组时间nowTime;
布尔FIRST_TIME;字符串file_service_get_dif_path(路径BASE_PATH,路径new_path)
{
路径sdiffpath;
路径stmppath = new_path;
而(stmppath!= BASE_PATH){
sdiffpath =路径(stmppath.stem()的字符串()+ stmppath.extension()字符串()。)/ sdiffpath;
stmppath = stmppath.parent_path();
}
字符串diff_path = sdiffpath.string(); //提高:: lexical_cast的<串GT;(sdiffpath);
diff_path = diff_path.substr(0,(diff_path.length()));
的std ::代替(diff_path.begin(),diff_path.end(),'\\\\','/');
返回diff_path;
}无效is_file(路径p)
{
性病::字符串= file_service_get_dif_path(file_service_default_path,P);
性病::法院LT&;< 文件:<< A<<的std :: ENDL;
}无效is_new_file(路径p)
{
的std :: time_t的T =的boost ::文件系统:: last_write_time(P);
分组时间的LastAccessTime = from_time_t(T);
如果(的LastAccessTime> =古来&放大器;&安培;&的LastAccessTime LT = nowTime)
{
性病::字符串= file_service_get_dif_path(file_service_default_path,P);
性病::法院LT&;< 文件:<< A<< 是新的给我们。 <<的std :: ENDL;
}
}无效is_dir(路径DIR)
{
如果(!存在(DIR))
{
返回;
}
提高::文件系统:: directory_iterator dirIter(DIR);
提高::文件系统:: directory_iterator dirIterEnd;
而(dirIter!= dirIterEnd)
{
如果(的boost ::文件系统::存在(* dirIter)及和放大器;!的boost ::文件系统:: is_directory(* dirIter))
{
如果(FIRST_TIME)
{
is_file((* dirIter));
}
其他
{
is_new_file((* dirIter));
}
}
其他
{
is_dir((* dirIter));
}
++ dirIter;
}
}无效files_walker()
{
而(真)
{
timerFame.restart();
古来= nowTime;
nowTime = second_clock :: universal_time();
is_dir(file_service_default_path);
FIRST_TIME = FALSE;
spendedTimeFame =(的int64_t)timerFame.elapsed();
COUT<< spendedTimeFame<<的std :: ENDL;
如果(spendedTimeFame< desiredTimeFame)
提高:: this_thread ::睡眠(升压::了posix_time ::毫秒(desiredTimeFame - spendedTimeFame));
}
}诠释的main()
{
desiredTimeFame =(的int64_t)(5000.0f);
FIRST_TIME = TRUE;
file_service_default_path =./new;
提高::线程的WorkerThread(files_walker);
cin.get();
}
由于埃米尔·科米尔说,你将无法得到的Unix文件的创建时间。但是,你仍然可以寻找新鲜的文件,这取决于你的新鲜的定义,通过看最后访问时间。这是通过调用如下实现的:
的boost ::文件系统::路径p(somefile);
的std :: time_t的T =的boost ::文件系统:: last_write_time(P);
提高::分组时间的LastAccessTime =提振::分组时间:: from_time_t(T);
所以,如果你再看看的boost ::文件系统:: directory_iterator
您可以遍历目录,即./,你在你的问题说明和比较的LastAccessTime
中的例子给出以上在你的问题中指定的范围。
所以,我会做这个,假设古来
的方式已经pviously定义$ P $,如下:
的boost ::文件系统::路径目录(./);
提高::分组时间nowTime(升压:: second_clock :: LOCAL_TIME());提高::文件系统:: directory_iterator dirIter(DIR),dirIterEnd;
而(dirIter!= dirIterEnd)
{
如果(的boost ::文件系统::存在(* dirIter)及和放大器;!的boost ::文件系统:: is_directory(* dirIter))
{
的std :: time_t的T =的boost ::文件系统:: last_write_time(* dirIter);
提高::分组时间的LastAccessTime =提振::分组时间:: from_time_t(T);
如果(的LastAccessTime> =古来&放大器;&安培;&的LastAccessTime LT = nowTime)
{
性病::法院LT&;< 文件符合标准:<< (* dirIter).filename()<<的std :: ENDL;
}
}
++ dirIter;
}
希望这有助于!
您可以通过持久地存储您选中的目录和/或文件present在您上次检查目录中的最后一个列表的最后时间完善这一,并以此作为一个点的比较,而不是。如果没有为您的意图的更多信息我不能提供进一步的指导。
相关文档:
,的
Given:
filesystem::path toDir("./");
ptime oldTime;
ptime now(second_clock::local_time());
How can I determine which files were created in the time period between oldTime
and now
?
The names of such "fresh" files should be streamed to cout
.
Update:Well based on given answer I made a small 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();
}
But it seems not to show any new files=( how to fix it?
Update 2:
Solved with nowTime = second_clock::universal_time();
Update 3:fixed code:
#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();
}
As Emile Cormier stated, you will be unable to get the file creation time in Unix. However, you can still look for 'fresh' files, depending on your definition of 'fresh', by looking at last access time. This is achieved by a call as follows:
boost::filesystem::path p( "somefile" );
std::time_t t = boost::filesystem::last_write_time( p );
boost::ptime lastAccessTime = boost::ptime::from_time_t( t );
So if you then look at the boost::filesystem::directory_iterator
you are able to iterate over a directory, i.e. "./" as you state in your question, and compare lastAccessTime
given in the example above with the range specified in your question.
So the way I would do this, assuming oldTime
has been previously defined, is as follows:
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;
}
Hope this helps!
You could refine this by persistently storing the last time you checked the directory and/or the last list of files present in the directory when you last checked, and use this as a point of comparison instead. Without more information as to your intentions I can't provide further guidance.
Relevant docs:boost::filesystem Reference, boost::ptime Reference
这篇关于如何检查目录中的新文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!