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] Overwriting files when copying using QFile
Forum Updated to NodeBB v4.3 + New Features

[Solved] Overwriting files when copying using QFile

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 14.3k Views 1 Watching
  • 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.
  • E Offline
    E Offline
    Endless
    wrote on 18 Feb 2013, 20:38 last edited by
    #1

    Here's a code snippet that works fine provided the destinationFile doesn't exist:
    @
    QFile destinationFile, sourceFile;
    QDir destination("../CopiedFiles");
    QString fileName("MyData.dat");
    destinationFile.setFileName(destination.filePath(fileName));
    sourceFile.setFileName("../MainData/" + fileName);
    sourceFile.copy(destinationFile.fileName());
    @
    Is there any way to overwrite the file automatically, or would I have to go the long way around (checking if the destinationFile exists, and if it does, removing it and then copying)?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 18 Feb 2013, 22:05 last edited by
      #2

      As the docs say, QFile::copy() will not overwrite an existing file. It's hardly a "long way around" to delete or rename the file first.

      @
      if ( !destinationFile.exists() || destinationFile.remove() ) {
      if ( !sourceFile.copy(destinationFile.fileName()) )
      // Could not copy file
      }
      else
      // Could not remove file
      @

      1 Reply Last reply
      0
      • L Offline
        L Offline
        LovesTha
        wrote on 8 Oct 2014, 05:10 last edited by
        #3

        But "Remove, Copy" is hardly atomic, how do I ensure other processes, such as the web server never fail to find the file?

        1 Reply Last reply
        0

        • Login

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