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

倾然丶 夕夏残阳落幕 提交于 2020-11-28 01:51:37

问题


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 .__|_|____|_|______|_|____|_|_______|_|____.


回答1:


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



回答2:


What I would do is use the

rem

command to draw out each line producing ASCII art and making it more fancy or you can always do the

type

command and refrence a Python file with the text inside.



来源:https://stackoverflow.com/questions/35855062/windows-batch-cant-echo-ascii-art-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!