您的当前位置:首页正文

【转】Qt实现的拷贝文件文件夹的函数

来源:九壹网
【转】Qt实现的拷贝⽂件⽂件夹的函数

//拷贝⽂件:

bool MyTest007::copyFileToPath(QString sourceDir ,QString toDir, bool coverFileIfExist){

toDir.replace(\"\\\\\",\"/\"); if (sourceDir == toDir){ return true; }

if (!QFile::exists(sourceDir)){ return false; }

QDir *createfile = new QDir;

bool exist = createfile->exists(toDir); if (exist){

if(coverFileIfExist){

createfile->remove(toDir); } }//end if

if(!QFile::copy(sourceDir, toDir)) {

return false; }

return true;}

//拷贝⽂件夹:

bool MyTest007::copyDirectoryFiles(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(!copyDirectoryFiles(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()), coverFileIfExist)) return false; }

else{ /**< 当允许覆盖操作时,将旧⽂件进⾏删除操作 */ if(coverFileIfExist && targetDir.exists(fileInfo.fileName())){ targetDir.remove(fileInfo.fileName()); }

/// 进⾏⽂件copy

if(!QFile::copy(fileInfo.filePath(),

targetDir.filePath(fileInfo.fileName()))){ return false; } } }

return true;}

因篇幅问题不能全部显示,请点此查看更多更全内容

Top