1

I am trying to colorize specific parts of my batch file and was wondering if it is A even possible and B if I can just color specific text and not the whole cmd window.

This is my test code in which I wish to color Y and N green and red:

@echo off
:start
echo hello, are you a Robot?
:choice
echo How do you answer?
set /P c=[Y/N]
if /I "%c%" EQU "Y" goto :start
if /I "%c%" EQU "N" goto :shutdown
goto :choice
:shutdown
exit
Juan Serrats
  • 1,358
  • 5
  • 24
  • 30
Aceliip
  • 11
  • 1
  • 2
    Please check if this solves your problem http://stackoverflow.com/questions/21660249/how-do-i-make-one-particular-line-of-a-batch-file-a-different-color-then-the-oth – Girdhari Agrawal Mar 26 '17 at 20:00
  • 2
    Possible duplicate of [How do I make one particular line of a batch file a different color then the others?](http://stackoverflow.com/questions/21660249/how-do-i-make-one-particular-line-of-a-batch-file-a-different-color-then-the-oth) – jcarder Mar 26 '17 at 20:01
  • http://stackoverflow.com/questions/4339649/how-to-have-multiple-colors-in-a-windows-batch-file/5344911#5344911 – Aacini Mar 27 '17 at 14:10

3 Answers3

0

You can do something like this :

@echo off & setlocal enabledelayedexpansion
Call :init
:start
cls
Call :Color 0E " Hello, are you a Robot " 0 & echo|set /p="?"
echo(
:choice
Call :Color 0A " How do you answer " 0 & echo|set /p="?"
echo(
set /P c=[Y/N]
if /I "%c%" EQU "Y" goto :start
if /I "%c%" EQU "N" goto :shutdown
goto :choice
:shutdown
exit

::*************************************************************************************
:init
prompt $g
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
exit /b
::*************************************************************************************
:color
set nL=%3
if not defined nL echo requires third argument & pause > nul & goto :eof
if %3 == 0 (
    <nul set /p ".=%bs%">%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
) else if %3 == 1 (
    echo %bs%>%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
)
exit /b
::*************************************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70
0
@ECHO OFF
SETLOCAL
SET "esc="
SET "normal=%esc%[1;33m"
SET "red=%esc%[1;31m"
SET "green=%esc%[1;32m"
:start
echo hello, are you a Robot?
:choice
echo How do you answer?
set /P c=[%green%Y%normal%/%red%N%normal%]
if /I "%c%" EQU "Y" goto start
if /I "%c%" EQU "N" goto shutdown
goto choice
:shutdown

My normal colour settings are yellow on black. The escape character is well- an escape, however it might be rendered on SO. I use editplus, which allows control characters to be entered. Other editors may allow this in other ways.

Other than that, it's simply a matter of switching the colours as desired and reverting to normal colours after showing the specials.

I've added a setlocal so that environment changes are reverted when the batch closes (this is for the general case; yours seems to be set up to exit)

I've also removed the colons in the goto statements. The only condition that colons are required here is the specific case of goto :eof which has a particular meaning.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Hmm - the rendering of an `escape` character appears to be a cloak of invisibility. There needs to be an actual `Esc` between the `esc=` and the `"` – Magoo Mar 27 '17 at 01:35
-2

You can use ANSI escape codes in batch files.

Mike Lowery
  • 2,630
  • 4
  • 34
  • 44
  • I guess you are down voted because link only answers won't suffice on the long term. Links tend to fluctuate leaving nothing to help others. –  Mar 27 '17 at 09:19
  • ... and also perhaps this is barely an "answer". It would be a good comment, though. – Aacini Mar 27 '17 at 14:13