Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

c++ - Files in directory with wildcard on Windows

how can I easy get all files paths from path containing a wildcards? For example: C:/Data*Set/Files*/*.txt and I wrote it on Linux using glob function but I can't do it on Windows :/

FindFirstFile unfortunately doesn't support wildcards in directory names.

I think there should be a Windows solution available but I can't find it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So you should do away with using OS specific file access, in favor of the OS independent: Filesystem Library

Let's say that you're given filesystem::path input which contains the path with wildcards. To use this to solve your problem you'd need to:

  1. Use parent_path to break apart input into directories
  2. Use filename to obtain the input filename
  3. Obtain a directory_iterator to the relative or absolute path where the input begins
  4. Create a recursive function which takes in begin and end iterators to the obtained parent path, the directory iterator, and the filename
  5. Any time a directory or filename uses a '*' use a regex with the iterator to determine the directory which should be progressed to next
  6. Either return the path to the matching file or an empty path

Due to the excellent Ben Voigt's comment I've updated the algorithm to step over unwildcarded directories.

For example:

regex GenerateRegex(string& arg) {
    for (auto i = arg.find('*'); i != string::npos; i = arg.find('*', i + 2)) {
        arg.insert(i, 1, '.');
    }

    return regex(arg);
}

filesystem::path FindFirstFile(filesystem::path directory, filesystem::path::const_iterator& start, const filesystem::path::const_iterator& finish, string& filename) {
    while (start != finish && start->string().find('*') == string::npos) {
        directory /= *start++;
    }
    filesystem::directory_iterator it(directory);
    filesystem::path result;

    if (it != filesystem::directory_iterator()) {
        if (start == finish) {
            for (auto i = filename.find('.'); i != string::npos; i = filename.find('.', i + 2)) {
                filename.insert(i, 1, '\');
            }
            const auto re = GenerateRegex(filename);

            do {
                if (!filesystem::is_directory(it->status()) && regex_match(it->path().string(), re)) {
                    result = *it;
                    break;
                }
            } while (++it != filesystem::directory_iterator());
        }
        else {
            const auto re = GenerateRegex(start->string());

            do {
                if (it->is_directory() && regex_match(prev(it->path().end())->string(), re)) {
                    result = FindFirstFile(it->path(), next(start), finish, filename);

                    if (!result.empty()) {
                        break;
                    }
                }
            } while (++it != filesystem::directory_iterator());
        }
    }
    return result;
}

Which can be called with:

const filesystem::path input("C:/Test/Data*Set/Files*/*.txt");

if (input.is_absolute()) {
    const auto relative_parent = input.parent_path().relative_path();

    cout << FindFirstFile(input.root_path(), begin(relative_parent), end(relative_parent), input.filename().string()) << endl;
} else {
    const auto parent = input.parent_path();

    cout << FindFirstFile(filesystem::current_path(), begin(parent), end(parent), input.filename().string()) << endl;
}

Live Example


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...