头文件
#include <QDir>
#include <QFileInfo>
#include <QFile>
创建文件夹
void CreateFolder(QString folderPath)
{
QDir dir(folderPath);
if (!dir.exists())
{
dir.mkdir(folderPath);
}
}
复制文件夹
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())
{
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;
}
重命名文件夹
void RenameFolder(const QString oldPath, const QString newPath)
{
QDir dirOld(oldPath);
dirOld.rename(oldPath, newPath);
}
移除文件夹
void RemoveFolder(const QString dirPath)
{
QDir dirItem(dirPath);
dirItem.removeRecursively();
}