-2
\$\begingroup\$

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.

asked Jan 3, 2014 at 15:54
\$\endgroup\$
12
  • 2
    \$\begingroup\$ @ArlaudPierre Well, I had to pick one, and (unfortunately) I have more experience with it. \$\endgroup\$ Commented Jan 3, 2014 at 16:15
  • 3
    \$\begingroup\$ Must the program delete only itself, or can it nuke, say, lots of other things as well? \$\endgroup\$ Commented Jan 4, 2014 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\$ Commented Jan 4, 2014 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\$ Commented Jan 4, 2014 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\$ Commented Jan 25, 2014 at 10:18

7 Answers 7

5
\$\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.

answered Jan 4, 2014 at 12:03
\$\endgroup\$
4
\$\begingroup\$

Perl (11 bytes)

MZ+unlink0ドル

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.

answered Jan 4, 2014 at 12:23
\$\endgroup\$
3
  • \$\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\$ Commented Jan 4, 2014 at 19:20
  • 1
    \$\begingroup\$ @nitro2k01: Added two magic bytes at the beginning. \$\endgroup\$ Commented Jan 4, 2014 at 19:22
  • 1
    \$\begingroup\$ "Win16 executable, except I don't know what it will do" I love it, this needs more votes \$\endgroup\$ Commented Mar 4, 2016 at 12:17
3
\$\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.

Ry-
5,3931 gold badge25 silver badges34 bronze badges
answered Jan 4, 2014 at 2:03
\$\endgroup\$
3
  • 2
    \$\begingroup\$ The final count goes by executable size, not source size \$\endgroup\$ Commented Jan 4, 2014 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\$ Commented Jan 4, 2014 at 3:50
  • \$\begingroup\$ codegolf.stackexchange.com/a/123067/29325 \$\endgroup\$ Commented May 27, 2017 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.

answered Jan 4, 2014 at 1:43
\$\endgroup\$
2
  • 4
    \$\begingroup\$ rm on windows?? \$\endgroup\$ Commented Jan 4, 2014 at 3:12
  • \$\begingroup\$ @SztupY Sorry, I was not thinking :P \$\endgroup\$ Commented Jan 4, 2014 at 3:30
0
\$\begingroup\$

Python (59 bytes)

import os,sys;os.remove(sys._getframe().f_code.co_filename)
answered Jan 4, 2014 at 21:51
\$\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
)
answered Jan 11, 2014 at 22:32
\$\endgroup\$
-1
\$\begingroup\$

HTML + JS (33)

<body onload=document.write('')>
answered Jan 5, 2014 at 12:12
\$\endgroup\$
1
  • 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\$ Commented Jan 31, 2014 at 3:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.