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
Forum Updated to NodeBB v4.3 + New Features

Delete only one file from Folder Documents

Scheduled Pinned Locked Moved Solved General and Desktop
deleteqdirfile
5 Posts 3 Posters 4.5k 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 18 May 2016, 11:16 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

    R 1 Reply Last reply 18 May 2016, 11:28
    0
    • ? A Former User
      18 May 2016, 11:16

      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

      R Offline
      R Offline
      Ratzz
      wrote on 18 May 2016, 11:28 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 18 May 2016, 11:32 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 18 May 2016, 12:05 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 18 May 2016, 12:42
          3
          • P panosk
            18 May 2016, 12:05

            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 18 May 2016, 12:42 last edited by
            #5

            @panosk
            Thanks it works :)

            Henrik

            1 Reply Last reply
            0

            5/5

            18 May 2016, 12:42

            • Login

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