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. Delete only one file from Folder Documents

Delete only one file from Folder Documents

Scheduled Pinned Locked Moved Solved General and Desktop
deleteqdirfile
5 Posts 3 Posters 5.2k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi,

    is it possible to delete only one file from Documents with QDir? For example the file test.htm?
    I only heared about deleting all Content in a Folder but not only one file.

    Thank you,
    Henrik

    RatzzR 1 Reply Last reply
    0
    • ? A Former User

      Hi,

      is it possible to delete only one file from Documents with QDir? For example the file test.htm?
      I only heared about deleting all Content in a Folder but not only one file.

      Thank you,
      Henrik

      RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by Ratzz
      #2

      @HenrikSt.
      Have you tried remove(const QString & fileName) ?

      --Alles ist gut.

      1 Reply Last reply
      2
      • P Offline
        P Offline
        panosk
        wrote on last edited by
        #3

        Hi,

        Don't believe what you hear, just read the excellent docs :)
        bool QDir::remove(const QString &fileName)

        1 Reply Last reply
        2
        • P Offline
          P Offline
          panosk
          wrote on last edited by panosk
          #4

          After your request in chat, here is a small sample how you can do this:

          QDir dir(pathYouWantToSearch);
          QDirIterator it(dir);
          while (it.hasNext()) {
              QString currentFilePath = it.next();
              QFileInfo info(currentFilePath);         // Or simply replace these 3 lines
              QString fileName = info.fileName();      // with 'if (currentFilePath.endsWith("test.html"))
              if (fileName == "test.htm") {            // if you won't do anything more complex than that
                  dir.remove(currentFilePath);          
                  break;
              }
          }
          

          Edit: Maybe this is overkill if you always want to delete the same file from the same directory. Just use sth like this

          QDir dir(dirYouWant);
          dir.remove(fullPathToFile);
          

          And even simpler:

          QDir().remove(fullPathToTestHtm);
          
          ? 1 Reply Last reply
          3
          • P panosk

            After your request in chat, here is a small sample how you can do this:

            QDir dir(pathYouWantToSearch);
            QDirIterator it(dir);
            while (it.hasNext()) {
                QString currentFilePath = it.next();
                QFileInfo info(currentFilePath);         // Or simply replace these 3 lines
                QString fileName = info.fileName();      // with 'if (currentFilePath.endsWith("test.html"))
                if (fileName == "test.htm") {            // if you won't do anything more complex than that
                    dir.remove(currentFilePath);          
                    break;
                }
            }
            

            Edit: Maybe this is overkill if you always want to delete the same file from the same directory. Just use sth like this

            QDir dir(dirYouWant);
            dir.remove(fullPathToFile);
            

            And even simpler:

            QDir().remove(fullPathToTestHtm);
            
            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @panosk
            Thanks it works :)

            Henrik

            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