Write a program that deletes itself

-1

2

For this code golf challenge, you must produce a .exe runnable on windows. After running this .exe from any path with any name it must be deleted after execution.

My reason for limiting this to windows is something like this may be significantly easier if you have some niche OS.

Language(and code) used to produce the .exe is irrelevant, the file size of the executable is what will be measured for this. But still post the code used to produce it, along with the resulting file size.

This is code golf, but again the goal is the smallest executable file.

Cruncher

Posted 2014-01-03T15:54:48.300

Reputation: 2 135

Question was closed 2017-05-07T02:15:23.250

1You give a reason for limiting to one only OS, but certainly not a reason to limit to windows. – Pierre Arlaud – 2014-01-03T16:11:46.903

2@ArlaudPierre Well, I had to pick one, and (unfortunately) I have more experience with it. – Cruncher – 2014-01-03T16:15:58.623

3Must the program delete only itself, or can it nuke, say, lots of other things as well? – kojiro – 2014-01-04T01:50:48.597

@kojiro I think the point is to not have it be malicious, and to be able to be tested without fear of breaking a system. – Tyzoid – 2014-01-04T01:51:31.927

2@ArlaudPierre Also, notably, windows does not allow the deletion of a program while it is running. This increases the challenge level a bit. – Tyzoid – 2014-01-04T04:28:25.710

6Linux is totally a niche OS, where main(int a,char**b){unlink(*b);} is all you need. (Same goes for OSX.) – fluffy – 2014-01-04T04:33:34.440

Is it allowed to strip and upx the exe? – o0'. – 2014-01-04T12:10:03.180

1@JanDvorak Now that's strange - you shouldn't vote an existing question as duplicate for a newer one. That's not in the spirit of this site. – Howard – 2014-01-25T09:59:27.197

@Howard why not? It's perfecly legitimate on SO – John Dvorak – 2014-01-25T10:00:20.000

3@JanDvorak This is a lot like copying someone's song/book/movie, changing a couple of words and the title, and then suing them for copying you. At the very least this should go through meta first to discuss this course of action, in my opinion. – Gareth – 2014-01-25T10:18:37.653

@Gareth as if that didn't happen before. – Johannes Kuhn – 2014-01-25T10:40:22.883

@Gareth, no, its like writing a new book that is way better than the old book, and then publisher stops publishing the old book because nobody buys it anymore. – AJMansfield – 2014-01-31T16:17:33.980

Answers

5

Assembly, 4608 bytes

The source code for the assembly program, using nasm syntax, is as follows:

BITS 32

GLOBAL _main
EXTERN _system

SECTION .data
cmd:    db      'echo>).bat start cmd /c del ).bat '
cmdarg: times 128 db 0

SECTION .text

_main:
        mov     eax, [esp + 8]
        mov     esi, [eax]
        mov     edi, cmdarg
copy:   lodsb
        or      al, al
        stosb
        jnz     copy
        push    cmd
        call    _system
        add     dword [esp], 5
        call    _system
        pop     edx
        ret

I built the executable using the following commands:

nasm -f win32 selfdel.asm
gcc -Wall -s -o selfdel.exe selfdel.obj

(I'm curious if the size of the executable would be different if MS's linker were to be used in place of gcc's, but I don't have access to MS development tools.)

Hopefully the code is straightforward enough to not need much explanation. Basically the program creates a batch file that deletes the program (as well as itself) after the program has exited.

breadbox

Posted 2014-01-03T15:54:48.300

Reputation: 6 893

4

Perl (11 bytes)

MZ+unlink$0

Save this as a.exe, or something, and run it with Perl. And yes, this is Win16 executable, except I don't know what it will do.

Konrad Borowski

Posted 2014-01-03T15:54:48.300

Reputation: 11 185

1"Win16 executable, except I don't know what it will do" I love it, this needs more votes – cat – 2016-03-04T12:17:55.360

"And yes, this is Win16 executable" I do not believe this is true. I believe a Win16 binary needs to be an NE executable, in turn a subformat of an MZ executable, which needs to start with the two magic bytes MZ. – nitro2k01 – 2014-01-04T19:20:58.917

1@nitro2k01: Added two magic bytes at the beginning. – Konrad Borowski – 2014-01-04T19:22:17.483

2

Tcl, 48

exec cmd /c "ping ::1&&del [file na [info n]]" &

Use a tclsh basekit to create the executable.

Note: a process can not delete its own executable, because it is locked when the process is running. So your only option is to get some other process to delete your executable when it stops running.

Johannes Kuhn

Posted 2014-01-03T15:54:48.300

Reputation: 7 122

https://codegolf.stackexchange.com/a/123067/29325 – sergiol – 2017-05-27T08:36:16.830

2The final count goes by executable size, not source size – Tyzoid – 2014-01-04T03:35:34.960

I think I should use C for that. A simple system call should do it. The entire Tcl runtime is a little bit big with 2.3MB. – Johannes Kuhn – 2014-01-04T03:50:08.337

1

C - 857 byte source, 18,432 byte compilation

#include <stdlib.h>

size_t stringlen(const char *);
char * stringcat(char *, const char *);

main(int argc, char *argv[])
{
    if(argc > 0)
    {
        char *str = calloc(stringlen(argv[0])+11, sizeof(char));
        stringcat(str, "start del ");
        stringcat(str, argv[0]);
        system(str);
    }
}

/* Simple implementation of strcat to avoid including string.h */
char * stringcat(char *dest, const char *source)
{
    size_t i=0,j=0;
    while(dest[i] != '\0')
    {
        ++i;
    }

    while(source[j] != '\0')
    {
        dest[i+j] = source[j];
        ++j;
    }
    dest[i+j] = '\0';
    return dest;
}

/*Simple implementation of strlen to avoid including string.h */
size_t stringlen(const char *str)
{
    size_t i=0;
    while(str[i] != '\0')
    {
        ++i;
    }
    return i;
}

Compiled with:

gcc self-delete.c -pedantic -s -o self-delete.exe

It is now tested and works perfectly.

Tyzoid

Posted 2014-01-03T15:54:48.300

Reputation: 692

4rm on windows?? – SztupY – 2014-01-04T03:12:14.127

@SztupY Sorry, I was not thinking :P – Tyzoid – 2014-01-04T03:30:55.593

0

Python (59 bytes)

import os,sys;os.remove(sys._getframe().f_code.co_filename)

Thijs van Dien

Posted 2014-01-03T15:54:48.300

Reputation: 101

0

Assembly - 1548 bytes

Executes a delayed system command to find and delete itself

EXTERN system
IMPORT system msvcr100.dll

SECTION .text USE32
GLOBAL main
main:
    push string
    call [system]
    ret

string:
    db "start cmd /C ", 34, "timeout /t 3&for /f %a in ('dir /b *.exe') do (find ", 34, "someuniquestring134123asdfasd41324", 34,  " %a &if not errorlevel 1 del %a)", 34, 0

Compiled with:

nasm.exe -f obj filename.asm
alink.exe -oPE -entry main filename.obj

The executed script:

timeout /t 3
for /f %a in ('dir /b *.exe') do (
    find "someuniquestring134123asdfasd41324", %a
    if not errorlevel 1 del %a
)

Robert Sørlie

Posted 2014-01-03T15:54:48.300

Reputation: 1 036

-1

HTML + JS (33)

<body onload=document.write('')>

xem

Posted 2014-01-03T15:54:48.300

Reputation: 5 523

4“you must produce a .exe runnable on windows. After running this .exe from any path with any name it must be deleted after execution.” Not even close. – Ry- – 2014-01-31T03:47:47.133