C ++:boost文件系统返回早于特定时间的文件列表 [英] C++:boost file system to return a list of files older than a specific time

查看:366
本文介绍了C ++:boost文件系统返回早于特定时间的文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用在Linux平台下运行C ++的 Boost :: FileSystem 库,我有一个问题:

I am using the Boost::FileSystem library with C++ running under Linux platform and I have a question following:

我想有一个文件列表,这些文件的修改比给定的日期时间早。我不知道boost :: FileSystem是否提供如下方法:

I would like to have a list of files which are modified older than a given date time. I dont know whether the boost::FileSystem offer such a method as:

vector<string> listFiles = boost::FileSystem::getFiles("\directory", "01/01/2010 12:00:00");

如果是,请提供示例代码?

If yes, could you please provide sample code?

提前感谢!

推荐答案

Boost :: filesystem不提供类似的功能。但您可以使用:

Boost::filesystem doesn't offer a function exactly like that. But you can use this:

http://www.boost.org/doc/libs/1_45_0/libs/filesystem/v3/doc/reference.html#last_write_time

作为写你自己的基础。下面是一些使用last_write_time的示例代码:

as a basis to write your own. Here is some sample code using last_write_time:

#include <boost/filesystem/operations.hpp>
#include <ctime>
#include <iostream>

int main( int argc , char *argv[ ] ) {
   if ( argc != 2 ) {
      std::cerr << "Error! Syntax: moditime <filename>!\n" ;
      return 1 ;
   }
   boost::filesystem::path p( argv[ 1 ] ) ;
   if ( boost::filesystem::exists( p ) ) {
      std::time_t t = boost::filesystem::last_write_time( p ) ;
      std::cout << "On " << std::ctime( &t ) << " the file " << argv[ 1 ] 
     << " was modified the last time!\n" ;
      std::cout << "Setting the modification time to now:\n" ;
      std::time_t n = std::time( 0 ) ;
      boost::filesystem::last_write_time( p , n ) ; 
      t = boost::filesystem::last_write_time( p ) ;
      std::cout << "Now the modification time is " << std::ctime( &t ) << std::endl ;
      return 0 ;
   } else {
      std::cout << "Could not find file " << argv[ 1 ] << '\n' ;
      return 2 ;
   }
}

这篇关于C ++:boost文件系统返回早于特定时间的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆