1

I want to change the colors for the rest of the text promted by CMD and keep the colors for the text printed before color command was executed.

example:

@echo off
echo hello world
color 0A

The problem with this code is that it will turn the hello world text to green as well as all the other text in the command promt.

dbenham
  • 127,446
  • 28
  • 251
  • 390
Qlint
  • 13
  • 4
  • Are you ***really*** still using MS-DOS? (which would mean you are using `command.com` not `cmd.exe`) –  Apr 04 '16 at 08:58
  • 3
    Wait, no, [this one is better](http://stackoverflow.com/questions/4339649/how-to-have-multiple-colors-in-a-windows-batch-file). Regardless, this has been asked a bajillion times already. – SomethingDark Apr 04 '16 at 09:07
  • No I am not still using MS-DOS, I am using cmd.exe, sorry if there was any missunderstandings about that. – Qlint Apr 04 '16 at 16:45

2 Answers2

0

This code can did the trick :

When you call something like this : Call :Color 0B "Hello world !" 0 The second argument equal to 0 means you stay in the same line

If you choose the second argument equal to 1 like this one :

Call :Color 0e "Not in same line !" 1

@echo off
Title Example with multicolors 
Call :Color 0A "Hello world !" 0
Call :Color 0D "Hello world in the same line !" 0
echo(
Call :Color 0C "Hello world !" 1 
Call :Color 0E "Not in same line !" 1 
Pause
::***********************************
:Color
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
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
)
::***********************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70
0

You may "change the colors for the rest of the text promted by CMD" (that is not the same of "show text in a given color") with the aid of PowerShell, but the method is s-l-o-w...

@echo off

echo Standard color
call :SetColor Yellow/Blue
set /P "=Hello" < NUL
call :SetColor Magenta/DarkGreen
echo  World!
goto :EOF


:SetColor fore/back
for /F "tokens=1,2 delims=/" %%a in ("%~1") do (
   PowerShell $host.UI.RawUI.BackgroundColor = '%%b'; $host.UI.RawUI.ForegroundColor = '%%a'
)
exit /B

The colors set via this method are used in all posterior text displayed in the cmd.exe window!

Available color names:

Black           DarkGray
DarkBlue        Blue
DarkGreen       Green
DarkCyan        Cyan
DarkRed         Red
DarkMagenta     Magenta
DarkYellow      Yellow
Gray            White
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • This works as long as i dont double click the .bat file, if i put cmd /k in the end of the .bat file the cmd promt will stay open but all the colors will be changed to what i last told :SetColor to use. – Qlint Apr 04 '16 at 17:48
  • I am afraid I don't understand what you mean... This program change the colors of the text displayed _in the cmd.exe window_ where it is executed (exactly the same as `color` command). If the window is closed, ... – Aacini Apr 04 '16 at 18:23
  • Yes, it works! I will accept this one as a working answer. One problem i have with it though is that it is not possible to double click this .bat file and keep it open. But that is another problem i guess. Thanks a lot for the help @Aacini – Qlint Apr 18 '16 at 09:56