Delete a file upon exit, the own executable file.

Posted on 2006-06-06
Last Modified: 2012-08-14

Let said i have run.exe, is it possible to delete run.exe upon exit of run.exe? meaning deleting it self after exit from the app. I know it is quite impossible but is there any way around that possible made up for this?

Question by:iphone
    LVL 11

    Expert Comment

    You can have a batch script that deletes the file after running it.

    delete run.exe
    LVL 84

    Expert Comment

    It depends on the operating system.
    LVL 4

    Assisted Solution

    You can use the spawn function to spawn a secondary application that will delete the first application.

    You can then set the secondary application to take 2 parameters, being the file to be deleted and a wait period allowing the first file to terminate successfully.

    Something like:


    void main (void)
      /* Stuff here */
      spawnl(P_NOWAITO, "app2.exe", 2);


    void main (int argc, char *argv[])
      /* Wait required number of seconds */

      /* Delete the file */
    LVL 86

    Accepted Solution

    And how do you get rid of the batch file ? ;-)

    Module Name:    DeleteMe.cpp Author:        Jeffrey Richter
    #include <Windows.h>
    #include <stdlib.h>
    #include <tchar.h>


    int WINAPI WinMain(HINSTANCE h, HINSTANCE h2, LPSTR psz, int n) {

       // Is this the original EXE ther Clone?
       // The original EXE receives one argument,
       // the clone more.
       if (__argc == 1) {

          // Original EXE: Start the Clone.

          // Copy EXE to the temporary directory
          TCHAR szPathOrig[_MAX_PATH], szPathClone[_MAX_PATH];
          GetModuleFileName(NULL, szPathOrig, _MAX_PATH);
          GetTempPath(_MAX_PATH, szPathClone);
          GetTempFileName(szPathClone, __TEXT("Del"), 0, szPathClone);
          CopyFile(szPathOrig, szPathClone, FALSE);

          // Open the Clone using FILE_FLAG_DELETE_ON_CLOSE
          HANDLE hfile = CreateFile(szPathClone, 0, FILE_SHARE_READ, NULL,

          // Start the Clone. Pass the PID and the fully qualified path
          // of the original file
          TCHAR szCmdLine[512];
          HANDLE hProcessOrig = OpenProcess(SYNCHRONIZE, TRUE,
          wsprintf(szCmdLine, __TEXT("%s %d \"%s\""), szPathClone,
                   hProcessOrig, szPathOrig);
          STARTUPINFO si;
          ZeroMemory(&si, sizeof(si));
          si.cb = sizeof(si);
          CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, 0,
                        NULL, NULL, &si, &pi);

          // now the original process may terminate
          } else {

          // Clone-EXE: Delete the  original EXE after termination
          HANDLE hProcessOrig = (HANDLE) _ttoi(__targv[1]);
          WaitForSingleObject(hProcessOrig, INFINITE);

    Code taken from - to be precise, the scoop is how to how to write an unsetup program that was able to delete itself.
    LVL 53

    Assisted Solution

    A few thoughts :

    1) Use a loader exe to execute the program. Once the program finishes, the loader can remove it. But there is still the loader that remains in that case

    2) place the application in a temporary directory (that gets cleaned out regularly) ... the EXE won't be deleted immediately after execution, but it will be at some point in the future (depending on your goal, this might be ok)

    3) Under Windows, you can mark a file for deletion when the system boots the next time :


    An article that is related :

    LVL 17

    Assisted Solution

    Need to figure out the time in a minute's time (let's say it is 10:00) and run the following in an at exit function:

       system("at 10:00 erase c:\\bin\\run.exe");

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now