【问题标题】:search client's computer for specific files在客户的计算机上搜索特定文件
【发布时间】:2013-03-27 10:47:51
【问题描述】:

搜索客户计算机(和其他安装的驱动器)的最快方法是什么?我的客户将安装一个使用 Python 编写的桌面应用程序,但如果速度更快,我可以添加 C++ 代码......

【问题讨论】:

  • 基于哪个平台 Win32 或 Unix ???
  • 我使用 os.walk() 和 glob 模块来完成这些任务
  • 最好同时基于 Win32 和 Unix
  • 这已经比os.walk()好...github.com/benhoyt/betterwalk

标签: c++ python search desktop-application


【解决方案1】:

如果你的平台是 Win32,使用 C++ 你可以简单地使用 Winapi 函数

FindFirstFile

FindFirstFileEx

然后

FindNextFile

作为文件名,您可以为已知图像格式提供通配符,例如 jpg、jpeg、png、bmp 等。

如果您想要更高的速度,您可以在不同的线程上运行函数,然后同步结果。

编辑:

对于独立于平台的解决方案,您可以使用boost::filesystem 类或Qt 的QDir

使用boost::filesystem递归搜索文件的示例代码

std::string target_path( "C:\\" );
boost::regex my_filter( "*\.bmp" );
std::vector< std::string > all_matching_files;
for ( boost::filesystem::recursive_directory_iterator end, dir(target_path); 
    dir != end; ++dir ) 
{
    // Skip if not a file
    if( !boost::filesystem::is_regular_file( i->status() ) ) 
        continue;
    boost::smatch what;

    // Skip if no match
    if( !boost::regex_match( i->leaf(), what, my_filter ) ) continue;

    // File matches, store it
    all_matching_files.push_back( i->leaf() );                                 
}

为了更好地实施,我强烈建议您阅读 boost::filesystem 文档

对于QDir 示例

filesStack = new QStack<QString>();

QDir selectedDir("C:\\");
selectedDir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDot | QDir::NoDotDot);
QStringList qsl; qsl.append("*.bmp");
selectedDir.setNameFilters(qsl);
findFilesRecursively(selectedDir);


void findFilesRecursively(QDir rootDir) 
{
    QDirIterator it(rootDir, QDirIterator::Subdirectories);
    while(it.hasNext()) 
    {
        filesStack->push(it.next());
    }
}

【讨论】:

    【解决方案2】:

    对于python,简单地使用os 模块的内置功能将是一个足够的跨平台解决方案。对于C++,我建议使用boost filesystem,这将是迄今为止最不痛苦的解决方案。

    至于速度,嗯,这可能无关紧要。此类函数将完全受 I/O 限制。理论上你可以线程化它,但如果它在同一个驱动器上,它仍然是 I/O 绑定的。如果它在单独的驱动器上,它可能会给你一些加速,但像往常一样,在优化之前进行配置。

    【讨论】:

      猜你喜欢
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 2012-01-20
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 2014-10-27
      相关资源
      最近更新 更多