java中windows目录通配符,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.

解决方案

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:

Use parent_path to break apart input into directories

Use filename to obtain the input filename

Obtain a directory_iterator to the relative or absolute path where the input begins

Create a recursive function which takes in begin and end iterators to the obtained parent path, the directory iterator, and the filename

Any time a directory or filename uses a '*' use a regex with the iterator to determine the directory which should be progressed to next

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值