Qt拷贝文件、文件夹、创建文件夹、删除文件夹操作
-
/**
-
* @brief 拷贝文件到目的文件夹
-
* @param srcFileName 源文件全路径,比如 "F:/tx/wwxx.txt" ,"F:/tx/des/desd"
-
* @param desFilePathName 要COPY到的目的路径 比如 "F:/tx/des/desd"
-
* @param coverFileIfExist 如果目的文件存在,比如 "F:/tx/des/desd/wwxx.txt" 存在,是否覆盖,true表示覆盖
-
* @param bDelSrcFile 是否删除原来的文件,类似于剪切功能,比如是否删除 "F:/tx/wwxx.txt"
-
*/
-
bool MainWindow::copyFileToPath(QString srcFileName ,QString desFilePathName, bool coverFileIfExist,bool bDelSrcFile)
-
{
-
desFilePathName.replace(
"\\",
"/");
-
if (srcFileName == desFilePathName){
-
return
true;
-
}
-
if (!QFile::exists(srcFileName)){
-
return
false;
-
}
-
-
makeDir(desFilePathName);
// 校验目的文件夹路径是否存在,如果 不存在,创建
-
-
QFileInfo srcFinfo(srcFileName);
-
QFile srcFile(srcFileName);
-
-
QString desFileName = desFilePathName+
"/"+srcFinfo.fileName();
// 目的文件全路径
-
QFile desFinfo(desFileName);
-
if(desFinfo.exists()==
true)
-
{
-
if(coverFileIfExist)
-
{desFinfo.remove();}
-
}
-
QFile::copy(srcFileName,desFileName);
-
if(bDelSrcFile)
//! 删除原文件
-
{
-
srcFile.remove();
-
}
-
-
return
true;
-
}
-
/**
-
* @brief 创建文件夹,里面有递归创建操作
-
* @param dirName 源文件全路径,比如 "F:/tx/des/desd/wwxx" 如果已经存在,则什么也不做,如果原先不存在,则创建
-
*/
-
bool MainWindow::makeDir(QString dirName)
-
{
-
QStringList dirNameArray = dirName.split(
'/');
-
int nameSize = dirNameArray.size();
-
for(
int i=
1;i<nameSize+
1;i++)
-
{
-
QString iBefAllDirStr=
"";
-
for(
int j=
0;j<i;j++)
-
{
-
iBefAllDirStr += QString(dirNameArray.at(j)+
'/');
-
}
-
-
//QString sCurNowDirName = dirNameArray.at(0);
-
QDir diri(iBefAllDirStr);
-
if(diri.exists()==
false)
-
{diri.mkdir(iBefAllDirStr);}
-
}
-
return
true;
-
}
-
/**
-
* @brief 拷贝文件夹到目的文件夹
-
* @param source 源文件夹全路径,比如 "F:/tx" ,"F:/txd/des/desd"
-
* @param source 要COPY到的目的路径 比如 "F:/tx/des/desd"
-
* @param override 如果目的文件存在,比如 "F:/txd/des/desd" 存在,是否覆盖,true表示覆盖
-
*/
-
bool MainWindow::copyDir(const QString &source, const QString &destination, bool override)
-
{
-
QDir directory(source);
-
if (!directory.exists())
-
{
-
return
false;
-
}
-
-
-
QString srcPath = QDir::toNativeSeparators(source);
-
if (!srcPath.endsWith(QDir::separator()))
-
srcPath += QDir::separator();
-
QString dstPath = QDir::toNativeSeparators(destination);
-
if (!dstPath.endsWith(QDir::separator()))
-
dstPath += QDir::separator();
-
-
-
bool error =
false;
-
QStringList fileNames = directory.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
-
for (QStringList::size_type i=
0; i != fileNames.size(); ++i)
-
{
-
QString fileName = fileNames.at(i);
-
QString srcFilePath = srcPath + fileName;
-
QString dstFilePath = dstPath + fileName;
-
QFileInfo fileInfo(srcFilePath);
-
if (fileInfo.isFile() || fileInfo.isSymLink())
-
{
-
if (
override)
-
{
-
QFile::setPermissions(dstFilePath, QFile::WriteOwner);
-
}
-
QFile::copy(srcFilePath, dstFilePath);
-
}
-
else
if (fileInfo.isDir())
-
{
-
QDir dstDir(dstFilePath);
-
dstDir.mkpath(dstFilePath);
-
if (!copyDir(srcFilePath, dstFilePath,
override))
-
{
-
error =
true;
-
}
-
}
-
}
-
-
-
return !error;
-
}
-
/**
-
* @brief 删除文件夹-- 里面有递归操作
-
* @param path 删除的源文件夹全路径,比如 "F:/tx" ,"F:/txd/des/desd"
-
* 如果要实现文件夹的剪切功能,可以在调用的地方在copyDir之后,调用此函数
-
*/
-
bool MainWindow::DelDir(const QString &path)
-
{
-
if (path.isEmpty()){
-
return
false;
-
}
-
QDir dir(path);
-
if(!dir.exists()){
-
return
true;
-
}
-
dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
//设置过滤
-
QFileInfoList fileList = dir.entryInfoList();
// 获取所有的文件信息
-
foreach (QFileInfo file, fileList){
//遍历文件信息
-
if (file.isFile()){
// 是文件,删除
-
file.dir().remove(file.fileName());
-
}
else{
// 递归删除
-
DelDir(file.absoluteFilePath());
-
}
-
}
-
return dir.rmpath(dir.absolutePath());
// 删除文件夹
-
}