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.
7 Answers 7
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.
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.
-
\$\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– nitro2k012014年01月04日 19:20:58 +00:00Commented Jan 4, 2014 at 19:20 -
1\$\begingroup\$ @nitro2k01: Added two magic bytes at the beginning. \$\endgroup\$null– null2014年01月04日 19:22:17 +00:00Commented 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\$cat– cat2016年03月04日 12:17:55 +00:00Commented Mar 4, 2016 at 12:17
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.
-
2\$\begingroup\$ The final count goes by executable size, not source size \$\endgroup\$Tyzoid– Tyzoid2014年01月04日 03:35:34 +00:00Commented 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\$Johannes Kuhn– Johannes Kuhn2014年01月04日 03:50:08 +00:00Commented Jan 4, 2014 at 3:50
-
\$\begingroup\$ codegolf.stackexchange.com/a/123067/29325 \$\endgroup\$sergiol– sergiol2017年05月27日 08:36:16 +00:00Commented May 27, 2017 at 8:36
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.
Python (59 bytes)
import os,sys;os.remove(sys._getframe().f_code.co_filename)
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
)
HTML + JS (33)
<body onload=document.write('')>
-
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-– Ry-2014年01月31日 03:47:47 +00:00Commented Jan 31, 2014 at 3:47
main(int a,char**b){unlink(*b);}
is all you need. (Same goes for OSX.) \$\endgroup\$