1.递归删除文件夹,包括空文件夹
bool DeleteFileOrFolder(const QString& strPath)
{
if (strPath.isEmpty() || !QDir().exists(strPath))//是否传入了空的路径||路径是否存在
return false;
QFileInfo FileInfo(strPath);
if (FileInfo.isFile())//如果是文件
QFile::remove(strPath);
else if (FileInfo.isDir())//如果是文件夹
{
QDir qDir(strPath);
qDir.removeRecursively();
}
return true;
}
2.指定目录递归寻找文件夹
//寻找到文件返回0,找不到返回非零值
int FindFileFromDir(const QString& _filePath, const QString& fileName)
{
QDir dir(_filePath); //QDir的路径一定要是全路径,相对路径会有错误
if (!dir.exists())
return -1;
//取到所有的文件和文件名,去掉.和..文件夹
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
dir.setSorting(QDir::DirsFirst);
//将其转化为一个list
QFileInfoList list = dir.entryInfoList();
if (list.size() < 1)
return -1;
int i = 0;
//采用递归算法
do {
QFileInfo fileInfo = list.at(i);
bool bisDir = fileInfo.isDir();
if (bisDir) {
int ret = FindFileFromDir(fileInfo.filePath(), fileName);
if (0 == ret) {
return 0;
}
}
else {
if (0 == fileInfo.fileName().compare(fileName, Qt::CaseInsensitive)) {
qDebug() << "has find " << fileInfo.filePath() << ":" << fileInfo.fileName();
return 0;
}
}
++i;
} while (i < list.size());
return -1;
}
3.检查文件是否被占用
bool isFileUsed(QString path)
{
bool isUsed = false;
QString pathx = path + "x";
QFileInfo fileInfo(path);
if (fileInfo.isDir()) {
QDir dir(path);
if (dir.exists()) {
bool isCanRename = dir.rename(path, pathx);
if (isCanRename == false) {
isUsed = false;
}
else {
dir.rename(pathx, path);
}
}
}
else if (fileInfo.isFile()) {
QFile file(path);
if (file.exists())
{
bool isCanRename = file.rename(path, pathx);
if (isCanRename == false) {
isUsed = true;
}
else {
file.rename(pathx, path);
}
}
file.close();
}
return isUsed;
}
4.寻找文件所在的文件夹
QString findDirByFileName(const QString& strDir, const QString& fileName)
{
QDir dir(strDir); //QDir的路径一定要是全路径,相对路径会有错误
if (!dir.exists())
return QString();
//取到所有的文件和文件名,去掉.和..文件夹
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
dir.setSorting(QDir::DirsFirst);
//将其转化为一个list
QFileInfoList list = dir.entryInfoList();
if (list.size() < 1)
return QString();
int i = 0;
//采用递归算法
do {
QFileInfo fileInfo = list.at(i);
bool bisDir = fileInfo.isDir();
if (bisDir) {
QString strDir = findDirByFileName(fileInfo.filePath(), fileName);
if (!strDir.isEmpty()) {
return strDir;
}
}
else {
if (0 == fileInfo.fileName().compare(fileName, Qt::CaseInsensitive)) {
qDebug() <<"has find " << fileInfo.filePath() << ":" << fileInfo.fileName();
QString mdir = fileInfo.absoluteDir().absolutePath();
return mdir;
}
}
++i;
} while (i < list.size());
return QString();
}
5.拷贝文件夹
bool copyFolder(const QString& fromDir, const QString& toDir, bool coverFileIfExist)
{
QDir sourceDir(fromDir);
QDir targetDir(toDir);
if (!targetDir.exists())
{ //如果目标目录不存在,则进行创建
if (!targetDir.mkdir(targetDir.absolutePath())) return false;
}
QFileInfoList fileInfoList = sourceDir.entryInfoList();
foreach(QFileInfo fileInfo, fileInfoList)
{
if (fileInfo.fileName() == "." || fileInfo.fileName() == "..") continue;
if (fileInfo.isDir())
{ // 当为目录时,递归的进行copy
if (!copyFolder(fileInfo.filePath(),
targetDir.filePath(fileInfo.fileName()),
coverFileIfExist))
return false;
}
else
{ //当允许覆盖操作时,将旧文件进行删除操作
if (coverFileIfExist && targetDir.exists(fileInfo.fileName()))
{
targetDir.remove(fileInfo.fileName());
}
// 进行文件拷贝
if (!QFile::copy(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName())))
{
return false;
}
}
}
return true;
}