How do I make one particular line of a batch file a different color then the others?

前端 未结 4 812
孤独总比滥情好
孤独总比滥情好 2020-11-30 10:27

I\'m working on a program which generates the day\'s weather for D&D games. I want the program to display a warning in red text when a storm is generated so the DM is aw

相关标签:
4条回答
  • 2020-11-30 11:01

    Script from here: How to have multiple colors in a Windows batch file? a little modified (simplyfied):

    @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
    

    It was a link in the very first answer of "possible duplicate: How to change the text color of comments line in a batch file "

    0 讨论(0)
  • 2020-11-30 11:03

    I was having the same problem yesterday, I did some research and this worked for me. EDIT: This is NOT the same code as the other one.

    @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
    

    it does this: Output

    All you need to do to fit this is put the part from SETLOCAL EnableDel... to ) to the beginning of your code (If your code starts with @echo off then put it under that) and also put the part from :colorEcho to del %~2 at the exact bottom of your script (NOTHING UNDERNEATH!)

    Now between you notice

    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
    

    Explained line by line: First line (call :colorEcho 0a "This is colored green with a black background!"): This is a colored echo, it suprisingly says This is colored green with a black background! in 0a (Black Bg, Green Text) Second line (echo.) prints a newline, since our colored echo doesn't print one.

    SO

    Let's say you wanted to say "Hello World" in Hello being yellow and World being green. Example!

    @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
    

    This is what it does: Output I hope this is understandable!

    EDIT: Make sure the program exits before it could reach :colorEcho

    0 讨论(0)
  • 2020-11-30 11:17
    @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\
    
    0 讨论(0)
  • 2020-11-30 11:18

    You can try this script. Use it to print any text with any color in any screen position. It does not use temporary files, but your system must have debug.exe executable (Windows native):

    @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
    
    0 讨论(0)
提交回复
热议问题