Results 1 to 5 of 5

Thread: Copy Directory Recusively

  1. #1
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Copy Directory Recusively

    So I'm writing a function to copy a file or directory recursively, if it's just a file It just uses QFile Copy, and it copies the file and it works, but when it's a directory, I get the directory Entry List and loop through it calling the copy method for each file, for some reason I keep getting put into some endless loop. Can anyone help me out with this? My code is below.
    Qt Code:
    1. bool copy(QString sourceFilePath, QString destinationFilePath)
    2. {
    3. QFile sourceFile(sourceFilePath);
    4. QFile destinationFile(destinationFilePath);
    5. QFileInfo sourceFileInfo(sourceFile);
    6. QFileInfo destinationFileInfo(destinationFile);
    7.  
    8. if(sourceFileInfo.fileName() == "." || sourceFileInfo.fileName() == "..")
    9. return true;
    10.  
    11. //If we're just copying a file and it's already readable.
    12. if(sourceFileInfo.isFile() && sourceFileInfo.isReadable())
    13. {
    14. if(destinationFileInfo.isFile() && destinationFile.isWritable())
    15. {
    16. //Warning, this deletes the file and replaces it.
    17. QString newFileName = destinationFile.fileName();
    18. destinationFile.remove();
    19. return sourceFile.copy(newFileName);
    20. }
    21. else if(destinationFileInfo.isDir() && destinationFileInfo.isWritable())
    22. {
    23. //We can use fileName in this case because we know that the FULL PATH is set above.
    24. QString newFileName = destinationFile.fileName();
    25. newFileName.append("/");
    26. //We have to use this to get JUST THE FILE NAME of the original file.
    27. newFileName.append(QFileInfo(sourceFile).fileName());
    28. return sourceFile.copy(newFileName);
    29. }
    30. }
    31. //If we're copying a directory, it'll have to be done recursively.
    32. else if(sourceFileInfo.isDir() && sourceFileInfo.isReadable())
    33. {
    34. qDebug() << "SFI Abs Path:" << sourceFileInfo.absolutePath();
    35. QDir sourceFileDir(sourceFileInfo.absolutePath());
    36. sourceFileDir.setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot | QDir::Readable);
    37. QDir destinationFileDir(destinationFileInfo.absolutePath());
    38. qDebug() << "Mkdir output" << destinationFileDir.mkdir(sourceFileInfo.baseName());
    39. destinationFileDir.cd(sourceFileInfo.baseName());
    40.  
    41. const QFileInfoList sourceFileDirInfoList = sourceFileDir.entryInfoList();
    42. for(int i = 0; i < sourceFileDirInfoList.size(); i++)
    43. {
    44. qDebug() << "Copy params: " << sourceFileDirInfoList[i].absoluteFilePath() << destinationFileDir.absolutePath();
    45. copy(sourceFileDirInfoList[i].absoluteFilePath(), destinationFileDir.absolutePath());
    46. }
    47.  
    48. return true;
    49. }
    50.  
    51. //If we end up here, something went wrong.
    52. return false;
    53. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Copy Directory Recusively

    Use this function:
    Qt Code:
    1. void copyFolder(QString sourceFolder, QString destFolder)
    2. {
    3. QDir sourceDir(sourceFolder);
    4. if(!sourceDir.exists())
    5. return;
    6. QDir destDir(destFolder);
    7. if(!destDir.exists())
    8. {
    9. destDir.mkdir(destFolder);
    10. }
    11. QStringList files = sourceDir.entryList(QDir::Files);
    12. for(int i = 0; i< files.count(); i++)
    13. {
    14. QString srcName = sourceFolder + "/" + files[i];
    15. QString destName = destFolder + "/" + files[i];
    16. QFile::copy(srcName, destName);
    17. }
    18. files.clear();
    19. files = sourceDir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
    20. for(int i = 0; i< files.count(); i++)
    21. {
    22. QString srcName = sourceFolder + "/" + files[i];
    23. QString destName = destFolder + "/" + files[i];
    24. copyFolder(srcName, destName);
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to yogeshgokul for this useful post:

    hkothari (21st August 2009)

  4. #3
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Copy Directory Recusively

    Thank you very much, that seems to work.

  5. #4
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Copy Directory Recusively

    Here's a way to do it with iteration rather than recursion. None of these solutions avoid the infinite recursion problem.
    Qt Code:
    1. void copyFolder(const QString& sourceFolder,const QString& destFolder)
    2. {
    3. QQueue< QPair<QString, QString> > queue;
    4.  
    5. queue.enqueue(qMakePair(sourceFolder, destFolder));
    6.  
    7. while (!queue.isEmpty())
    8. {
    9. QPair<QString, QString> pair = queue.dequeue();
    10. QDir sourceDir(pair.first);
    11. QDir destDir(pair.second);
    12.  
    13. if(!sourceDir.exists())
    14. continue;
    15.  
    16. if(!destDir.exists())
    17. destDir.mkpath(pair.second);
    18.  
    19. QStringList files = sourceDir.entryList(QDir::Files);
    20. for(int i = 0; i < files.count(); i++)
    21. {
    22. QString srcName = pair.first + "/" + files.at(i);
    23. QString destName = pair.second + "/" + files.at(i);
    24. QFile::copy(srcName, destName);
    25. }
    26.  
    27. QStringList dirs = sourceDir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
    28. for(int i = 0; i < dirs.count(); i++)
    29. {
    30. QString srcName = pair.first + "/" + dirs.at(i);
    31. QString destName = pair.second + "/" + dirs.at(i);
    32. queue.enqueue(qMakePair(srcName, destName));
    33. }
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Copy Directory Recusively

    I'd prefer recursive calling instead of cashing the paths in a queue, but anyway, use QDir::separator() instead of the slash in case you want cross platform compatibility.

Similar Threads

  1. some questions about install qwt
    By dycjiaoda in forum Qwt
    Replies: 1
    Last Post: 8th February 2011, 01:37
  2. install help nedded.
    By aj2903 in forum Installation and Deployment
    Replies: 9
    Last Post: 13th November 2008, 07:57
  3. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  4. Replies: 4
    Last Post: 24th February 2006, 10:30
  5. Am I the only one with "make" error ?
    By probine in forum Installation and Deployment
    Replies: 1
    Last Post: 13th February 2006, 12:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.