Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Copy all files in folder to another directory

[solved] Copy all files in folder to another directory

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 25.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    ThaRez
    wrote on last edited by
    #1

    Hello
    If I know the origin and destination path, let's say c:/org -> d:/trg, how can I copy all the files so that it overwrites those with the same file name ? Thank you!
    Richard

    ps. I'd also be interested in how to remove all the files in a folder... :)

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fluca1978
      wrote on last edited by
      #2

      I would iterate on each file in the source directory, get a file, and "copy":http://doc.qt.nokia.com/4.7/qfile.html#copy it on the final path. QFile has also a method remove to delete a file.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        QDir has also the "file and folder removing functionality":http://doc.qt.nokia.com/4.7/qdir.html#remove (see also rmdir there).

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • T Offline
          T Offline
          ThaRez
          wrote on last edited by
          #4

          Hello
          Thank you for your reply! I just tried the following:

          @QString orgFileName;

          foreach (orgFileName, files) // files = qstringlist with file names in folder 
          {
              QFile tmpFile(folderPath+orgFileName); 
              qDebug() << tmpFile.copy(jPath+orgFileName);
              qDebug() << "Copy: " << folderPath+"/UI/"+orgFileName << "To: " << jPath+orgFileName;
          }@
          

          But the copy just returns false... Do I need to open/create some file or... ? Thank you!
          Richard

          1 Reply Last reply
          0
          • R Offline
            R Offline
            raja26
            wrote on last edited by
            #5

            To remove all files
            Qt Code

            @
            QDir directory("C:/DirectoryPath");
            QStringList filesList = directory.entryList(QDir::Files);
            QString fileName;
            foreach(fileName, fileList)
            {
            if(directory.remove(fileName))
            qDebug() << "Removed: " << fileName;
            else
            qDebug() << "Not removed";
            }
            @

            To copy look "Look here for Documentation":http://doc.qt.nokia.com/4.7-snapshot/qfile.html#copy

            If you want to over write, you can check whether the file already present in the Directory using
            @
            QDir dir("filePath");
            if(!dir.exists("fileName"))
            //copy
            @

            Avoid overwriting, if the file is already there, simply don't copy. If you want to check for File size to see if it is the same file, then "See here":http://doc.qt.nokia.com/4.7-snapshot/qfile.html#size

            1 Reply Last reply
            0
            • T Offline
              T Offline
              ThaRez
              wrote on last edited by
              #6

              Now it works, thank you!

              1 Reply Last reply
              0
              • Y ynatynat referenced this topic on

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved