How to have multiple colors in a Windows batch file?

前端 未结 12 1406
我寻月下人不归
我寻月下人不归 2020-11-22 01:05

I was wondering if its possible to have different colored text on the same line in a Windows batch file, for example if it says

echo hi world
12条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 01:28

    You can do multicolor outputs without any external programs.

    @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 say the name of the colors, don't read
    
    call :ColorText 0a "blue"
    call :ColorText 0C "green"
    call :ColorText 0b "red"
    echo(
    call :ColorText 19 "yellow"
    call :ColorText 2F "black"
    call :ColorText 4e "white"
    
    goto :eof
    
    :ColorText
    echo off
     "%~2"
    findstr /v /a:%1 /R "^$" "%~2" nul
    del "%~2" > nul 2>&1
    goto :eof
    

    It uses the color feature of the findstr command.

    Findstr can be configured to output line numbers or filenames in a defined color.
    So I first create a file with the text as filename, and the content is a single character (ASCII 8).
    Then I search all non empty lines in the file and in nul, so the filename will be output in the correct color appended with a colon, but the colon is immediatly removed by the .

    EDIT: One year later ... all characters are valid

    @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"
    )
    
    rem Prepare a file "X" with only one dot
     X set /p ".=."
    
    call :color 1a "a"
    call :color 1b "b"
    call :color 1c "^!<>&| %%%%"*?"
    exit /b
    
    :color
    set "param=^%~2" !
    set "param=!param:"=\"!"
    findstr /p /A:%1 "." "!param!\..\X" nul
    

    This uses the rule for valid path/filenames.
    If a \..\ is in the path the prefixed elemet will be removed completly and it's not necessary that this element contains only valid filename characters.

提交回复
热议问题