Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Files in directory with wildcard on Windows

Tags:

c++

winapi

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.

like image 753
Mikolaj Avatar asked Nov 26 '25 00:11

Mikolaj


1 Answers

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

like image 176
Jonathan Mee Avatar answered Nov 27 '25 13:11

Jonathan Mee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!