1

When I run this code I get an error but then I can't see it because the batch file closes to fast. Anyone know how to fix this?

When I run this all I can see before it closes are a few lines e.g _ and a black batch file screen closing.

Cool841
  • 97
  • 1
  • 1
  • 8
  • Instead of an image you should provide the code so we can try to figure out the problem, I can imagine thant nobody is motivated enough to re-type the code from your picture especially since we do not see the end of the lines – KIC Jul 15 '15 at 13:17
  • @KIC Well I guess I'm sorry but when I tried typing in the code the website screwed it up so it was just a bunch of lines :( – Cool841 Jul 15 '15 at 13:29
  • 1
    To create code blocks on Stack Exchange, indent with a tab or four spaces. [See this formatting help page](http://stackoverflow.com/editing-help). – rojo Jul 15 '15 at 13:39

1 Answers1

3

The | character is called a pipe. It separates commands, pushing the output of one command into the input of another. Because they have special meaning in batch scripts, you can't just echo them without escaping them with a caret, or setting them to a variable and retrieving with delayed expansion.

Here's a simple example of escaping:

@echo off
setlocal

echo                      _______             _____    _______              _______
echo \                /  ^|          ^|        /     \  /       \  ^|\    /^|  ^|
echo  \              /   ^|          ^|        ^|        ^|       ^|  ^| \  / ^|  ^|
echo   \            /    ^|_______   ^|        ^|        ^|       ^|  ^|  \/  ^|  ^|_______
echo    \    /\    /     ^|          ^|        ^|        ^|       ^|  ^|      ^|  ^|
echo     \  /  \  /      ^|          ^|        ^|        ^|       ^|  ^|      ^|  ^|
echo      \/    \/       ^|_______   ^|______  \_____/  \_______/  ^|      ^|  ^|_______
pause

Here's an example with delayed expansion:

@echo off
setlocal enabledelayedexpansion
set "I=|"

echo                      _______             _____    _______              _______
echo \                /  !I!          !I!        /     \  /       \  !I!\    /!I!  !I!
echo  \              /   !I!          !I!        !I!        !I!       !I!  !I! \  / !I!  !I!
echo   \            /    !I!_______   !I!        !I!        !I!       !I!  !I!  \/  !I!  !I!_______
echo    \    /\    /     !I!          !I!        !I!        !I!       !I!  !I!      !I!  !I!
echo     \  /  \  /      !I!          !I!        !I!        !I!       !I!  !I!      !I!  !I!
echo      \/    \/       !I!_______   !I!______  \_____/  \_______/  !I!      !I!  !I!_______
pause

As an advanced exercise, if you'd like to keep the figlet "WELCOME" text readable within the source code, you could employ a batch script heredoc:

@echo off
setlocal

call :heredoc welcome && goto end_welcome
                     _______             _____    _______              _______
\                /  |          |        /     \  /       \  |\    /|  |
 \              /   |          |        |        |       |  | \  / |  |
  \            /    |_______   |        |        |       |  |  \/  |  |_______
   \    /\    /     |          |        |        |       |  |      |  |
    \  /  \  /      |          |        |        |       |  |      |  |
     \/    \/       |_______   |______  \_____/  \_______/  |      |  |_______
:end_welcome
pause

goto :EOF

rem // https://stackoverflow.com/a/15032476/1683264
:heredoc <uniqueIDX>
setlocal enabledelayedexpansion
set go=
for /f "delims=" %%A in ('findstr /n "^" "%~f0"') do (
    set "line=%%A" && set "line=!line:*:=!"
    if defined go (if #!line:~1!==#!go::=! (goto :EOF) else echo(!line!)
    if "!line:~0,13!"=="call :heredoc" (
        for /f "tokens=3 delims=>^ " %%i in ("!line!") do (
            if #%%i==#%1 (
                for /f "tokens=2 delims=&" %%I in ("!line!") do (
                    for /f "tokens=2" %%x in ("%%I") do set "go=%%x"
                )
            )
        )
    )
)
goto :EOF

By the way, you should keep in mind that command prompt windows are 80 columns by default. What you have screenshot looks like it'll be too wide for a typical console window.

Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101
  • Thank you so much and btw I was using the command "mode 1000" before. I was just wondering what is a heredoc? (sorry im not that great at programming) – Cool841 Jul 15 '15 at 13:59
  • 1
    It's a section of source code that's treated as if it were a separate file. Basically, the batch parser skips over the stuff between `goto end_welcome` and `:end_welcome`, but the `:heredoc` function reads that section as though it were reading from an external text file. See the [heredoc Wikipedia entry](https://en.wikipedia.org/wiki/Here_document) for a full explanation. – rojo Jul 15 '15 at 14:08