Page 1 of 1

Quine in Batch

Posted: 26 Jun 2014 10:27
by JWinslow23
I wanted to be able to make a quine in Batch. It was surprising how easily I made one:

Code: Select all

@for /f "delims=" %%g in (%~nx0)do @echo %%g&@pause>nul
This quine has been tested to work on Windows 7 (please test for compatibility with older OSes), and comes in at a petite 55 bytes in one line of code. Of course, this kinda cheats with the FOR /F command, but it is a quine nonetheless.
Save it as any filename; it doesn't matter. It auto-detects the filename with %~nx0 .

Please, tell me what you think of it, tell me if it worked on your OS, and notify me of any smaller quine possible.

Re: Quine in Batch

Posted: 26 Jun 2014 11:00
by penpen
Sorry, but a quine is forbidden to read any (essential) external data (including reading a file).
So your source above is not a quine.

penpen

Re: Quine in Batch

Posted: 26 Jun 2014 11:09
by JWinslow23
I admitted to cheating. :P

OK, I'll make another one.

Re: Quine in Batch

Posted: 26 Jun 2014 14:07
by JWinslow23
Here is my quine (made the only way I know how, so yeah, I know it's big):

Code: Select all

@echo off&setlocal enabledelayedexpansion&set h=0&for %%g in (60,110,117,108,32,115,101,116,32,47,112,61,64,101,99,104,111,32,111,102,102,94,38,115,101,116,108,111,99,97,108,32,101,110,97,98,108,101,100,101,108,97,121,101,100,101,120,112,97,110,115,105,111,110,94,38,115,101,116,32,104,61,48,94,38,102,111,114,32,47,108,32,37,37,37,37,103,32,105,110,32,40,10,102,111,114,32,47,108,32,37,37,103,32,105,110,32,40,48,44,49,44,51,57,51,41,100,111,32,40,60,110,117,108,32,115,101,116,32,47,112,61,33,119,37,37,103,33,38,105,102,32,37,37,103,61,61,51,57,51,32,40,115,101,116,32,34,97,61,41,34,41,32,101,108,115,101,32,40,115,101,116,32,34,97,61,44,34,41,10,60,110,117,108,32,115,101,116,32,47,112,61,33,97,33,41,10,101,99,104,111,32,100,111,32,115,101,116,32,119,94,94,33,104,94,94,33,61,37,37,37,37,103,94,38,115,101,116,47,97,104,43,61,49,10,115,101,116,32,97,61,10,102,111,114,32,47,108,32,37,37,103,32,105,110,32,40,48,44,49,44,51,57,51,41,100,111,32,40,99,109,100,32,47,99,32,101,120,105,116,32,47,98,32,33,119,37,37,103,33,38,105,102,32,33,119,37,37,103,33,61,61,49,48,32,101,99,104,111,32,33,97,33,38,115,101,116,32,97,61,10,105,102,32,33,119,37,37,103,33,61,61,51,50,32,115,101,116,32,34,97,61,33,97,33,32,34,10,105,102,32,33,119,37,37,103,33,32,103,116,114,32,51,50,32,115,101,116,32,34,97,61,33,97,33,33,61,69,120,105,116,67,111,100,101,65,115,99,105,105,33,34,41,10,112,97,117,115,101,62,110,117,108,10)do set w!h!=%%g&set/ah+=1
<nul set /p=@echo off^&setlocal enabledelayedexpansion^&set h=0^&for /l %%%%g in (
for /l %%g in (0,1,393)do (<nul set /p=!w%%g!&if %%g==393 (set "a=)") else (set "a=,")
<nul set /p=!a!)
echo do set w^^!h^^!=%%%%g^&set/ah+=1
set a=
for /l %%g in (0,1,393)do (cmd /c exit /b !w%%g!&if !w%%g!==10 echo !a!&set a=
if !w%%g!==32 set "a=!a! "
if !w%%g! gtr 32 set "a=!a!!=ExitCodeAscii!")
pause>nul

Re: Quine in Batch

Posted: 27 Jun 2014 01:43
by Squashman
Did you happen to search the dostips.com website or even Google search for this topic at all.

Re: Quine in Batch

Posted: 27 Jun 2014 07:47
by JWinslow23
Squashman wrote:Did you happen to search the dostips.com website or even Google search for this topic at all.


Only for a php quine that took a similar format. I believe that my Batch version of the quine, while not the best, is 100% original.

Re: Quine in Batch

Posted: 27 Jun 2014 10:41
by ShadowThief

Re: Quine in Batch

Posted: 27 Jun 2014 11:06
by JWinslow23
Those quines follow an entirely different format. :P

Re: Quine in Batch

Posted: 28 Jun 2014 15:48
by penpen
Sidenote (don't know a better place to post this):
You could shorten the 109 bytes BatchQuine.bat located at
http://www.dostips.com/DtTipsQuine.php
by resorting the lines (and adjusting the T variable):

Code: Select all

Set E=%%
Set "T=echo Set E=!E!!E!&Echo Set "T=!T!"&Echo Cmd/V:On/C"!E!T!E!""
Cmd/V:On/C"%T%"
Result: 96 bytes quine.

penpen

Re: Quine in Batch

Posted: 28 Jun 2014 17:12
by Aacini
Although I'm not a quine expert, I think that specifying conditions previous to run a quine in a Batch file is cheating. In particular, I refer to this line that appear in more than half the Batch quines at that link:

Quine Collection wrote:Run the Quine using the following command in order to make sure Delayed Expansion is disabled and ECHO is OFF:
C:> Cmd /Q /V:Off /C BatchQuine.bat

If this trick is valid, then this is my version of a 6 bytes quine:

-----------------------------------------------------------

Create the Quine.bat file with this Batch file:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set LF=^


rem Two previous empty lines are required
echo !LF!rem> Quine.bat


Run the Quine using the following two commands in order to make sure Prompt is $H:

Code: Select all

C:> prompt=$H
C:> Quine.bat


Example:

Code: Select all

C:> prompt=$H

Quine.bat > Quine.txt

prompt=

C:> type Quine.bat

rem

C:> type Quine.txt

rem

C:>


... I told you: this is cheating! :wink:

Antonio

Re: Quine in Batch

Posted: 28 Jun 2014 19:01
by penpen
It is allowed to use (no cheating):
Cmd /Q /V:Off /C BatchQuine.bat
There is no external data, it is one of the states of the command line interpreter (cmd.exe).
It is allowed to set the ionterpreters options for interpreted languages;
This is similar to automatically import dll's by compiler options, which is allowed, too.
(If you only want to allow the default options, then just add 6 @'s; delayed expansion is OFF by default.)

Your batch is no quine, as for example the output contains backspace characters:

Code: Select all

Z:\>fc /B Quine.bat Quine.txt
Vergleichen der Dateien Quine.bat und QUINE.TXT
00000000: 0A 0D
00000001: 72 0A
00000002: 65 08
00000003: 6D 20
00000004: 0D 08
00000005: 0A 72
FC: QUINE.TXT ist länger als Quine.bat
Hexadecimal representations using Win xp home 32 bit:
Quine.bat wrote:0A 72 65 6D 0D 0A
Quine.txt wrote:0D 0A 08 20 08 72 65 6D 0D 0A


You could fix it in this way (hex: 0D 0A 20 72 65 6D 0D 0A):
Quine.bat wrote:
rem
and start it with

Code: Select all

Z:\>set "prompt= "


 Quine.bat > Quine.bat.txt

 fc Quine.bat Quine.txt
Vergleichen der Dateien Quine.bat und QUINE.TXT
FC: Keine Unterschiede gefunden

But this is also no quine, as it uses external data (stored within the prompt environment variable).

penpen

Edited: Added further information, to make it more clear.