如何在批处理中修复对另一个变量的引用?



所以,我试图在批处理文件中创建一个自定义格式的图像查看器,女巫从这个名为图像的文件中读取。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]---------------

和程序(ansi2 .bat)看起来像这样:

@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

我认为这个错误来自第14行,但我不知道该怎么做来修复它…

有人能帮帮我吗?

您的替换尝试已经关闭,您读取文件的方法可以改进:

所有变量都在源文件中进行了格式化,因此可以使用for循环通过在=分隔符

处分割字符串来读取和分配它们使用延迟展开来执行替换,使用:
!varname:search=replace!;其中varname是使用for循环第一个标记的变量来引用的。

@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

其他笔记:

如果无法找到所提供的文件,将输出以下内容:

The system cannot find the file .

<nul set /p "=!variable!";允许安全输出有毒字符
%E%[E;相当于输出换行符

源文件的数据结构:使用单个字符进行替换可以防止它们在ASCII艺术中被用作字符。使用成对字符或唯一字符串来防止这种情况。即:

i=%E%[48;5;
e=%E%[38;5;
m=m

最新更新