当前位置:网站首页>Qt读取路径下所有文件或指定类型文件(含递归、判断是否为空、创建路径)
Qt读取路径下所有文件或指定类型文件(含递归、判断是否为空、创建路径)
2022-04-23 07:15:00 【欧特_Glodon】
//遍历获取文件夹下的所有文件
void Dialog::GetAllFiles(const QString path, QFileInfoList &fileInfoList)
{
QDir dir(path);
foreach(QFileInfo info,dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs))
{
//qDebug()<<"dir:" << info.filePath();
GetAllFiles(info.filePath(),fileInfoList);
}
foreach(QFileInfo info,dir.entryInfoList(QDir::Files))
{
fileInfoList.append(info);
qDebug()<< "文件:" << info.filePath();
}
}
// 获取当前文件夹下的指定类型文件(非递归)
void Dialog::GetDirFiles(const QString dirPath)
{
QDir *dir = new QDir(dirPath);
// 判断路径是否存在,不存在则创建
if(!dir->exists(dirPath))
{
// 创建路径文件,可多级
dir->mkpath(dirPath);
}
// 判断文件夹内容是否为空
QFileInfoList fileInfoList;
GetAllFiles(dirPath, fileInfoList);
if(fileInfoList.size() == 0)
{
QMessageBox::information(this,"提示","文件路径下不存在任何文件!");
return;
}
// 判断文件是否存在
// QFile fileName(filePath);
// if(!fileName.exists())
// {
// return true;
// }
QStringList filter;
QList<QFileInfo> *fileInfo = new QList<QFileInfo>(dir->entryInfoList(filter));
for(int i = 0; i < fileInfo->count(); i++)
{
if(fileInfo->at(i).fileName().split(".").back() != "cpp")
{
continue;
}
qDebug()<< fileInfo->at(i).filePath();
qDebug()<< fileInfo->at(i).fileName();
}
}
// 获取当前文件夹下的所有文件夹(非递归)
void Dialog::GetAllDirs(const QString path)
{
QDir dir(path);
foreach(QFileInfo info,dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs))
{
qDebug()<<"path:" << info.filePath();
qDebug()<<"name:" << info.fileName();
}
}
版权声明
本文为[欧特_Glodon]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_37251750/article/details/124300242
边栏推荐
- 将实例化对象的方法 给新的对象用
- Feign source code analysis
- 剑指offer day24 数学(中等)
- Hump naming object
- js将树形结构数据转为一维数组数据
- 1216_ MISRA_ C standard learning notes_ Rule requirements for control flow
- 以下程序实现从字符串str中删除第i个字符开始的连续n个字
- The following program deletes n consecutive words starting from the ith character from the string str
- Asynchronous learning
- clang 如何产生汇编文件
猜你喜欢
随机推荐
几种智能机器人室内定位方法对比
英语课小记(四)
Positioning and decoration style
vivo,硬件安全的爱与雷霆
編譯原理題-帶答案
Usage of databinding
巨头押注的全屋智能,正在驱动海信、华为、小米们「自我革命」
LeetCoed18. Sum of four numbers
一篇文章看懂变量提升(hoisting)
LeetCode简单题之重新排列日志文件
智能名片小程序名片详情页功能实现关键代码
dried food! Point based: differentiable Poisson solver
[Effective Go 中文翻译] 第一篇
惨了,搞坏了领导的机密文件,吐血分享备份文件的代码技巧
LeetCode中等题之旋转函数
编译原理题-带答案
The following program deletes n consecutive words starting from the ith character from the string str
Thinkphp6 + JWT realizes login verification
C outputs a two-dimensional array with the following characteristics.
CSV Column Extract列提取









