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 All Files

Delete All Files

Scheduled Pinned Locked Moved Solved General and Desktop
applicationdeleteqprocessexefunction
13 Posts 5 Posters 1.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.
  • Joe von HabsburgJ Joe von Habsburg

    I need a function. If you ask, for what? That fuction's mission is delete all exe folder and some other folders.

    void delete(){
      //confirm the process or return
      // delete some folders
      // delete exe folder
    }
    

    When this process will be compleated, any one can not run exe again. If you ask again, why you need that ? Because I have to...

    Anyone will be able to help me?

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @Joe-von-Habsburg And what is the problem?
    There are https://doc.qt.io/qt-6/qfile.html#remove-1 and https://doc.qt.io/qt-6/qdir.html#remove
    If you need to know where the your exe is located then use https://doc.qt.io/archives/qt-5.15/qcoreapplication.html#applicationDirPath

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • Joe von HabsburgJ Offline
      Joe von HabsburgJ Offline
      Joe von Habsburg
      wrote on last edited by
      #3

      this function will be called in exe. When I run exe and call this function, could it be deleted ? or this function can delete runing exe ?

      I want to explain my self to you.

      My QtWidget App = MyApp.exe

      it's running, no problem

      Button = call delete() funtion

      It can be delete itself ?

      jsulmJ 1 Reply Last reply
      0
      • Joe von HabsburgJ Joe von Habsburg

        this function will be called in exe. When I run exe and call this function, could it be deleted ? or this function can delete runing exe ?

        I want to explain my self to you.

        My QtWidget App = MyApp.exe

        it's running, no problem

        Button = call delete() funtion

        It can be delete itself ?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @Joe-von-Habsburg On Windows you cannot delete files which are open (this includes executable files). On Linux you can.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • Joe von HabsburgJ Offline
          Joe von HabsburgJ Offline
          Joe von Habsburg
          wrote on last edited by
          #5

          I tried the QDir removeRecursively command, and it deleted some folders and files, which worked. However, when I try again with windeploy, this problem might occur. I shouldn't be able to redeploy.

          void delete()
          {
             QString password = QInputDialog::getText(nullptr, "Delete", tr("Password"), QLineEdit::Password);
             qDebug() << "password" << password;
             if(password == "test"){
                 QDir dir(qApp->applicationDirPath());
                 bool status = dir.removeRecursively();
                 qDebug() << "deleted....";
             }
          }
          

          @jsulm said in Delete All Files:

          On Windows you cannot delete files

          There need to be some solutions here.

          JonBJ jsulmJ 2 Replies Last reply
          0
          • Joe von HabsburgJ Joe von Habsburg

            I tried the QDir removeRecursively command, and it deleted some folders and files, which worked. However, when I try again with windeploy, this problem might occur. I shouldn't be able to redeploy.

            void delete()
            {
               QString password = QInputDialog::getText(nullptr, "Delete", tr("Password"), QLineEdit::Password);
               qDebug() << "password" << password;
               if(password == "test"){
                   QDir dir(qApp->applicationDirPath());
                   bool status = dir.removeRecursively();
                   qDebug() << "deleted....";
               }
            }
            

            @jsulm said in Delete All Files:

            On Windows you cannot delete files

            There need to be some solutions here.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #6

            @Joe-von-Habsburg said in Delete All Files:

            There need to be some solutions here.

            There aren't. Just because you want to delete an open file doesn't mean that Windows will let you do so.

            If you really want to delete a currently open file, such as your executable when it is running, the Windows approach is: you may add the file path to a Windows list of files to be deleted on next reboot. The delete will then happen automatically but only after user next reboots. This is typically what installers use. I don't know about windeploy or under Windows. It perhaps has its own installer way of allowing you to mark for deletion on next reboot.

            If by any chance you are trying to use this to do something like "hide" your executable from people who want to see it, forget it....

            Joe von HabsburgJ 1 Reply Last reply
            1
            • Joe von HabsburgJ Joe von Habsburg

              I tried the QDir removeRecursively command, and it deleted some folders and files, which worked. However, when I try again with windeploy, this problem might occur. I shouldn't be able to redeploy.

              void delete()
              {
                 QString password = QInputDialog::getText(nullptr, "Delete", tr("Password"), QLineEdit::Password);
                 qDebug() << "password" << password;
                 if(password == "test"){
                     QDir dir(qApp->applicationDirPath());
                     bool status = dir.removeRecursively();
                     qDebug() << "deleted....";
                 }
              }
              

              @jsulm said in Delete All Files:

              On Windows you cannot delete files

              There need to be some solutions here.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @Joe-von-Habsburg said in Delete All Files:

              However, when I try again with windeploy, this problem might occur.

              What problem? What do you try with windeploy? It is hard to understand what you mean.

              An alternative to what @JonB suggested: from your first app (which you want to delete) start another one. The first one then terminates. The second one waits a bit and then deletes the folder where the first one is located.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • JonBJ JonB

                @Joe-von-Habsburg said in Delete All Files:

                There need to be some solutions here.

                There aren't. Just because you want to delete an open file doesn't mean that Windows will let you do so.

                If you really want to delete a currently open file, such as your executable when it is running, the Windows approach is: you may add the file path to a Windows list of files to be deleted on next reboot. The delete will then happen automatically but only after user next reboots. This is typically what installers use. I don't know about windeploy or under Windows. It perhaps has its own installer way of allowing you to mark for deletion on next reboot.

                If by any chance you are trying to use this to do something like "hide" your executable from people who want to see it, forget it....

                Joe von HabsburgJ Offline
                Joe von HabsburgJ Offline
                Joe von Habsburg
                wrote on last edited by Joe von Habsburg
                #8

                @JonB said in Delete All Files:

                f by any chance you are trying to use this to do something like "hide" your executable from people who want to see it, forget it....

                No I want to delete for all of them which would like to.

                I can give example.

                You use my app ok? You have to drop computer and call delete function. When this happens, Joe or anyone won't be able to come and use the app and access the files.

                @JonB said in Delete All Files:

                Windows approach is: you may add the file path to a Windows list of files to be deleted on next reboot. The delete will then happen automatically but only after user next reboots

                I will look and research thanks

                @jsulm said in Delete All Files:

                An alternative to what @JonB suggested: from your first app (which you want to delete) start another one. The first one then terminates. The second one waits a bit and then deletes the folder where the first one is located.

                Yes, it may solve my problem thank you guys

                JonBJ 1 Reply Last reply
                0
                • Joe von HabsburgJ Joe von Habsburg

                  @JonB said in Delete All Files:

                  f by any chance you are trying to use this to do something like "hide" your executable from people who want to see it, forget it....

                  No I want to delete for all of them which would like to.

                  I can give example.

                  You use my app ok? You have to drop computer and call delete function. When this happens, Joe or anyone won't be able to come and use the app and access the files.

                  @JonB said in Delete All Files:

                  Windows approach is: you may add the file path to a Windows list of files to be deleted on next reboot. The delete will then happen automatically but only after user next reboots

                  I will look and research thanks

                  @jsulm said in Delete All Files:

                  An alternative to what @JonB suggested: from your first app (which you want to delete) start another one. The first one then terminates. The second one waits a bit and then deletes the folder where the first one is located.

                  Yes, it may solve my problem thank you guys

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #9

                  @Joe-von-Habsburg said in Delete All Files:

                  You use my app ok? You have to drop computer and call delete function. When this happens, Joe or anyone won't be able to come and use the app and access the files.

                  I don't know what this means. If you want to get rid of an installed application use an uninstaller. I cannot imagine why you wish to delete your own executable (or other things of yours) from your own executable.

                  1 Reply Last reply
                  0
                  • J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by J.Hilk
                    #10

                    You could open terminal or powershell via QProcess , define a short wait period, in which time you close your current running application and than execute Remove-Item "C:\path\to\folder" -Recurse -Force

                    that might work in most cases?

                    Start-Sleep -Seconds 2
                    Remove-Item "C:\path\to\folder" -Recurse -Force
                    

                    very fragile setup, I have to add/say!


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    JonBJ 1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      You could open terminal or powershell via QProcess , define a short wait period, in which time you close your current running application and than execute Remove-Item "C:\path\to\folder" -Recurse -Force

                      that might work in most cases?

                      Start-Sleep -Seconds 2
                      Remove-Item "C:\path\to\folder" -Recurse -Force
                      

                      very fragile setup, I have to add/say!

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #11

                      @J.Hilk It will certainly be "fragile" if you run it via e.g. QProcess::start(), won't it (unless something "unusual" under Windows)? ;-)

                      1 Reply Last reply
                      0
                      • Kent-DorfmanK Offline
                        Kent-DorfmanK Offline
                        Kent-Dorfman
                        wrote on last edited by
                        #12

                        The appropriate way to handle self-termination in a platform independent manner is with a batch system. Submit a request to spawn a background job to do it...and no, I'm not going to elaborate on how to create a batch processing system.

                        The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

                        1 Reply Last reply
                        0
                        • Joe von HabsburgJ Offline
                          Joe von HabsburgJ Offline
                          Joe von Habsburg
                          wrote on last edited by
                          #13

                          @J.Hilk said in Delete All Files:

                          Remove-Item "C:\path\to\folder" -Recurse -Force

                          It's working, Thank you..

                          void delete()
                          {
                              const QString appDir = QDir::toNativeSeparators(QCoreApplication::applicationDirPath());
                          
                              const QString ps = QString(
                                                     "Start-Sleep -Seconds 2; "
                                                     "Remove-Item -LiteralPath \"%1\" -Recurse -Force"
                                                     ).arg(appDir);
                          
                              QString program = "powershell.exe";
                              QStringList args = {
                                  "-NoProfile",
                                  "-ExecutionPolicy", "Bypass",
                                  "-WindowStyle", "Hidden",
                                  "-Command", ps
                              };
                          
                              const QString workingDir = QDir::tempPath();
                          
                              qint64 pid = 0;
                              const bool ok = QProcess::startDetached(program, args, workingDir, &pid);
                          
                              if (!ok) {
                                  return;
                              }
                          
                              QTimer::singleShot(0, qApp, &QCoreApplication::quit);
                          }
                          
                          
                          1 Reply Last reply
                          1
                          • Joe von HabsburgJ Joe von Habsburg has marked this topic as solved on

                          • Login

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