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. i want remove Directory
QtWS25 Last Chance

i want remove Directory

Scheduled Pinned Locked Moved Solved General and Desktop
removedirectoryqprocess
7 Posts 2 Posters 9.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.
  • F Offline
    F Offline
    ForestPoem
    wrote on last edited by ForestPoem
    #1

    Hi.
    i want remove folder
    QDir dir1;
    dir.remove("TEST");

    Fail.... because
    There is a subfolder.
    TEST
    └A
    └B
    another way

     QProcess  pProcess = new QProcess;
    QString total;
        total="TEST";
            list1<<"-r"+total;
            pProcess->execute("rm",list1);
    

    This failure

    rm: Inappropriate options -- 'L'

    RatzzR 1 Reply Last reply
    0
    • F ForestPoem

      Hi.
      i want remove folder
      QDir dir1;
      dir.remove("TEST");

      Fail.... because
      There is a subfolder.
      TEST
      └A
      └B
      another way

       QProcess  pProcess = new QProcess;
      QString total;
          total="TEST";
              list1<<"-r"+total;
              pProcess->execute("rm",list1);
      

      This failure

      rm: Inappropriate options -- 'L'

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

      @ForestPoem
      For Qt5 you can try

      QDir dir("C:\\Path\\To\\Folder\\Here");
      dir.removeRecursively();
      

      --Alles ist gut.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        ForestPoem
        wrote on last edited by ForestPoem
        #3

        @Ratzz
        thank.
        but my QT ver 4.8

        RatzzR 1 Reply Last reply
        0
        • F ForestPoem

          @Ratzz
          thank.
          but my QT ver 4.8

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

          @ForestPoem
          You may try this https://john.nachtimwald.com/2010/06/08/qt-remove-directory-and-its-contents/

          --Alles ist gut.

          F 1 Reply Last reply
          0
          • RatzzR Ratzz

            @ForestPoem
            You may try this https://john.nachtimwald.com/2010/06/08/qt-remove-directory-and-its-contents/

            F Offline
            F Offline
            ForestPoem
            wrote on last edited by ForestPoem
            #5

            @Ratzz

            Yes I have tried before.
            not remove.....

            .cpp
            bool ResetSetting::removeDir(const QString & dirName)
            {
            bool result = true;
            QDir dir(dirName);

            if (dir.exists(dirName)) {
                Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden  | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
                    if (info.isDir()) {
                        result = removeDir(info.absoluteFilePath());
                    }
                    else {
                        result = QFile::remove(info.absoluteFilePath());
                    }
            
                    if (!result) {
                        return result;
                    }
                }
                result = dir.rmdir(dirName);
            }
            return result;
            

            }
            ////////////////////////////////////////////
            .h
            static bool removeDir(const QString &dirName);

            ///////////////////////////////////////////

            removeDir("TEST");

            RatzzR 1 Reply Last reply
            0
            • F ForestPoem

              @Ratzz

              Yes I have tried before.
              not remove.....

              .cpp
              bool ResetSetting::removeDir(const QString & dirName)
              {
              bool result = true;
              QDir dir(dirName);

              if (dir.exists(dirName)) {
                  Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden  | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
                      if (info.isDir()) {
                          result = removeDir(info.absoluteFilePath());
                      }
                      else {
                          result = QFile::remove(info.absoluteFilePath());
                      }
              
                      if (!result) {
                          return result;
                      }
                  }
                  result = dir.rmdir(dirName);
              }
              return result;
              

              }
              ////////////////////////////////////////////
              .h
              static bool removeDir(const QString &dirName);

              ///////////////////////////////////////////

              removeDir("TEST");

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

              @ForestPoem
              I tried the same code you pasted. Works fine even for subfolders with contents.
              Where do you pass the QString dirName?
              You should pass the full path name of directory

                  QString dir="C:/Users/Path_to_test/TEST";
                  removeDir(dir);

              --Alles ist gut.

              F 1 Reply Last reply
              1
              • RatzzR Ratzz

                @ForestPoem
                I tried the same code you pasted. Works fine even for subfolders with contents.
                Where do you pass the QString dirName?
                You should pass the full path name of directory

                    QString dir="C:/Users/Path_to_test/TEST";
                    removeDir(dir);
                F Offline
                F Offline
                ForestPoem
                wrote on last edited by
                #7

                @Ratzz
                Did you use a relative path. And I've solved another way thanks

                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