拷贝文件、文件夹、创建文件夹、删除文件夹操作

来源:互联网 发布:金庸武侠知乎 编辑:程序博客网 时间:2024/05/22 03:18

Qt拷贝文件、文件夹、创建文件夹、删除文件夹操作

[cpp] view plain copy
  1. /** 
  2.  * @brief 拷贝文件到目的文件夹 
  3.  * @param srcFileName 源文件全路径,比如  "F:/tx/wwxx.txt" ,"F:/tx/des/desd" 
  4.  * @param desFilePathName 要COPY到的目的路径 比如 "F:/tx/des/desd" 
  5.  * @param coverFileIfExist 如果目的文件存在,比如 "F:/tx/des/desd/wwxx.txt" 存在,是否覆盖,true表示覆盖 
  6.  * @param bDelSrcFile 是否删除原来的文件,类似于剪切功能,比如是否删除 "F:/tx/wwxx.txt" 
  7.  */  
  8. bool MainWindow::copyFileToPath(QString srcFileName ,QString desFilePathName, bool coverFileIfExist,bool bDelSrcFile)  
  9. {  
  10.     desFilePathName.replace("\\","/");  
  11.     if (srcFileName == desFilePathName){  
  12.         return true;  
  13.     }  
  14.     if (!QFile::exists(srcFileName)){  
  15.         return false;  
  16.     }  
  17.   
  18.     makeDir(desFilePathName);// 校验目的文件夹路径是否存在,如果 不存在,创建  
  19.   
  20.     QFileInfo srcFinfo(srcFileName);  
  21.     QFile srcFile(srcFileName);  
  22.   
  23.     QString desFileName = desFilePathName+"/"+srcFinfo.fileName();// 目的文件全路径  
  24.     QFile desFinfo(desFileName);  
  25.     if(desFinfo.exists()==true)  
  26.     {  
  27.         if(coverFileIfExist)  
  28.         {desFinfo.remove();}  
  29.     }  
  30.     QFile::copy(srcFileName,desFileName);  
  31.     if(bDelSrcFile)//! 删除原文件  
  32.     {  
  33.         srcFile.remove();  
  34.     }  
  35.   
  36.     return true;  
  37. }  

[cpp] view plain copy
  1. /** 
  2.  * @brief 创建文件夹,里面有递归创建操作 
  3.  * @param dirName 源文件全路径,比如  "F:/tx/des/desd/wwxx" 如果已经存在,则什么也不做,如果原先不存在,则创建 
  4. */  
  5. bool MainWindow::makeDir(QString dirName)  
  6. {  
  7.     QStringList dirNameArray = dirName.split('/');  
  8.     int nameSize = dirNameArray.size();  
  9.     for(int i=1;i<nameSize+1;i++)  
  10.     {  
  11.         QString iBefAllDirStr="";  
  12.         for(int j=0;j<i;j++)  
  13.         {  
  14.             iBefAllDirStr += QString(dirNameArray.at(j)+'/');  
  15.         }  
  16.   
  17.         //QString sCurNowDirName = dirNameArray.at(0);  
  18.         QDir diri(iBefAllDirStr);  
  19.         if(diri.exists()==false)  
  20.         {diri.mkdir(iBefAllDirStr);}  
  21.     }  
  22.     return true;  
  23. }  

[cpp] view plain copy
  1. /** 
  2.  * @brief 拷贝文件夹到目的文件夹 
  3.  * @param source 源文件夹全路径,比如  "F:/tx" ,"F:/txd/des/desd" 
  4.  * @param source 要COPY到的目的路径 比如 "F:/tx/des/desd" 
  5.  * @param override 如果目的文件存在,比如 "F:/txd/des/desd" 存在,是否覆盖,true表示覆盖 
  6.  */  
  7. bool MainWindow::copyDir(const QString &source, const QString &destination, bool override)  
  8. {  
  9.     QDir directory(source);  
  10.     if (!directory.exists())  
  11.     {  
  12.         return false;  
  13.     }  
  14.   
  15.   
  16.     QString srcPath = QDir::toNativeSeparators(source);  
  17.     if (!srcPath.endsWith(QDir::separator()))  
  18.         srcPath += QDir::separator();  
  19.     QString dstPath = QDir::toNativeSeparators(destination);  
  20.     if (!dstPath.endsWith(QDir::separator()))  
  21.         dstPath += QDir::separator();  
  22.   
  23.   
  24.     bool error = false;  
  25.     QStringList fileNames = directory.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);  
  26.     for (QStringList::size_type i=0; i != fileNames.size(); ++i)  
  27.     {  
  28.         QString fileName = fileNames.at(i);  
  29.         QString srcFilePath = srcPath + fileName;  
  30.         QString dstFilePath = dstPath + fileName;  
  31.         QFileInfo fileInfo(srcFilePath);  
  32.         if (fileInfo.isFile() || fileInfo.isSymLink())  
  33.         {  
  34.             if (override)  
  35.             {  
  36.                 QFile::setPermissions(dstFilePath, QFile::WriteOwner);  
  37.             }  
  38.             QFile::copy(srcFilePath, dstFilePath);  
  39.         }  
  40.         else if (fileInfo.isDir())  
  41.         {  
  42.             QDir dstDir(dstFilePath);  
  43.             dstDir.mkpath(dstFilePath);  
  44.             if (!copyDir(srcFilePath, dstFilePath, override))  
  45.             {  
  46.                 error = true;  
  47.             }  
  48.         }  
  49.     }  
  50.   
  51.   
  52.     return !error;  
  53. }  

[cpp] view plain copy
  1. /** 
  2.  * @brief 删除文件夹--  里面有递归操作 
  3.  * @param path 删除的源文件夹全路径,比如  "F:/tx" ,"F:/txd/des/desd" 
  4.  * 如果要实现文件夹的剪切功能,可以在调用的地方在copyDir之后,调用此函数 
  5.  */  
  6. bool MainWindow::DelDir(const QString &path)  
  7. {  
  8.     if (path.isEmpty()){  
  9.         return false;  
  10.     }  
  11.     QDir dir(path);  
  12.     if(!dir.exists()){  
  13.         return true;  
  14.     }  
  15.     dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot); //设置过滤  
  16.     QFileInfoList fileList = dir.entryInfoList(); // 获取所有的文件信息  
  17.     foreach (QFileInfo file, fileList){ //遍历文件信息  
  18.         if (file.isFile()){ // 是文件,删除  
  19.             file.dir().remove(file.fileName());  
  20.         }else// 递归删除  
  21.             DelDir(file.absoluteFilePath());  
  22.         }  
  23.     }  
  24.     return dir.rmpath(dir.absolutePath()); // 删除文件夹  
  25. }  

阅读全文
0 0
原创粉丝点击