[英]How can I find the file by its part of name
How can I find the a file by its part of name in c++/linux? 如何在c ++ / linux中按文件名查找文件? The file full name maybe like:
文件全名可能类似于:
foo.db.1
or 要么
foo.db.2
How can I find the file by part of its name "foo.db", Thanks 如何通过文件名“ foo.db”查找文件,谢谢
The Posix standard function for your use case is glob()
. 您的用例的Posix标准函数是
glob()
。 Earlier somebody posted an easy to use C++ wrapper around it here on Stackoverflow . 早些时候有人在Stackoverflow上发布了一个易于使用的C ++包装器。
For example with: 例如:
find /your/path -name "foo.db.*"
It will match all files in /your/path
structure whose name is like foo.db.<something>
. 它将匹配
/your/path
结构中所有名称类似于foo.db.<something>
。
If the text after db.
如果在
db.
之后输入文本db.
is always a number, you can do: 始终是数字,您可以执行以下操作:
find /your/path -name "foo.db.[0-9]*"
You can also use ls
: 您也可以使用
ls
:
ls -l /your/path/ls.db.[0-9]
to get files whose name is like foo.db.<number>
or even 获取名称类似于
foo.db.<number>
甚至偶数的文件
ls -l /your/path/ls.db.[0-9]*
to get files whose name is like foo.db.<different numbers>
获取名称类似于
foo.db.<different numbers>
文件。 foo.db.<different numbers>
Do like this: 这样做:
find /filepath -name "foo.db*"
In C/C++, you can do like this: 在C / C ++中,您可以这样:
void findFile(const char *path)
{
DIR *dir;
struct dirent *entry;
char fileName[] = "foo.db";
if (!(dir = opendir(path)))
return;
if (!(entry = readdir(dir)))
return;
do {
if (entry->d_type == DT_DIR) {
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", path, entry->d_name);
path[len] = 0;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
if(strstr(fileName, entry->d_name) != NULL) {
printf("%s\n", "", entry->d_name);
}
findFile(path);
}
else
printf(" %s\n", "", entry->d_name);
} while (entry = readdir(dir));
closedir(dir);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.