简体   繁体   English

C ++:如何获取文件夹列表

[英]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.

相关问题 如何通过在C ++中单击它来将文件夹名称发送到函数 - How can i send a folders name to a function by clicking on it in C++ 如何使用 C 或 C++ 获取目录中的文件列表? - How can I get the list of files in a directory using C or C++? 如何在 C++ 中获取坐标? - How can i get coordinates in C++? 如何使用Boost和C ++递归和分别列出文件和文件夹 - How to list files and folders recursively and separately with Boost and C++ 如何设置Visual C ++ 2010 Express在构建过程中复制某些文件夹? - How can I setup Visual C++ 2010 Express to copy some folders during the build process? 如何使用非托管C ++获取Windows上已安装字体的列表? - How can I get a list of installed fonts on Windows, using unmanaged C++? 如何获取 Windows 上已安装 fonts 的列表,包括使用 c++ 的字体样式 - How can I get a list of installed fonts on Windows including font style using c++ 如何从单声道的c ++中获取某个类中所有方法的列表? - How can I get a list of all methods in a certain class from c++ in mono? 如何获取项目中所有 UClass 的列表(蓝图和 C++) - How can I get a List of all UClasses in the project (Blueprint and C++) C ++如何从虚拟设备获取文件夹和文件名? - C++ how to get folders and files names from virtual device?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM