[英]C++: how can I get the list of folders
I am not familiar with C++ as I am a C# developer. 我不熟悉C ++,因为我是C#开发人员。
In my project, I need to delete all folders of one week before. 在我的项目中,我需要删除一周前的所有文件夹。 In C++, how can I get the list of the folders of one week before based on the current system date time?
在C ++中,如何根据当前系统日期时间获取前一周的文件夹列表?
I am working on Eclipse IDE running on Ubuntu 10.10. 我正在运行在Ubuntu 10.10上的Eclipse IDE。
If you could provide some sample of code, that would be great. 如果您可以提供一些代码示例,那就太好了。
Thanks in advance and your help is much appreciated! 在此先感谢您的帮助,不胜感激!
It's more a question of your OS API than C++. 与C ++相比,这更是OS API的问题。 C++ itself provides no facilities for filesystem operations.
C ++本身不提供文件系统操作的功能。 However, there are several portable libraries that do, boost::filesystem for example.
但是,有几个可移植的库,例如boost :: filesystem 。
However, if you're stuck only to one operating system, it's easier to use it's facilities -- POSIX on *nixes, or WinAPI on Windows. 但是,如果仅使用一个操作系统,则使用它的功能会更容易-* nixes上的POSIX或Windows上的WinAPI。
Both are C based, to get a C++ solution, you need a third party library. 两者都是基于C的,要获得C ++解决方案,您需要一个第三方库。
On Linux, the following might get you started: 在Linux上,以下可能使您入门:
With boost: 随着提升:
#include <boost/foreach.hpp>
#include <boost/filesystem.hpp>
int main(int, char**)
{
time_t one_week_ago = std::time(NULL) - (7 * 24 * 3600);
boost::filesystem::directory_iterator dir("/tmp"), end;
BOOST_FOREACH(const boost::filesystem::path& p, std::make_pair(dir, end))
if(boost::filesystem::is_directory(p))
if(boost::filesystem::last_write_time(p) < one_week_ago)
boost::filesystem::remove_all(p);
}
or without using boost::foreach 或不使用boost :: foreach
#include <boost/filesystem.hpp>
int main(int, char**)
{
time_t one_week_ago = std::time(NULL) - (7 * 24 * 3600);
boost::filesystem::directory_iterator dir("/tmp"), it, end;
for(it = dir; it != end; it++)
{
const boost::filesystem::path& p = *it;
if(boost::filesystem::is_directory(p))
if(boost::filesystem::last_write_time(p) < one_week_ago)
boost::filesystem::remove_all(p);
}
}
I would have thought you should look at the functions opendir and readdir - also look at stat. 我本以为您应该看一下函数opendir和readdir-还看一下stat。 Basically, open the top level directory, and examine all entries in it that are directories.
基本上,打开顶级目录,并检查其中的所有条目都是目录。 The stat function will be able to give you access and modify times.
统计功能将使您能够访问和修改时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.