Results 1 to 6 of 6

Thread: how can i copy a directary using QDir

  1. #1
    Join Date
    Jan 2006
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question how can i copy a directary using QDir

    hi,
    i want to create a directory and copy the content of another directory on it,how can i do?

    another question :
    this copy contains an executing file ,and i want to launch this execution using a qpushButton in a form, any answer ?


    regards.

  2. #2
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how can i copy a directary using QDir

    Hello,

    With QDir, you have the method mkpath to creat a full path (including parent directories that don't exist yet), really useful.

    Then to copy a file, you should use QFile::copy(QString sourceFile, QString outputFile)...

    There is no copy function in QDir, so you have to do a simple loop to copy a whole directory...by doing this, you can also connect a progressbar to monitor the copy process !

    Guilugi.

  3. #3
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how can i copy a directary using QDir

    Ooops,

    For your second question, it's also really simple.

    You connect your push button to a dedicated slot, that will start a QProcess linked to your executable, with all the arguments you want !
    Just check the docs

    Guilugi.

  4. #4
    Join Date
    Jan 2006
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how can i copy a directary using QDir

    but this directory contains other directories,how can i do?

  5. #5
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how can i copy a directary using QDir

    Quote Originally Posted by monaem
    but this directory contains other directories,how can i do?
    I have given below a trimmed down version which I used in an app. You still need to do some work.
    Qt Code:
    1. .
    2. #include <dirent.h>
    3. #include <sys/stat.h>
    4.  
    5. void copy(QString dir){
    6. DIR * d = opendir(QFile::encodeName(dir));
    7. dirent *ent;
    8. if(d == NULL)//Non-readble
    9. return;
    10. //code for copying dir goes here ---------------------------- 1 ------------------------------
    11. .
    12. .
    13. while((ent = readdir(d))){
    14. QByteArray entry = ent->d_name;
    15. if( entry == "." || entry == ".." )
    16. continue;
    17. entry.prepend( QFile::encodeName( dir.endsWith( "/" ) ? dir : dir + "/" ) );
    18. if(stat(entry, &statBuf) == 0){
    19. if(S_ISREG(statBuf.st_mode)){
    20. QString file_name_with_path = QString::fromLocal8Bit(entry);
    21. //code for copying file goes here
    22. }
    23. if(S_ISDIR(statBuf.st_mode)){
    24. QString dir_name_with_path = QString::fromLocal8Bit(entry);
    25. //code for copying dir goes here ---------- same as 1-----------------------
    26. .
    27. .
    28. copy(dir_name_with_path);
    29. }
    30. }
    31. }
    32. closedir( d );
    33. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how can i copy a directary using QDir

    Ok, this doesn't look very "Qt-ish". How about:

    Qt Code:
    1. bool copyDir(const QString &src, const QString &dest){
    2. QDir dir(src);
    3. QDir dirdest(dest);
    4. if(!dir.isReadable()) return false;
    5. QFileInfoList entries = dir.entryInfoList();
    6. for(QList<QFileInfo>::iterator it = entries.begin(); it!=entries.end();++it){
    7. QFileInfo &finfo = *it;
    8. if(finfo.fileName()=="." || finfo.fileName()=="..") continue;
    9. if(finfo.isDir()){ copyDir(finfo.filePath()); continue; }
    10. if(finfo.isSymLink()) { /* do something here */ continue; }
    11. if(finfo.isFile() && file.isReadable()){
    12. QFile file(finfo.filePath());
    13. file.copy(dirdest.absoluteFilePath(finfo.fileName()));
    14. } else return false;
    15. }
    16. return true;
    17. }
    To copy to clipboard, switch view to plain text mode 

    I didn't compile/test it, so it may not work, but shows the right direction.

Similar Threads

  1. How to explicitely delete a QDir?
    By alan in forum Newbie
    Replies: 2
    Last Post: 13th February 2006, 17:48

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.