Windows批处理:无法使用._ | _回显ASCII艺术作品 [英] Windows batch: Can't echo ASCII art with ._|_

查看:66
本文介绍了Windows批处理:无法使用._ | _回显ASCII艺术作品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了ASCII艺术作品,并试图在打开.bat文件时显示它。这是我拥有的(绝大多数)代码。我已经在线检查过,找不到任何可以帮助我的东西。有建议吗?

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.

此行崩溃: echo .__ | _ | ____ | _ | ______ | _ | ____ | _ | _______ | _ | ____ 。

推荐答案

ASCII艺术和 echo 命令可能会有些棘手,因为命令处理器会特别处理几个字符。

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

| 字符您使用的是一个特殊字符:它构成了一个所谓的管道,它允许将命令的输出提供给另一个命令(例如: type file.txt | find您好 )。在您的代码中, _ 被认为是命令,但是它不存在,因此会失败(它尝试提供 echo .__ <的输出。 / code>,即 .__ 转换为命令 _ )。为了克服这个问题,您需要转义每个 | ,例如 ^ | ^ 使以下字符失去其特殊含义)。

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).

还有更多特殊字符,例如& < > ,...以及 ^ 本身。因此,如果需要编写这样的代码,可以将ASCII美术文本导出到外部文件中,并使用 type 命令显示其内容,如下所示:

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:

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



cake.txt



cake.txt:

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



批处理脚本:



batch script:

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

因此,您永远不会遇到任何文字字符的问题,上面的脚本希望 *。txt 文件与以下文件位于同一目录中

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:

您可以定义类似数组的变量,并使用 set 来显示它们,假设这些变量名不是

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

或者,如果您不希望任何变量值被其他代码部分覆盖,则可以将ASCII艺术作品直接嵌入批处理文件中,并使用 for / F 循环:

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

这篇关于Windows批处理:无法使用._ | _回显ASCII艺术作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆