Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. QT Creator - QDir

QT Creator - QDir

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
14 Posts 2 Posters 2.1k 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
    FreeHassan29
    wrote on last edited by
    #1

    Hi. I am trying to use QDir to delete files and folders inside a folder. I want it to delete all the folders and files except one folder and it's files. Is there any way I can do this?

    Many thanks
    Hassan

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      you have to recursively run through all and skip the folder you want to keep.

      bool 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() && yourcheck ) {
                      result = removeDir(info.absoluteFilePath());
                  }
                  else {
                      result = QFile::remove(info.absoluteFilePath());
                  }
      
                  if (!result) {
                      return result;
                  }
              }
              result = dir.rmdir(dirName);
          }
          return result;
      }
      

      just need to add a check its its the folder you want to keep.
      https://stackoverflow.com/questions/27758573/deleting-a-folder-and-all-its-contents-with-qt

      F 1 Reply Last reply
      4
      • mrjjM mrjj

        Hi
        you have to recursively run through all and skip the folder you want to keep.

        bool 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() && yourcheck ) {
                        result = removeDir(info.absoluteFilePath());
                    }
                    else {
                        result = QFile::remove(info.absoluteFilePath());
                    }
        
                    if (!result) {
                        return result;
                    }
                }
                result = dir.rmdir(dirName);
            }
            return result;
        }
        

        just need to add a check its its the folder you want to keep.
        https://stackoverflow.com/questions/27758573/deleting-a-folder-and-all-its-contents-with-qt

        F Offline
        F Offline
        FreeHassan29
        wrote on last edited by
        #3

        @mrjj said in QT Creator - QDir:

        Hi
        you have to recursively run through all and skip the folder you want to keep.

        bool 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() && yourcheck ) {
                        result = removeDir(info.absoluteFilePath());
                    }
                    else {
                        result = QFile::remove(info.absoluteFilePath());
                    }
        
                    if (!result) {
                        return result;
                    }
                }
                result = dir.rmdir(dirName);
            }
            return result;
        }
        

        just need to add a check its its the folder you want to keep.
        https://stackoverflow.com/questions/27758573/deleting-a-folder-and-all-its-contents-with-qt

        Would be easier for me to just write the paths and delete needed for a batch file delete or implement this.
        My context is, I'm using Q String to store and save a path to a text file. I'd then like to read this line from the text file and add the rest of the path to it (folders I want to delete). The problem I am having with the batch file is that the path needs to be surrounded in "" in the batch file but with Q TextStream I can't do this.

        mrjjM 1 Reply Last reply
        0
        • F FreeHassan29

          @mrjj said in QT Creator - QDir:

          Hi
          you have to recursively run through all and skip the folder you want to keep.

          bool 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() && yourcheck ) {
                          result = removeDir(info.absoluteFilePath());
                      }
                      else {
                          result = QFile::remove(info.absoluteFilePath());
                      }
          
                      if (!result) {
                          return result;
                      }
                  }
                  result = dir.rmdir(dirName);
              }
              return result;
          }
          

          just need to add a check its its the folder you want to keep.
          https://stackoverflow.com/questions/27758573/deleting-a-folder-and-all-its-contents-with-qt

          Would be easier for me to just write the paths and delete needed for a batch file delete or implement this.
          My context is, I'm using Q String to store and save a path to a text file. I'd then like to read this line from the text file and add the rest of the path to it (folders I want to delete). The problem I am having with the batch file is that the path needs to be surrounded in "" in the batch file but with Q TextStream I can't do this.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @FreeHassan29
          ok. im not sure i think bat file is easier.
          Anyway, why cant you write " in TextStream ?
          Do you forget to escape the " ?

          F 1 Reply Last reply
          3
          • mrjjM mrjj

            @FreeHassan29
            ok. im not sure i think bat file is easier.
            Anyway, why cant you write " in TextStream ?
            Do you forget to escape the " ?

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

            @mrjj To be honest. I don't know how to escape it. This is what I need.

            FMC_dir = C:\Users\Hassan\AppData\Local\FiveM\FiveM.app\cache

            "rmdir "+ FMC_dir + "/priv /q /s "

            I need text written to the bat to be like this
            "C:\Users\Hassan\AppData\Local\FiveM\FiveM.app\cache\priv"
            In quotes or the bat doesn't work.
            Thanks

            mrjjM 1 Reply Last reply
            0
            • F FreeHassan29

              @mrjj To be honest. I don't know how to escape it. This is what I need.

              FMC_dir = C:\Users\Hassan\AppData\Local\FiveM\FiveM.app\cache

              "rmdir "+ FMC_dir + "/priv /q /s "

              I need text written to the bat to be like this
              "C:\Users\Hassan\AppData\Local\FiveM\FiveM.app\cache\priv"
              In quotes or the bat doesn't work.
              Thanks

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @FreeHassan29

              well its just

              \"
              something like
              stream << "rmdir " <<  "\"" <<  FMC_dir  <<  "/priv" << "\"" <<  "/q /s ";
              or
              QString quote = "\"";
              "rmdir "+ quote +FMC_dir + "/priv"+quote + "/q /s "
              
              
              
              F 1 Reply Last reply
              3
              • mrjjM mrjj

                @FreeHassan29

                well its just

                \"
                something like
                stream << "rmdir " <<  "\"" <<  FMC_dir  <<  "/priv" << "\"" <<  "/q /s ";
                or
                QString quote = "\"";
                "rmdir "+ quote +FMC_dir + "/priv"+quote + "/q /s "
                
                
                
                F Offline
                F Offline
                FreeHassan29
                wrote on last edited by
                #7

                @mrjj That works but I forgot to add, the /priv is a part of the path. Would I do this?
                ccache << "rmdir " << """ << FMC_dir << """ << "priv /q /s " << "\n";

                mrjjM 1 Reply Last reply
                0
                • F FreeHassan29

                  @mrjj That works but I forgot to add, the /priv is a part of the path. Would I do this?
                  ccache << "rmdir " << """ << FMC_dir << """ << "priv /q /s " << "\n";

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @FreeHassan29
                  just saw that.

                  "rmdir "+ quote +FMC_dir + "/priv"+quote + "/q /s "
                  stream << "rmdir " <<  "\"" <<  FMC_dir  <<  "/priv" << "\"" <<  "/q /s ";
                  
                  F 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @FreeHassan29
                    just saw that.

                    "rmdir "+ quote +FMC_dir + "/priv"+quote + "/q /s "
                    stream << "rmdir " <<  "\"" <<  FMC_dir  <<  "/priv" << "\"" <<  "/q /s ";
                    
                    F Offline
                    F Offline
                    FreeHassan29
                    wrote on last edited by
                    #9

                    @mrjj That's perfect. Thank you very much. Last question while I have your attention, would I use QProcess to launch this bat, and would QProcess work for creating another button to launch an application and connect to an IP?

                    mrjjM 1 Reply Last reply
                    0
                    • F FreeHassan29

                      @mrjj That's perfect. Thank you very much. Last question while I have your attention, would I use QProcess to launch this bat, and would QProcess work for creating another button to launch an application and connect to an IP?

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @FreeHassan29
                      Yes you can use QProcess to launch a bat file but you must use the /C
                      parameter

                      process.setProgram( "cmd.exe" ); // we need this to run the bat
                      process.setArguments( { "/C", " E:\\deleteme.bat" ); // notice the /C
                      

                      Notice sample uses startDetached. you dont need that.
                      https://stackoverflow.com/questions/51740460/execute-batch-file-with-qt-as-a-new-process

                      and yes, you can simply use QProcess to launch other app. (cmd.exe and /C not needed for that)

                      F 1 Reply Last reply
                      4
                      • mrjjM mrjj

                        @FreeHassan29
                        Yes you can use QProcess to launch a bat file but you must use the /C
                        parameter

                        process.setProgram( "cmd.exe" ); // we need this to run the bat
                        process.setArguments( { "/C", " E:\\deleteme.bat" ); // notice the /C
                        

                        Notice sample uses startDetached. you dont need that.
                        https://stackoverflow.com/questions/51740460/execute-batch-file-with-qt-as-a-new-process

                        and yes, you can simply use QProcess to launch other app. (cmd.exe and /C not needed for that)

                        F Offline
                        F Offline
                        FreeHassan29
                        wrote on last edited by FreeHassan29
                        #11

                        @mrjj
                        Thank you very much. If I didn't specify a directory because I wanted the bat to be in the same folder as the bat file what would I do. I tried it but it doesn't seem to be executing the bat.
                        QProcess process;
                        process.setProgram( "cmd.exe" ); // we need this to run the bat
                        process.setArguments( { "/C", "ClearCache.bat"} ); // notice the /C
                        Stack overflow is down right now

                        mrjjM 1 Reply Last reply
                        0
                        • F FreeHassan29

                          @mrjj
                          Thank you very much. If I didn't specify a directory because I wanted the bat to be in the same folder as the bat file what would I do. I tried it but it doesn't seem to be executing the bat.
                          QProcess process;
                          process.setProgram( "cmd.exe" ); // we need this to run the bat
                          process.setArguments( { "/C", "ClearCache.bat"} ); // notice the /C
                          Stack overflow is down right now

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #12

                          @FreeHassan29
                          Hi
                          you have to use full path to the bat file or else it can find it.
                          When your programs runs, the current folder is most likely inside the build folder.
                          You can use
                          qApp->applicationDirPath(); to say "where .exe" is
                          and place bat file there.
                          also the code you shown is not complete
                          need to call start or startDetached.

                          sample from SO

                          QProcess process;
                          process.setProgram( "cmd.exe" );
                          process.setArguments( { "/C", R"(E:\deleteme.bat)" } );
                          process.setWorkingDirectory( R"(E:\)" );
                          process.setStandardOutputFile( QProcess::nullDevice() );
                          process.setStandardErrorFile( QProcess::nullDevice() );
                          process.startDetached();
                          

                          https://doc.qt.io/qt-5/qprocess.html

                          F 1 Reply Last reply
                          1
                          • mrjjM mrjj

                            @FreeHassan29
                            Hi
                            you have to use full path to the bat file or else it can find it.
                            When your programs runs, the current folder is most likely inside the build folder.
                            You can use
                            qApp->applicationDirPath(); to say "where .exe" is
                            and place bat file there.
                            also the code you shown is not complete
                            need to call start or startDetached.

                            sample from SO

                            QProcess process;
                            process.setProgram( "cmd.exe" );
                            process.setArguments( { "/C", R"(E:\deleteme.bat)" } );
                            process.setWorkingDirectory( R"(E:\)" );
                            process.setStandardOutputFile( QProcess::nullDevice() );
                            process.setStandardErrorFile( QProcess::nullDevice() );
                            process.startDetached();
                            

                            https://doc.qt.io/qt-5/qprocess.html

                            F Offline
                            F Offline
                            FreeHassan29
                            wrote on last edited by
                            #13

                            @mrjj It worked without me having to set the path. I just used the filename like this.
                            QProcess process;
                            process.setProgram( "cmd.exe" ); // we need this to run the bat
                            process.setArguments( { "/C", "ClearCache.bat"} ); // notice the /C
                            process.startDetached();

                            It only ran because I used start detached

                            1 Reply Last reply
                            1
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Ok super. :)

                              1 Reply Last reply
                              1

                              • Login

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