问题描述:

what is the fastest way of searching the clients's computer (and other mounted drives) say for images. My clients will have a desktop app installed which is in Python but I can add C++ code if that is faster...

网友答案:

If your platform is Win32, with C++ you can simply use Winapi functions

FindFirstFile

FindFirstFileEx

and then

FindNextFile

as filename you can give wildcards for known image formats such as jpg, jpeg, png, bmp etc.

If you want extra speed you can run functions on different threads then synchronize the results.

Edit:

For platform independent solution you can you use boost::filesystem class or Qt's QDir

Sample code for searching files recursively with 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() );                                 
}

For a better implenetation I strongly offer you to read boost::filesystem documentation

For QDir Example

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());
    }
}
网友答案:

For python, simply using the inbuilt functionality from the os module will be an adequate cross-platform solution. For C++, I'd suggest using boost filesystem, which will be by far the least painful solution.

As for speed, well, it likely won't matter. Such functions will be completely I/O bound. You could theoretically thread this, but if it's on the same drive, it'll still be I/O bound. If it's on separate drives, it may give you some speed-up, although, as usual, profile before optimizing.

相关阅读:
Top