QT file copy, move (cut) operation
File Copy
bool x= QFile::copy(old_name,new_name); qDebug()<<x;
File Move (Cut)
QString old_name="Path A"; QString new_name="New Path A"; bool x= QFile::rename(old_name,new_name); //Path A moves to path B qDebug()<<x;
Before renaming, the file has been used in other modules of my program, and the files in use cannot be renamed (under win platform).
Additional: I encountered a bug that cannot be moved, and after trying to manually move the file, the program was successfully executed
How to move a file?
QString old_name=QString("D:\\c++\\c++Excellent source code learning.txt"); QString new_name=QString("D:\\c++ Excellent source code learning.txt"); bool x= QFile::rename(old_name,new_name); //Path A moves to path B qDebug()<<x; //true
QString old_name=QString("D:\\c++\\c++Excellent source code learning.txt"); QString new_name=QString("D:\\123456\\c++ Excellent source code learning.txt"); bool x= QFile::rename(old_name,new_name); //Path A moves to path B qDebug()<<x; //false
Because there is no such directory as D:\\123456. You need to create this directory first.
QString old_name=QString("D:\\ccc\\");//exist QString new_name=QString("D:\\ccc\\ddd\\");//It exists itself bool x= QFile::rename(old_name,new_name); //Path A moves to path B qDebug()<<x; //false
Because the B path itself already has a file with the same name, the movement failed.
Just write this way:
QString old_name=QString("D:\\ccc\\"); QString new_name=QString("D:\\ccc\\ddd\\"); bool is_exists=QFile::exists(new_name); if(is_exists){ QFile::remove(new_name); } bool x= QFile::rename(old_name,new_name); //Path A moves to path B qDebug()<<x;
How to move a folder (including all the contents in it):
#include "" #include <QApplication> #include <QFile> #include <QDebug> #include <QDir> #include <> QString old_path; QString new_path; void create_Multilevel_folder(char* path) { int len = strlen(path); int _len = 0; for (int i = 0; i < len; ++i) { if (path[i] == '/') { _len++; } } int* a = new int[_len]; memset(a, 0, sizeof(a)); for (int i = 0, b = 0; i < len; ++i) { if (path[i] == '/') { a[b] = i; b++; } } for (int i = 0; i < _len; i++) { char p[4096]; strcpy(p, path); p[a[i]] = '\0'; if (access(p, 0) == -1) { qDebug()<<"Don't exist, create one"; mkdir(p); } } } void print_Files(QString path) { QDir dir(path); (QDir::Files | QDir::NoDotAndDotDot); QStringList list = (); for (int i = 0; i < (); ++i) { QString path1 = path + "/" + list[i]; qDebug() <<"Old_Files:"<< path1; //replace QString path2=(()-old_path.length()); QString path3=new_path+path2; qDebug()<<path3; qDebug()<<"New_Files:"<<QFile::rename(path1,path3); } } void print_files_and_dirs(QString path) { QDir dir(path); (QDir::Dirs | QDir::NoDotAndDotDot); QStringList list = (); for (int i = 0; i < (); ++i) { QString path1 = path + "/" + list[i]; qDebug() <<"old_Dir:"<< path1; QString path2=(()-old_path.length()); QString path3=new_path+path2+"/"; qDebug()<<"new_Dir:"<<path3; create_Multilevel_folder(path3.toLocal8Bit().data()); print_files_and_dirs(path1);//Prefer to prefix } print_Files(path); } int main(int argc, char *argv[]) { QApplication a(argc, argv); old_path="D:/Qt/zip"; new_path="D:/Qt/aaa"; QString str=new_path+"/"; create_Multilevel_folder(str.toLocal8Bit().data()); print_files_and_dirs(old_path); return (); }
How to delete a folder:
QString str="D:/Qt/a"; QDir dir(str); if(!()){ qDebug()<<"not exists"; } qDebug()<<(());
When the folder is empty, the deletion is successful.
Deletion fails when the folder is not empty.
Delete folders (recursive) (There must be only folders in the folder)
bool del_folder(QString str) { QDir dir(str); if(!()){ qDebug()<<"not exists"; } return (()); } void del_folders(QString path) { QDir dir(path); (QDir::Dirs | QDir::NoDotAndDotDot); QStringList list = (); //qDebug()<<"count:"<<(); //qDebug()<<list; for (int i = 0; i < (); ++i) { QString path1 = path + "/" + list[i]; qDebug() <<"old_Dir:"<< path1; del_folders(path1); } if(()==0){ del_folder(path); } } int main(int argc, char *argv[]) { QApplication a(argc, argv); old_path="D:/app/f"; del_folders(old_path); //It seems that the .vs file cannot be found return (); }
But after testing:.vs folderIt cannot be deleted.
because.vsIt's a hidden folder
Need to add:QDir::HiddenField.
(QDir::Hidden|QDir::Dirs | QDir::NoDotAndDotDot);
Function: Move the folder from A to B and delete it in its original location.
#include "" #include <QApplication> #include <QFile> #include <QDebug> #include <QDir> #include <> QString old_path; QString new_path; void create_Multilevel_folder(char* path) { int len = strlen(path); int _len = 0; for (int i = 0; i < len; ++i) { if (path[i] == '/') { _len++; } } int* a = new int[_len]; memset(a, 0, sizeof(a)); for (int i = 0, b = 0; i < len; ++i) { if (path[i] == '/') { a[b] = i; b++; } } for (int i = 0; i < _len; i++) { char p[4096]; strcpy(p, path); p[a[i]] = '\0'; if (access(p, 0) == -1) { qDebug()<<"Don't exist, create one"; mkdir(p); } } } void print_Files(QString path) { QDir dir(path); (QDir::Hidden|QDir::Files | QDir::NoDotAndDotDot); QStringList list = (); for (int i = 0; i < (); ++i) { QString path1 = path + "/" + list[i]; qDebug() <<"Old_Files:"<< path1; //replace QString path2=(()-old_path.length()); QString path3=new_path+path2; qDebug()<<path3; qDebug()<<"New_Files:"<<QFile::rename(path1,path3); } } void print_files_and_dirs(QString path) { QDir dir(path); (QDir::Hidden|QDir::Dirs | QDir::NoDotAndDotDot); QStringList list = (); for (int i = 0; i < (); ++i) { QString path1 = path + "/" + list[i]; qDebug() <<"old_Dir:"<< path1; QString path2=(()-old_path.length()); QString path3=new_path+path2+"/"; qDebug()<<"new_Dir:"<<path3; create_Multilevel_folder(path3.toLocal8Bit().data()); print_files_and_dirs(path1);//Prefer to prefix } print_Files(path); } bool del_folder(QString str) { QDir dir(str); if(!()){ qDebug()<<"not exists"; } return (()); } void del_folders(QString path) { QDir dir(path); (QDir::Hidden|QDir::Dirs | QDir::NoDotAndDotDot); QStringList list = (); //qDebug()<<"count:"<<(); //qDebug()<<list; for (int i = 0; i < (); ++i) { QString path1 = path + "/" + list[i]; qDebug() <<"old_Dir:"<< path1; del_folders(path1); } if(()==0){ del_folder(path); } } int main(int argc, char *argv[]) { QApplication a(argc, argv); old_path="D:/VS/ConsoleApplication1"; new_path="D:/VS/ConsoleApplication2"; QString str=new_path+"/"; create_Multilevel_folder(str.toLocal8Bit().data()); print_files_and_dirs(old_path); del_folders(old_path); return (); }
This is the article about the implementation example of Qt moving folders from A to B. This is all about this. For more related Qt moving folders from A to B. Please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!