Problem with copying a folder in a specific path
-
QDirIterator it(fromDir, QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs ,QDirIterator::Subdirectories); QDir dir(fromDir); dir.setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs); dir.setSorting(QDir::SortFlag::DirsFirst | QDir::SortFlag::Name); qDebug() << "00000000: " << dir.absoluteFilePath(fromDir); qDebug() << "44444444: " << dir.dirName(); const int absSourcePathLength = dir.absoluteFilePath(fromDir).length(); while (it.hasNext()) { it.next(); const auto fileInfo = it.fileInfo(); if (!fileInfo.isHidden()) { qDebug() << "111111111: " << fileInfo.fileName(); qDebug() << "222222222: " << fileInfo.filePath(); qDebug() << "333333333: " << fileInfo.absoluteFilePath(); const QString subPathStructure = fileInfo.absoluteFilePath().mid(absSourcePathLength); const QString constructedAbsolutePath = toDir + "/" + dir.dirName() + subPathStructure; if (fileInfo.isDir()) { //Create directory in target folder dir.mkpath(constructedAbsolutePath); } else if (fileInfo.isFile()) { //Copy File to target directory //Remove file at target location, if it exists, or QFile::copy will fail QFile::remove(constructedAbsolutePath); QFile::copy(fileInfo.absoluteFilePath(), constructedAbsolutePath); } } } if (copyAndRemove) dir.removeRecursively(); m_lstCopyPath.clear();
Copy paste source code.
If you designate a specific folder as the copy destination, only the contents within the specific folder are copied. What I wanted was to copy the folder.folder
A
-> A_1
-> A_2
-> a_1.txt
-> a_2.txt
BFor example
There are two folders, A and B. Folder B is empty and you want to copy folder A into folder B.
If I run it with the source I made
A
-> A_1
-> A_2
-> a_1.txt
-> a_2.txt
B
-> A_1
-> A_2
-> a_1.txt
-> a_2.txtIt shows results like this.
The result I want is
A
-> A_1
-> A_2
-> a_1.txt
-> a_2.txt
B
A
-> A_1
-> A_2
-> a_1.txt
-> a_2.txtI want results like this. How can I modify the code to get the result I want?
-
@IknowQT Is really not that difficult to do (I assume that my assumption of what you want to achieve is correct, as you still did not clarify that).
First: create A inside B and then apply the code from the link using B/A as toDir. -
@IknowQT said in Problem with copying a folder in a specific path:
I want results like this
Like what?
Do you want to have A as subfolder in B?
Maybe https://forum.qt.io/topic/105993/copy-folder-qt-c/5 helps. -
@IknowQT Is really not that difficult to do (I assume that my assumption of what you want to achieve is correct, as you still did not clarify that).
First: create A inside B and then apply the code from the link using B/A as toDir.