Home > Enterprise >  How to store ASCII Art into a variable in a batch file?
How to store ASCII Art into a variable in a batch file?

Time:12-17

So I'm trying to create a simple ROCK-PAPER-SCISSORS game wherein I want to display ASCII artwork for the individual elements. It would be great if I could store them into variables in some way for the further logic to follow up & use those variables.

   ....
   .....
     .......
      ........
         ........
            ........
               ........           .........
                 ..................       ..
                   ................       ..
                     .........   ....  ....
                      ..     ....
                      ..        ...
                       ...         ...
                       ...         ..
                          ...........

This is the piece of art & when I try to store it into a variable like:

set /p scissors = " the above art "

I get errors.

Does someone know how to resolve this?

CodePudding user response:

Another of may ways to approach the goal is to use findstr to read data embedded in your file. The findstr command may be assigned to a variable allowing it to be used repeatedly for different elements:

@echo off

Setlocal EnableDelayedExpansion

 Mode 1000
 Set "Scissors="
 Setlocal EnableDelayedExpansion
 Set "RPS=For /f "tokens=2 Delims=RPS123" %%G in ('%SystemRoot%\System32\Findstr.exe /bl ":RPS#" "%~f0"')Do Echo(%%G"
 Choice /C:RPS
 %RPS:#=!Errorlevel!%

:RPS1             
:RPS1                
:RPS1                .. ... .. .. . 
:RPS1            . ... ... .. . .. .
:RPS1          . .. .  ... ... ......
:RPS1         .........   ..... ......
:RPS1        . ......   ..... ........ .
:RPS1         .........   ..... ......
:RPS1        .. ..  ................ .        
:RPS1          .. .....  .. .. .......
:RPS1            . . . ... . .. ... .
:RPS1             . ... .. . . . .
:RPS1              ...   ...   ..
:RPS1       
:RPS1          


:RPS2      . 
:RPS2     .  .
:RPS2    .     .
:RPS2   .        .
:RPS2  .           .
:RPS2 .              .
:RPS2.                 .
:RPS2  .                 .
:RPS2    .                 . 
:RPS2      .              .
:RPS2        .           .
:RPS2          .        .
:RPS2            .     .
:RPS2              .  .
:RPS2                .

:RPS3  ....
:RPS3   .....
:RPS3     .......
:RPS3      ........
:RPS3         ........
:RPS3            ........
:RPS3               ........           .........
:RPS3                 ..................       ..
:RPS3                   ................       ..
:RPS3                     .........   ....  ....
:RPS3                      ..     ....
:RPS3                      ..        ...
:RPS3                       ...         ...
:RPS3                       ...         ..
:RPS3                          ...........

CodePudding user response:

Just putting an option out there, if you are running Windows 10, which supports VT100 escape codes, you can do something like:

@echo off
for /F %%i in ('echo prompt $E ^| cmd') do set "n=%%iE"
set "scissors=%n%   ....%n%   .....%n%     .......%n%      ........%n%         ........%n%            ........%n%               ........           .........%n%                 ..................       ..%n%                   ................       ..%n%                     .........   ....  ....%n%                      ..     ....%n%                      ..        ...%n%                       ...         ...%n%                       ...         ..%n%                          ..........."
echo %scissors%

CodePudding user response:

You could define your variable like this:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set Scissors=^

....^

  .....^

    .......^

      ........^

         ........^

            ........^

               ........           .........^

                 ..................       ..^

                   ................       ..^

                     .........   ....  ....^

                      ..     ....^

                      ..        ...^

                       ...         ...^

                       ...         ..^

                          ...........

Then to use it you could enable delayed expansion.

SetLocal EnableDelayedExpansion
Echo(!Scissors!

Then later, revert to the previous delayed expansion state:

EndLocal

If you did not want to enable delayed expansion, it would take a little more work to output your image:

For /F "Tokens=1,* Delims==" %%G In ('"(Set Scissors) 2>NUL"') Do Echo(%%G

CodePudding user response:

Another way:

@echo off
setlocal EnableDelayedExpansion

REM Define variables:

(for %%a in (
"      .. ... .. .. ."
"    . ... ... .. . .. ."
"  . .. .  ... ... ......"
" .........   ..... ......"
". ......   ..... ........ ."
" .........   ..... ......"
".. ..  ................ ."
"  .. .....  .. .. ......."
"    . . . ... . .. ... ."
"     . ... .. . . . ."
"      ...   ...   ..") do set "rock=!rock!$%%~a") & set "rock=!rock:~1!"

(for %%a in (
"      ."
"     .  ."
"    .     ."
"   .        ."
"  .           ."
" .              ."
".                 ."
"  .                 ."
"    .                 ."
"      .              ."
"        .           ."
"          .        ."
"            .     ."
"              .  ."
"                .") do set "paper=!paper!$%%~a") & set "paper=!paper:~1!"

(for %%a in (
"...."
" ....."
"   ......."
"    ........"
"       ........"
"          ........"
"             ........           ........."
"               ..................       .."
"                 ................       .."
"                   .........   ....  ...."
"                    ..     ...."
"                    ..        ..."
"                     ...         ..."
"                     ...         .."
"                        ...........") do set "scissors=!scissors!$%%~a") & set "scissors=!scissors:~1!"

REM Display variables:

cls

echo rock: & echo/& echo %rock:$=& echo %& echo/& echo/

echo paper: & echo/& echo %paper:$=& echo %& echo/& echo/

echo scissors: & echo/& echo %scissors:$=& echo %& echo/& echo/
  • Related