Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. File should delete itself

File should delete itself

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
10 Posts 7 Posters 1 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.
  • V Offline
    V Offline
    VCProgrammer
    wrote on last edited by
    #1

    Hi all, I have made a dialog based application from which i am trying to delete various other files.. my problem is it deletes all the file from the folder but leaves itself and the folder there only.... how can i delete it. Thanks in advance

    R J S J 4 Replies Last reply
    0
    • V VCProgrammer

      Hi all, I have made a dialog based application from which i am trying to delete various other files.. my problem is it deletes all the file from the folder but leaves itself and the folder there only.... how can i delete it. Thanks in advance

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      An executable cannot delete "itself". You may consider a script, or a batch file which will have all the files of your interest deleted. Here's a batch file, which works for me:

      @echo off
      sleep 50000
      del /q "D:\files\file1.txt"
      del /q "D:\files\file2.txt"
      del /q "D:\files\MyDialog.exe"
      del /q "D:\files\mybatch.bat"
      exit

      Whereas mybatch.bat is the batch file itself. Finish the stuff with your dialog and call this batch file with a ShellExecute()[^]. I've added a delay at the beginning of the batch file to make sure that the dialog gets enough time to quit.

      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

      H 1 Reply Last reply
      0
      • V VCProgrammer

        Hi all, I have made a dialog based application from which i am trying to delete various other files.. my problem is it deletes all the file from the folder but leaves itself and the folder there only.... how can i delete it. Thanks in advance

        J Offline
        J Offline
        Jagdish V Bhimbha
        wrote on last edited by
        #3

        Hi Friend, You should try this trick : There are several tricks for making programs that are in one way or another able to delete themselves, without leaving any traces in the system. One of the most wide-spread techniques, and also the one which is supposed to be one of the most platform independent (between Windows versions) is the following:

        Start the program you want to delete (EXE1).

        From this program, drop a second exe file (EXE2).

        Open a file handle to EXE2 from EXE1, using CreateFile, with the flag "FILE_FLAG_DELETE_ON_CLOSE".

        Execute EXE2 from EXE1 (e.g. with CreateProcess), causing the operating system to open another file handle to EXE2.

        Let EXE1 exit and terminate (which will implicitly cause its filehandle to EXE2 to close, leaving only the operating system's filehandle to EXE2 left open).

        EXE2 waits for EXE1 to terminate, and as soon as it detects this, it deletes EXE1 from disk.

        After successfully deleting EXE1, the job of EXE2 is complete, and it exits and terminates, causing the operating system's filehandle to it to close.

        The general concept and idea is now that EXE2 should immediately be deleted upon this closing of the last open file handle to it. This is also a seemingly correct assumption, especially when you read the entry for the "FILE_FLAG_DELETE_ON_CLOSE" flag of the CreateFile API in the Win32API reference

        Or you can visit the following link : http://www.woodmann.com/forum/archive/index.php/t-4542.html Have a Good Luck...:rose:

        Jagdish Bhimbha S/W Developer

        D 1 Reply Last reply
        0
        • V VCProgrammer

          Hi all, I have made a dialog based application from which i am trying to delete various other files.. my problem is it deletes all the file from the folder but leaves itself and the folder there only.... how can i delete it. Thanks in advance

          S Offline
          S Offline
          santhoshv84
          wrote on last edited by
          #4

          Hi, If you want to delete the folder after deleting the files. You this syntax. _wrmdir(dirname );

          Thanks and Regards. SANTHOSH V

          1 Reply Last reply
          0
          • V VCProgrammer

            Hi all, I have made a dialog based application from which i am trying to delete various other files.. my problem is it deletes all the file from the folder but leaves itself and the folder there only.... how can i delete it. Thanks in advance

            J Offline
            J Offline
            Jijo Raj
            wrote on last edited by
            #5

            VCProgrammer wrote:

            my problem is it deletes all the file from the folder but leaves itself and the folder there only.... how can i delete it.

            Since your exe is running, it could not be deleted. Better mark it for deletion after next reboot. You can use the api MoveFileEx() with flag MOVEFILE_DELAY_UNTIL_REBOOT. Check here for the code snippet - http://weseetips.com/2008/05/19/how-to-mark-your-file-for-deletion-after-next-reboot/[^] Regards, Jijo.

            _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

            1 Reply Last reply
            0
            • R Rajesh R Subramanian

              An executable cannot delete "itself". You may consider a script, or a batch file which will have all the files of your interest deleted. Here's a batch file, which works for me:

              @echo off
              sleep 50000
              del /q "D:\files\file1.txt"
              del /q "D:\files\file2.txt"
              del /q "D:\files\MyDialog.exe"
              del /q "D:\files\mybatch.bat"
              exit

              Whereas mybatch.bat is the batch file itself. Finish the stuff with your dialog and call this batch file with a ShellExecute()[^]. I've added a delay at the beginning of the batch file to make sure that the dialog gets enough time to quit.

              Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

              H Offline
              H Offline
              Hamid Taebi
              wrote on last edited by
              #6

              Hi Rajesh like yesterday?why you told good answer! :laugh:

              R 1 Reply Last reply
              0
              • H Hamid Taebi

                Hi Rajesh like yesterday?why you told good answer! :laugh:

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                Ah! I never knew that posting a good answer would get me a 1 vote. I wish the poor 1-voting creature all the happiness he could get. He'll need it to live with his lowly attitude. :) BTW, Thanks for the vote.

                Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                H 1 Reply Last reply
                0
                • R Rajesh R Subramanian

                  Ah! I never knew that posting a good answer would get me a 1 vote. I wish the poor 1-voting creature all the happiness he could get. He'll need it to live with his lowly attitude. :) BTW, Thanks for the vote.

                  Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                  H Offline
                  H Offline
                  Hamid Taebi
                  wrote on last edited by
                  #8

                  Rajesh R Subramanian wrote:

                  Ah! I never knew that posting a good answer would get me a 1 vote

                  Its not only for you its for some members. :-D

                  R 1 Reply Last reply
                  0
                  • H Hamid Taebi

                    Rajesh R Subramanian wrote:

                    Ah! I never knew that posting a good answer would get me a 1 vote

                    Its not only for you its for some members. :-D

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #9

                    Like - I belong to some kind of a special members group? :-D

                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                    1 Reply Last reply
                    0
                    • J Jagdish V Bhimbha

                      Hi Friend, You should try this trick : There are several tricks for making programs that are in one way or another able to delete themselves, without leaving any traces in the system. One of the most wide-spread techniques, and also the one which is supposed to be one of the most platform independent (between Windows versions) is the following:

                      Start the program you want to delete (EXE1).

                      From this program, drop a second exe file (EXE2).

                      Open a file handle to EXE2 from EXE1, using CreateFile, with the flag "FILE_FLAG_DELETE_ON_CLOSE".

                      Execute EXE2 from EXE1 (e.g. with CreateProcess), causing the operating system to open another file handle to EXE2.

                      Let EXE1 exit and terminate (which will implicitly cause its filehandle to EXE2 to close, leaving only the operating system's filehandle to EXE2 left open).

                      EXE2 waits for EXE1 to terminate, and as soon as it detects this, it deletes EXE1 from disk.

                      After successfully deleting EXE1, the job of EXE2 is complete, and it exits and terminates, causing the operating system's filehandle to it to close.

                      The general concept and idea is now that EXE2 should immediately be deleted upon this closing of the last open file handle to it. This is also a seemingly correct assumption, especially when you read the entry for the "FILE_FLAG_DELETE_ON_CLOSE" flag of the CreateFile API in the Win32API reference

                      Or you can visit the following link : http://www.woodmann.com/forum/archive/index.php/t-4542.html Have a Good Luck...:rose:

                      Jagdish Bhimbha S/W Developer

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      Jagdish V. Bhimbha wrote:

                      Open a file handle to EXE2 from EXE1, using CreateFile, with the flag "FILE_FLAG_DELETE_ON_CLOSE".

                      This does not work on all platforms. Sometimes CreateFile() will return INVALID_HANDLE_VALUE and GetLastError() will return ERROR_ACCESS_DENIED. This is because the OS supports the ability to share a file with delete access.

                      "Love people and use things, not love things and use people." - Unknown

                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups