如何使批处理文件的某一特定行与其他行不同?

时间:2014-02-09 14:26:25

标签: batch-file

我正在制作一个为D& D游戏创造当天天气的节目。我希望程序在生成风暴时以红色文本显示警告,以便DM知道这种天气不典型。要减少击键次数,必须在与详细说明天气本身的文本相同的屏幕上进行此操作。

目前在暴风雨中我有这个:

:des4a
cls
type c:\AutoDM\WeatherGen\data\forcast1.txt
color 0c
echo     There is an Ashstorm!
color 0a
echo.
echo     It is %temp% degrees and the sky is invisible threw the thick 
echo     billowing clouds of ash.
echo.
echo         # Survival checks to light a fire are at +15. 
echo         # Small unprotected flames will be snuffed out.
echo         # Non-firearm ranged attacks are at a -8 to hit.
echo         # Preception checks take a -10 for every 10 feet of distance.
echo         # Survival checks to get along in the wild are at +15.
echo         # Stealth checks are at a +5.
echo.
echo     SPECIAL!
echo.
set /a die=6
set /a inches=%random%%%die+1
echo         # The ashstorm will deposit %inches% inches of ash durring its
echo           durration.
echo         # Tracking a target after an ashstorm is at a +15.
type c:\AutoDM\WeatherGen\data\forcast2.txt
echo.
echo.
pause
goto menu

类型命令调用文本文档,其中包含每个条目的页眉和页脚,以帮助程序看起来很专业,并提供边框以协助自动换行。你不能被删除。请不要提出一些让我无法使用当前存在的类型命令的内容。请理解,每当发生器中的每个生物群系都出现风暴时,此红色文本行将添加到程序的许多不同部分。我希望它不仅仅是2或3行代码(但如果只有一种方法可以做到这一点......)

我可以让一行为红色,其余为绿色吗?或者我应该让程序发出嘟嘟声以引起注意这一事实?你有更好更简单的想法吗?我该怎么做?

4 个答案:

答案 0 :(得分:26)

昨天我遇到了同样的问题,我做了一些研究,这个为我工作。 编辑:这是与其他代码相同的代码。

@Echo Off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do     rem"') do (
  set "DEL=%%a"
)
call :colorEcho 0a "This is colored green with a black background!"
echo.
call :colorEcho a0 "This is colored black with a green background!"
echo.
pause
exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i

它这样做: Output

您需要做的就是将SETLOCAL EnableDel ...到)的部分放到代码的开头(如果您的代码以@echo开头,则将其置于并且还将:colorEchodel %~2的部分放在脚本的确切底部( NOTHING UNDERNEATH!

现在你注意到了

call :colorEcho 0a "This is colored green with a black background!"
echo.
call :colorEcho a0 "This is colored black with a green background!"
echo.
pause
exit

逐行说明: 第一行(call :colorEcho 0a "This is colored green with a black background!"):这是一个colored回音,令人惊讶的是This is colored green with a black background!中的0a(Black Bg,Green Text) 第二行(echo.)打印换行符,因为我们的彩色回声不打印一行。

SO

假设您想说Hello中的“Hello World”为黄色且World为绿色。 例如!

@Echo Off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do     rem"') do (
  set "DEL=%%a"
)
call :colorEcho 0e "Hello "
call :colorEcho 0a " World"
pause
exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i

这就是它的作用: Output 我希望这是可以理解的!

编辑:确保计划exit之前可以达到:colorEcho

答案 1 :(得分:1)

来自这里的脚本: How to have multiple colors in a Windows batch file? 稍加修改(简单):

@echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)
echo next line in another color:
call :ColorText 0c "There is an Ashstorm!"
echo this was red.
call :ColorText 0a "you survived it."

goto :eof

:ColorText
echo off
echo %DEL% > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof

这是“可能重复的第一个答案中的链接:How to change the text color of comments line in a batch file

答案 2 :(得分:0)

@Echo Off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do     rem"') 

do (
  set "DEL=%%a"
)
call :colorEcho 0c "HyperBeast"
echo.
call :colorEcho 0c "NeonRider"
echo.
call :colorEcho 0d "Djinn"
echo.
call :colorEcho 0d "Eco"
echo.
call :colorEcho 0d "MonkeyBuisness"
echo.
call :colorEcho 05 "GrandPrix"
echo.
call :colorEcho 05 "PolePosition"
echo.
call :colorEcho 05 "Heat"
echo.
call :colorEcho 05 "WormGod"
echo.
call :colorEcho 09 "Origami"
echo.
call :colorEcho 09 "Man'o'war"
echo.
call :colorEcho 09 "Valence"
echo.
call :colorEcho 09 "BronzeDeco"
echo.
call :colorEcho 09 "ArmorCore"
echo.
call :colorEcho 09 "EliteBuild"
echo.
pause
exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1\

答案 3 :(得分:0)

您可以尝试使用此脚本。使用它可以在任何屏幕位置打印任何颜色的任何文本。它不使用临时文件,但您的系统必须具有debug.exe可执行文件(Windows本机):

@echo off
rem Script written by BrendanSilva [bl8086]
rem You need DEBUG.EXE executable in your system.
setlocal enabledelayedexpansion
set /a _er=0
set /a _n=0
set _ln=%~4
goto init
:howuse ------------------------------------------------------------------------
    echo.
    echo ECOL.BAT - v2.0
    echo Print colored text as batch script without temporary files.
    echo Written by bl8086
    echo.
    echo Syntax:
    echo ECOL.BAT [COLOR] [X] [Y] "Insert your text here"
    echo COLOR value must be a hexadecimal number like "color /?" information
    echo.
    echo Example:
    echo ECOL.BAT F0 20 30 "640K ought to be enough for anybody."
    echo.
    goto :eof
:error ------------------------------------------------------------------------
    set /a "_er=_er | (%~1)"
    goto :eof
:gcnvhx ------------------------------------------------------------------------
    set _cvhx=
    set /a _cvint=%~1
:cnvhx
    set /a "_gch = _cvint & 0xF"
    set _cvhx=!nsys:~%_gch%,1!%_cvhx%
    set /a "_cvint = _cvint >> 4"
    if !_cvint! neq 0 goto cnvhx
    goto :eof
:init --------------------------------------------------------------------------
    if "%~4"=="" call :error 0xff
    (
        set /a _cl=0x%1
        call :error !errorlevel!
        set _cl=%1
        call :error "0x!_cl! ^>^> 8"
        set /a _px=%2
        call :error !errorlevel!
        set /a _py=%3
        call :error !errorlevel!
    ) 2>nul 1>&2
    if !_er! neq 0 (
        echo.
        echo ERROR: value exception "!_er!" occurred. Check memory out.
        echo.
        goto howuse
    )
    set nsys=0123456789ABCDEF
    set /a cnb=0
    set /a cnl=0
    set _cvhx=0
    set _cvint=0
    set _cvmhx=0
:parse -------------------------------------------------------------------------
    set _ch=!_ln:~%_n%,1!
    if "%_ch%"=="" goto perform
    set /a "cnb += 1"
    if %cnb% gtr 7 (
        set /a cnb=0
        set /a "cnl += 1"
    )
    set bln%cnl%=!bln%cnl%! "!_ch!" %_cl%
    set /a "_n += 1"
    goto parse
:perform -----------------------------------------------------------------------
    set /a "in = ((_py * 0xA0) + (_px << 1)) & 0xFFFF"
    call :gcnvhx %in%
    set ntr=!_cvhx!
    set /a jmp=0xe
    set bl8086str=echo.h 0 0
    @for /l %%x in (0,1,%cnl%) do (
        set bl8086str=!bl8086str!^&echo.eb800:!ntr! !bln%%x!
        set /a "in=!jmp! + 0x!ntr!"
        call :gcnvhx !in!
        set ntr=!_cvhx!
        set /a jmp=0x10
    )
    (
    echo %bl8086str%
    echo.q
    ) |debug >nul 2>&1