-1
\$\begingroup\$

This question already has an answer here:

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.

\$\endgroup\$

marked as duplicate by user62131 May 7 '17 at 2:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    \$\begingroup\$ @ArlaudPierre Well, I had to pick one, and (unfortunately) I have more experience with it. \$\endgroup\$ – Cruncher Jan 3 '14 at 16:15
  • 3
    \$\begingroup\$ Must the program delete only itself, or can it nuke, say, lots of other things as well? \$\endgroup\$ – kojiro Jan 4 '14 at 1:50
  • 2
    \$\begingroup\$ @ArlaudPierre Also, notably, windows does not allow the deletion of a program while it is running. This increases the challenge level a bit. \$\endgroup\$ – Tyzoid Jan 4 '14 at 4:28
  • 6
    \$\begingroup\$ Linux is totally a niche OS, where main(int a,char**b){unlink(*b);} is all you need. (Same goes for OSX.) \$\endgroup\$ – fluffy Jan 4 '14 at 4:33
  • 3
    \$\begingroup\$ @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. \$\endgroup\$ – Gareth Jan 25 '14 at 10:18
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ "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. \$\endgroup\$ – nitro2k01 Jan 4 '14 at 19:20
  • 1
    \$\begingroup\$ @nitro2k01: Added two magic bytes at the beginning. \$\endgroup\$ – Konrad Borowski Jan 4 '14 at 19:22
  • 1
    \$\begingroup\$ "Win16 executable, except I don't know what it will do" I love it, this needs more votes \$\endgroup\$ – cat Mar 4 '16 at 12:17
2
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ The final count goes by executable size, not source size \$\endgroup\$ – Tyzoid Jan 4 '14 at 3:35
  • \$\begingroup\$ 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. \$\endgroup\$ – Johannes Kuhn Jan 4 '14 at 3:50
  • \$\begingroup\$ codegolf.stackexchange.com/a/123067/29325 \$\endgroup\$ – sergiol May 27 '17 at 8:36
1
\$\begingroup\$

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.

\$\endgroup\$
  • 4
    \$\begingroup\$ rm on windows?? \$\endgroup\$ – SztupY Jan 4 '14 at 3:12
  • \$\begingroup\$ @SztupY Sorry, I was not thinking :P \$\endgroup\$ – Tyzoid Jan 4 '14 at 3:30
0
\$\begingroup\$

Python (59 bytes)

import os,sys;os.remove(sys._getframe().f_code.co_filename)
\$\endgroup\$
0
\$\begingroup\$

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
)
\$\endgroup\$
-1
\$\begingroup\$

HTML + JS (33)

<body onload=document.write('')>
\$\endgroup\$
  • 4
    \$\begingroup\$ “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. \$\endgroup\$ – Ry- Jan 31 '14 at 3:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.