Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows batch: Can't echo ASCII art with ._|_

I made ASCII art and I'm trying to have it shown when a .bat file is open. This is the (overwhelmingly basic) code I have. I've checked online and can't really find anything that could help me. Any suggestions?

@echo off
echo    __  __    _______    _____     _____     __  __
echo   / /_/ /   / ___  /   /  _  /   /  _  /   / /_/  /
echo  / __  /   / /__/ /   / ____/   /  ___/     \   /
echo /_/ /_/   /_/  /_/   /_/       /_/          /__/
echo    _____          _____     _______  __  __
echo   /  _ /  ____   /  _   /  / ___  / / /_/  /
echo  /  _ /  /___/  /  _/  /  / /__/ /  \    /
echo /____/         /______/  /_/  /_/    /__/
pause
echo  
echo     #      #        #      #         #
echo .__|_|____|_|______|_|____|_|_______|_|____.
echo ############################################
echo |                                          |
echo ############################################
echo ############################################
echo |__________________________________________|
pause

I can already see myself crying because I didn't notice a tiny mistake.

It crashes on this line: echo .__|_|____|_|______|_|____|_|_______|_|____.

like image 222
iPhynx Avatar asked Sep 26 '22 03:09

iPhynx


1 Answers

ASCII art together with echo commands could be a bit tricky, because there are several characters that are treated especially by the command processor.

So to correct your code, it should look like this:

@echo off
echo    __  __    _______    _____     _____     __  __
echo   / /_/ /   / ___  /   /  _  /   /  _  /   / /_/  /
echo  / __  /   / /__/ /   / ____/   /  ___/     \   /
echo /_/ /_/   /_/  /_/   /_/       /_/          /__/
echo    _____          _____     _______  __  __
echo   /  _ /  ____   /  _   /  / ___  / / /_/  /
echo  /  _ /  /___/  /  _/  /  / /__/ /  \    /
echo /____/         /______/  /_/  /_/    /__/
pause
echo(
echo     #      #        #      #         #      
echo .__^|_^|____^|_^|______^|_^|____^|_^|_______^|_^|____.
echo ############################################
echo ^|                                          ^|
echo ############################################
echo ############################################
echo ^|__________________________________________^|
pause

The | character you are using is such a special character: it constitutes a so-called "pipe", which allows to feed the output of a command into another one (for instance: type "file.txt" | find "Hello"). In your code, _ is considered a command, but this does not exist, therefore it fails (it tries to feed the output of echo .__, namely .__, into a command _). To overcome this you need to escape every single | like ^| (the ^ makes the following character to lose its special meaning).


There are more special characters, like &, (, ), <, >,..., and also the ^ itself. So if I needed to write such a code, I would export the ASCII art text into external files and display their contents using a type command, like the following:

congrats.txt:

   __  __    _______    _____     _____     __  __
  / /_/ /   / ___  /   /  _  /   /  _  /   / /_/  /
 / __  /   / /__/ /   / ____/   /  ___/     \   /
/_/ /_/   /_/  /_/   /_/       /_/          /__/
   _____          _____     _______  __  __
  /  _ /  ____   /  _   /  / ___  / / /_/  /
 /  _ /  /___/  /  _/  /  / /__/ /  \    /
/____/         /______/  /_/  /_/    /__/

cake.txt:

    #      #        #      #         #      
.__|_|____|_|______|_|____|_|_______|_|____.
############################################
|                                          |
############################################
############################################
|__________________________________________|

batch script:

@echo off
type "%~dp0congrats.txt"
> nul pause
echo(
type "%~dp0cake.txt"
> nul pause

So you would not ever run into problems with any text characters. The script above expects the *.txt files to be located in the same directory as the script itself.


However, if you do want to have everything within the batch file, I have the following options in mind:

You could define array-like variables and use set to display them, supposing these variable names are not used elsewhere:

@echo off

set "CONGRATS_00=.   __  __    _______    _____     _____     __  __"
set "CONGRATS_01=.  / /_/ /   / ___  /   /  _  /   /  _  /   / /_/  /"
set "CONGRATS_02=. / __  /   / /__/ /   / ____/   /  ___/     \   /"
set "CONGRATS_03=./_/ /_/   /_/  /_/   /_/       /_/          /__/"
set "CONGRATS_04=.   _____          _____     _______  __  __"
set "CONGRATS_05=.  /  _ /  ____   /  _   /  / ___  / / /_/  /"
set "CONGRATS_06=. /  _ /  /___/  /  _/  /  / /__/ /  \    /"
set "CONGRATS_07=./____/         /______/  /_/  /_/    /__/"

set "CAKE_00=.    #      #        #      #         #      "
set "CAKE_01=..__|_|____|_|______|_|____|_|_______|_|____."
set "CAKE_02=.############################################"
set "CAKE_03=.|                                          |"
set "CAKE_04=.############################################"
set "CAKE_05=.############################################"
set "CAKE_06=.|__________________________________________|"

call :SUB "CONGRATS"
> nul pause
echo(
call :SUB "CAKE"
> nul pause
exit /B

:SUB "name"
for /F "delims=" %%V in ('
    set "%~1_"
') do (
    set "VALUE=%%V"
    setlocal EnableDelayedExpansion
    echo(!VALUE:*.=!
    endlocal
)
exit /B

Alternatively, if you do not want to risk any variable values to be overwritten by some other code portions, you could embed the ASCII art within the batch file directly and read it using a for /F loop:

@echo off
goto :BEGIN

:::congrats.   __  __    _______    _____     _____     __  __
:::congrats.  / /_/ /   / ___  /   /  _  /   /  _  /   / /_/  /
:::congrats. / __  /   / /__/ /   / ____/   /  ___/     \   /
:::congrats./_/ /_/   /_/  /_/   /_/       /_/          /__/
:::congrats.   _____          _____     _______  __  __
:::congrats.  /  _ /  ____   /  _   /  / ___  / / /_/  /
:::congrats. /  _ /  /___/  /  _/  /  / /__/ /  \    /
:::congrats./____/         /______/  /_/  /_/    /__/

:::cake.    #      #        #      #         #      
:::cake..__|_|____|_|______|_|____|_|_______|_|____.
:::cake.############################################
:::cake.|                                          |
:::cake.############################################
:::cake.############################################
:::cake.|__________________________________________|

:BEGIN
set "BATCH=%~f0"
call :SUB "congrats"
> nul pause
echo(
call :SUB "cake"
> nul pause
exit /B

:SUB "name"
for /F "delims=" %%L in ('
    findstr /L /I /B /C:":::%~1." "%BATCH%"
') do (
    set "LINE=%%L"
    setlocal EnableDelayedExpansion
    echo(!LINE:*.=!
    endlocal
)
exit /B
like image 61
aschipfl Avatar answered Sep 29 '22 07:09

aschipfl