如何忽略隐藏文件(文件和隐藏目录中)以提高文件系统? [英] How do I ignore hidden files (and files in hidden directories) with Boost Filesystem?

查看:150
本文介绍了如何忽略隐藏文件(文件和隐藏目录中)以提高文件系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在所有目录中的文件递归迭代使用下列内容:

I am iterating through all files in a directory recursively using the following:

try
{
    for ( bf::recursive_directory_iterator end, dir("./");
          dir != end; ++dir )
    {
       const bf::path &p = dir->path();
       if(bf::is_regular_file(p))
       {
           std::cout << "File found: " << p.string() << std::endl;
       }
    }
} catch (const bf::filesystem_error& ex) {
    std::cerr << ex.what() << '\n';
}

但是,这包括隐藏目录中隐藏文件和文件。

But this includes hidden files and files in hidden directories.

我如何过滤掉这些文件?如果需要,我可以限制自己的平台,其中隐藏的文件和目录,开始与'。性格。

How do I filter out these files? If needed I can limit myself to platforms where hidden files and directories begin with the '.' character.

推荐答案

不幸的是,似乎没有要处理隐藏一个跨平台的方式。在类Unix平台的以下工作:

Unfortunately there doesn't seem to be a cross-platform way of handling "hidden". The following works on Unix-like platforms:

首先定义:

bool isHidden(const bf::path &p)
{
    bf::path::string_type name = p.filename();
    if(name != ".." &&
       name != "."  &&
       name[0] == '.')
    {
       return true;
    }

    return false;
}

然后遍历文件就变成了:

Then traversing the files becomes:

try
{
    for ( bf::recursive_directory_iterator end, dir("./");
           dir != end; ++dir)
    {
       const bf::path &p = dir->path();

       //Hidden directory, don't recurse into it
       if(bf::is_directory(p) && isHidden(p))
       {
           dir.no_push();
           continue;
       }

       if(bf::is_regular_file(p) && !isHidden(p))
       {
           std::cout << "File found: " << p.string() << std::endl;
       }
    }
} catch (const bf::filesystem_error& ex) {
    std::cerr << ex.what() << '\n';
}

这篇关于如何忽略隐藏文件(文件和隐藏目录中)以提高文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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