0

So, I'm trying to create a custom format image viewer in batch file witch reads from this file named image.ansii2 :

lines=18
line1=[15]---------------
line2=[15]----[160]------[15]-----
line3=[15]---[160]----------[15]--
line4=[15]---[94]---[223]---[0]-[223]-[15]----
line5=[15]--[94]-[223]-[94]-[223]----[0]-[223]---[15]--
line6=[15]--[94]-[223]-[94]--[223]----[0]-[223]---[15]-
line7=[15]--[94]--[223]-----[0]----[15]--
line8=[15]----[223]--------[15]---
line9=[15]---[160]--[26]-[160]--[26]-[160]-[15]-----
line10=[15]--[160]---[26]-[160]--[26]-[160]---[15]---
line11=[15]-[160]----[26]----[160]----[15]--
line12=[15]-[223]--[160]-[26]-[220]-[26]--[220]-[26]-[160]-[223]--[15]--
line13=[15]-[223]---[26]------[223]---[15]--
line14=[15]-[223]--[26]--------[223]--[15]--
line15=[15]---[26]---[15]--[26]---[15]----
line16=[15]--[94]---[15]----[94]---[15]---
line17=[15]-[94]----[15]----[94]----[15]--
line18=[15]---------------

and the program (ansii2.bat) looks like this :

@echo off
for %%a in (%1) do (set ext=%%~xa)    
if "%1" == "" (echo No file was specified&pause&exit /b)
if not "%ext%" == ".ansii2" (echo The file specified didn't have the expected extension [%ext%] -^> [.ansii2]&pause&exit /b)
title Ansii2  %1
:0
echo [0m
cls
for /f "delims== tokens=1,2" %%G in (%1) do set "%%G=%%H"
set loop=0
:loop
set /a loop+=1
set line=line%loop%
set "image=echo %!line!:[=[48;5;%"
set "image=%image:]=m%"
set "image=%image:(=[38;5;%"
set "image=%image:)=m%"
set "image=%image:-=  %"
%image%
echo %line%
if %loop% == %lines% (goto exitloop)
goto :loop
:exitloop
timeout /t -1 >nul
goto 0

I think that the bug comes from the line 14 but I don't know what to do to fix it...

Could someone help me?

Infers
  • 17
  • 4
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 06 '21 at 20:41

1 Answers1

1

Your attempt at substitution is off, and the method by which you read the file can be improved upon:

All your variables are formatted in the source file, so a for loop can be used to read and assign them by splitting the string at the = delimiter

Delayed expansion is used to perform the substitution, using:
!varname:search=replace! ; where varname is referenced using the meavariable of the for loops first token.

@Echo off & CD "%~dp0"
 cls
Setlocal EnableExtensions EnableDelayedExpansion
 For /F %%e in ('Echo prompt $E^|cmd')Do Set "\E=%%e"
 (For /f "usebackq Tokens=1,2 delims==" %%G in ("%~$Path:1")Do (
  Set "%%G=%%H"
  Set "%%G=!%%G:[=%\E%[48;5;!"
  Set "%%G=!%%G:(=%\E%[38;5;!"
  Set "%%G=!%%G:]=m!"
  Set "%%G=!%%G:)=m!"
  Set "%%G=!%%G:-= !"
  If not "%%G" == "lines" <nul set /p "=!%%G!%\E%[0m%\E%[E"
 )) > Con

Endlocal

Other notes:

In the event the supplied file cannot be found the following will be output:

 The system cannot find the file .

<nul set /p "=!variable!" ; allows safe output of poison characters
%\E%[E ; Equivalent to Outputting a linefeed.

Data structure for your source file: using single characters for substitution prevents them from being used as characters in the ascii art. use paired characters or unique strings to prevent this. Ie :

\i=%\E%[48;5;
\e=%\E%[38;5;
\m=m
T3RR0R
  • 2,747
  • 3
  • 10
  • 25
  • If your interested in animated Ascii art, see [here](https://stackoverflow.com/questions/68452225/is-it-possible-to-reliably-run-an-ascii-text-based-video-from-cmd-prompt/68454079#68454079) – T3RR0R Sep 03 '21 at 07:10