Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 252 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to echo with different colors in the Windows command line

#11
You can use the color command to change the color of the whole console

Color 0F

Is black and white

Color 0A
Is black and green



Reply

#12
You could use [ANSICON][1] to enable ANSI terminal codes in older versions of Windows. There are 32 and 64 bit versions that I have used in Windows XP and Windows 7.


[1]:

[To see links please register here]

Reply

#13
There is an accepted answer with more than 250 upvotes already. The reason I am still contributing is that the `escape` character required for echoing is not accepted by many editors (I am using, e.g., MS Code) and all other solutions require some third-party (non-Windows-default) pieces of software.

The work-around with using only plain batch commands is using `PROMPT` instead of `ECHO`. The `PROMPT` command accepts the `escape` character in an any-editor-friendly way as a `$E`character sequence. (Simply replace the `Esc` in the [ASCII Escape codes][1]) with `$E`.

Here is a demo code:

```bat
@ECHO OFF

:: Do not pollute environment with the %prompt.bak% variable
:: ! forgetting ENDLOCAL at the end of the batch leads to prompt corruption
SETLOCAL

:: Old prompt settings backup
SET prompt.bak=%PROMPT%

:: Entering the "ECHO"-like section

:: Forcing prompt to display after every command (see below)
ECHO ON

:: Setting the prompt using the ANSI Escape sequence(s)
:: - Always start with $E[1A, otherwise the text would appear on a next line
:: - Then the decorated text follows
:: - And it all ends with $E30;40m, which makes the following command invisible
:: - assuming default background color of the screen
@ PROMPT $E[1A$E[30;42mHELLO$E[30;40m

:: An "empty" command that forces the prompt to display.
:: The word "rem" is displayed along with the prompt text but is made invisible
rem

:: Just another text to display
@ PROMPT $E[1A$E[33;41mWORLD$E[30;40m
rem

:: Leaving the "ECHO"-like section
@ECHO OFF

:: Or a more readable version utilizing the cursor manipulation ASCII ESC sequences

:: the initial sequence
PROMPT $E[1A
:: formating commands
PROMPT %PROMPT%$E[32;44m
:: the text
PROMPT %PROMPT%This is an "ECHO"ed text...
:: new line; 2000 is to move to the left "a lot"
PROMPT %PROMPT%$E[1B$E[2000D
:: formating commands fro the next line
PROMPT %PROMPT%$E[33;47m
:: the text (new line)
PROMPT %PROMPT%...spreading over two lines
:: the closing sequence
PROMPT %PROMPT%$E[30;40m

:: Looks like this without the intermediate comments:
:: PROMPT $E[1A
:: PROMPT %PROMPT%$E[32;44m
:: PROMPT %PROMPT%This is an "ECHO"ed text...
:: PROMPT %PROMPT%$E[1B$E[2000D
:: PROMPT %PROMPT%$E[33;47m
:: PROMPT %PROMPT%...spreading over two lines
:: PROMPT %PROMPT%$E[30;40m

:: show it all at once!
ECHO ON
rem
@ECHO OFF

:: End of "ECHO"-ing

:: Setting prompt back to its original value
:: - We prepend the settings with $E[37;40m in case
:: the original prompt settings do not specify color
:: (as they don't by default).
:: - If they do, the $E[37;40m will become overridden, anyway.
:: ! It is important to write this command
:: as it is with `ENDLOCAL` and in the `&` form.
ENDLOCAL & PROMPT $E[37;40m%prompt.bak%

EXIT /B 0
```
NOTE: The only drawback is that this technique collides with user cmd color settings (`color` command or settings) if not known explicitly.

-- Hope this helps as thi is the only solution acceptable for me for the reasons mentioned at the beginning. --

**EDIT:**

Based on comments, I am enclosing another snippet inspired by @Jeb. It:

- Shows how to obtain and use the "Esc" character runtime (rather than entering it to an editor) (Jeb's solution)
- Uses "native" `ECHO` command(s)
- So it does not affect local `PROMPT` value
- Demonstrates that coloring the `ECHO` output inevitably affect `PROMPT` color so the color must be reset, anyway

```bat
@ECHO OFF

:: ! To observe color effects on prompt below in this script
:: run the script from a fresh cmd window with no custom
:: prompt settings

:: Only not to pollute the environment with the %\e% variable (see below)
:: Not needed because of the `PROMPT` variable
SETLOCAL

:: Parsing the `escape` character (ASCII 27) to a %\e% variable
:: Use %\e% in place of `Esc` in the [

[To see links please register here]

]
FOR /F "delims=#" %%E IN ('"prompt #$E# & FOR %%E IN (1) DO rem"') DO SET "\e=%%E"

:: Demonstrate that prompt did not get corrupted by the previous FOR
ECHO ON
rem : After for
@ECHO OFF

:: Some fancy ASCII ESC staff
ECHO [ ]
FOR /L %%G IN (1,1,10) DO (
TIMEOUT /T 1 > NUL
ECHO %\e%[1A%\e%[%%GC%\e%[31;43m.
ECHO %\e%[1A%\e%[11C%\e%[37;40m]
)

:: ECHO another decorated text
:: - notice the `%\e%[30C` cursor positioning sequence
:: for the sake of the "After ECHO" test below
ECHO %\e%[1A%\e%[13C%\e%[32;47mHELLO WORLD%\e%[30C

:: Demonstrate that prompt did not get corrupted by ECHOing
:: neither does the cursor positioning take effect.
:: ! But the color settings do.
ECHO ON
rem : After ECHO
@ECHO OFF

ENDLOCAL

:: Demonstrate that color settings do not reset
:: even when out of the SETLOCAL scope
ECHO ON
rem : After ENDLOCAL
@ECHO OFF

:: Reset the `PROMPT` color
:: - `PROMPT` itself is untouched so we did not need to backup it.
:: - Still ECHOING in color apparently collide with user color cmd settings (if any).
:: ! Resetting `PROMPT` color this way extends the `PROMPT`
:: by the initial `$E[37;40m` sequence every time the script runs.
:: - Better solution then would be to end every (or last) `ECHO` command
:: with the `%\e%[37;40m` sequence and avoid setting `PROMPT` altogether.
:: which makes this technique preferable to the previous one (before EDIT)
:: - I am keeping it this way only to be able to
:: demonstrate the `ECHO` color effects on the `PROMPT` above.
PROMPT $E[37;40m%PROMPT%

ECHO ON
rem : After PROMPT color reset
@ECHO OFF

EXIT /B 0
```

[1]:

[To see links please register here]

Reply

#14
I'm adding an answer to address an issue noted in some comments above: that inline ansi color codes can misbehave when inside a FOR loop (actually, within any parenthesized block of code). **The .bat code below demonstrates (1) the use of inline color codes, (2) the color failure that can occur when inline color codes are used in a FOR loop or within a parenthesized block of code, and (3) a solution to the problem.** When the .bat code executes, tests 2 and 3 demonstrate the colorcode failure, and test 4 shows no failure because it implements the solution.

[EDIT 2020-04-07: I found another solution that's presumably more efficient than calling a subroutine. Enclose the FINDSTR phrase in parentheses, as in the following line:
```
echo success | (findstr /R success)
```
ENDEDIT]

Note: In my (limited) experience, the color code problem manifests only after input is piped to FINDSTR inside the block of code. That's how the following .bat reproduces the problem. **It's possible the color code problem is more general than after piping to FINDSTR. If someone can explain the nature of the problem, and if there's a better way to solve it, I'd appreciate it.**
```
@goto :main
:resetANSI
EXIT /B
rem The resetANSI subroutine is used to fix the colorcode
rem bug, even though it appears to do nothing.

:main
@echo off
setlocal EnableDelayedExpansion

rem Define some useful colorcode vars:
for /F "delims=#" %%E in ('"prompt #$E# & for %%E in (1) do rem"') do set "ESCchar=%%E"
set "green=%ESCchar%[92m"
set "yellow=%ESCchar%[93m"
set "magenta=%ESCchar%[95m"
set "cyan=%ESCchar%[96m"
set "white=%ESCchar%[97m"
set "black=%ESCchar%[30m"

echo %white%Test 1 is NOT in a FOR loop nor within parentheses, and color works right.
echo %yellow%[Test 1] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
echo %magenta%This is magenta and FINDSTR found and displayed 'success'.%yellow%
echo %green%This is green.
echo %cyan%Test 1 completed.

echo %white%Test 2 is within parentheses, and color stops working after the pipe to FINDSTR.
( echo %yellow%[Test 2] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
echo %magenta%This is supposed to be magenta and FINDSTR found and displayed 'success'.
echo %green%This is supposed to be green.
)
echo %cyan%Test 2 completed.

echo %white%Test 3 is within a FOR loop, and color stops working after the pipe to FINDSTR.
for /L %%G in (3,1,3) do (
echo %yellow%[Test %%G] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
echo %magenta%This is supposed to be magenta and FINDSTR found and displayed 'success'.
echo %green%This is supposed to be green.
)
echo %cyan%Test 3 completed.

echo %white%Test 4 is in a FOR loop but color works right because subroutine :resetANSI is
echo called after the pipe to FINDSTR, before the next color code is used.
for /L %%G in (4,1,4) do (
echo %yellow%[Test %%G] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
call :resetANSI
echo %magenta%This is magenta and FINDSTR found and displayed 'success'.
echo %green%This is green.
)
echo %cyan%Test 4 completed.%white%

EXIT /B
```
Reply

#15
*Setting color to the log statements in powershell is not a big deal friend.*
you can use `-ForegroundColor` parameter.

**To write a confirmation message.**

Write-Host "Process executed Successfully...." -ForegroundColor Magenta

**To write an error message.**

Write-Host "Sorry an unexpected error occurred.." -ForegroundColor Red

**To write a progress message**.

Write-Host "Working under pocess..." -ForegroundColor Green
Reply

#16
I just converted from Win 7 Home to Win 10 Pro and wanted to replace the batch I call from other batches to echo info in color. Reviewing what is discussed above I use the following which will directly replace my previous batch. NOTE the addition of "~" to the message so that messages with spaces may be used. Instead of remembering codes I use letters for the colors I needed.

If %2 contains spaces requires "..."
%1 Strong Colors on black: R=Red G=GREEN Y=YELLOW W=WHITE

ECHO OFF
IF "%1"=="R" ECHO ^%~2
IF "%1"=="G" ECHO ^%~2
IF "%1"=="Y" ECHO ^%~2
IF "%1"=="W" ECHO ^%~2
Reply

#17
call :color_echo "blue" "blue txt"
call :color_echo "red" "red txt"
echo "white txt"


REM :

[To see links please register here]

:color_echo
@echo off

set "color=%~1"
set "txt=%~2"

set ESC=
set black=%ESC%[30m
set red=%ESC%[31m
set green=%ESC%[32m
set yellow=%ESC%[33m
set blue=%ESC%[34m
set magenta=%ESC%[35m
set cyan=%ESC%[36m
set white=%ESC%[37m

if "%~1" == "black" set "color=!black!"
if "%~1" == "red" set "color=!red!"
if "%~1" == "green" set "color=!green!"
if "%~1" == "yellow" set "color=!yellow!"
if "%~1" == "blue" set "color=!blue!"
if "%~1" == "magenta" set "color=!magenta!"
if "%~1" == "cyan" set "color=!cyan!"
if "%~1" == "white" set "color=!white!"

echo | set /p="!color!!txt!"
echo.

REM : return to standard white color
echo | set /p="!white!"

REM : exiting the function only
EXIT /B 0
Reply

#18
**Windows 10 - TH2 and above:**

(a.k.a. Version 1511, build 10586, release 2015-11-10)

**At Command Prompt:**

echo ^[[32m HI ^[[0m

Using the actual keys:   echo <kbd>Ctrl</kbd>+<kbd>[</kbd>`[32m HI ` <kbd>Ctrl</kbd>+<kbd>[</kbd>`[0m`<kbd>Enter</kbd>

You should see a green "HI" below it.

Code numbers can be found here:

-

[To see links please register here]


**Notepad:**

To save this into notepad, you can type ESC into it using: <kbd>Alt</kbd>+`027` with the numpad, then the `[32m` part. Another trick when I was on a laptop, redirect the line above into a file to get started, then cut and paste:

echo echo ^[[32m HI ^[[0m >> batch_file.cmd
Reply

#19
[![ This is just help, as optical example,
when make or modify colorful command prompt echoes.][1]][1]


The following code consists of two parts. If it is convenient for you too, there is also a .txt format in this .cmd file, below the "double" line (====).

::adonios77
::This is a .cmd file
@ECHO OFF
TITLE Colored Command Prompt echoes HELP
mode con: cols=55 lines=47
CLS
COLOR 0f
echo [93m
ECHO This is just help, as optical example,
ECHO when make or modify colorful command prompt echoes.
ECHO.
ECHO More info in Source:
ECHO [4m[94mhttps://stackoverflow.com/questions/2048509/how-to-echo-with-different-colors-in-the-windows-command-line[0m

ECHO.
ECHO [0mESC[0m "Text" Default colours Text[0m
ECHO [7mESC[7m "Text" Inversed Back-Fore colors[0m
ECHO [101mESC[101m "Text" in Red Background[0m
ECHO [91mESC[91m "Text" in Red Foreground)[0m

echo.
echo To make an ESC special character, (ASCII Escape code)
echo open or edit a .txt or .bat or .cmd file,
echo (hold)L-Alt and (type)027 in NumPad)
echo Or, in Command Prompt, (can't copy/paste special char.)
echo just press Ctrl+[
echo (it should look like: "echo ^[[33m'Text'^[[0m")
echo.
echo STYLES
echo [0mESC[0m Reset[0m
echo [1mESC[1m Bold [90m*This is not work for me[0m
echo [4mESC[4m Underline[0m
echo [7mESC[7m[0m Inverse
echo.
echo COLORS# Foreground-Background (color /? HEX) && echo.
echo [90mDark[0m / [100mLight[0m
echo Fore-Back / Fore-Back
echo Black * [100m[30m30[0m-[4m[40m40 [0m (0) / (8) [90m90[0m-[100m100 [0m
echo Red [31m31[0m-[41m41 [0m (4) / © [91m91[0m-[101m101 [0m
echo Green [32m32[0m-[42m42 [0m (2) / (A) [92m92[0m-[102m102 [0m
echo Yellow [33m33[0m-[90m[43m43 [0m (6) / (E) [93m93[0m-[90m[103m103 [0m
echo Blue [34m34[0m-[44m44 [0m (1) / (9) [94m94[0m-[104m104 [0m
echo Magenta [35m35[0m-[45m45 [0m (5) / (D) [95m95[0m-[105m105 [0m
echo Cyan [36m36[0m-[46m46 [0m (3) / (B) [96m96[0m-[106m106 [0m
echo White * [37m37[0m-[47m47 [0m (7) / (F) [97m97[0m-[7;97m107 [0m
echo.
echo Note: use ESC[0m at the end of (every) line.
echo.
echo COMBINATIONS
echo [7;91mESC[7;91m inverse red foreground color ESC[0m[0m
echo.

ECHO. && PAUSE
exit

============================================================
:: This is a .txt file.
This is just help, as optical example,
when make or modify colorful command prompt echoes.

More info in Source:

[To see links please register here]


To make an ESC special character, (),
open or edit a .txt or .bat or .cmd file,
(hold)L-Alt and (type)027 in NumPad)

STYLES
[0m Reset
[1m Bold
[4m Underline
[7m Inverse

COLORS# (Foreground-Background)
Dark / Light
Fore-Back / Fore-Back
Black 30-40 (0) / (8) 90-100
Red 31-41 (4) / © 91-101
Green 32-42 (2) / (A) 92-102
Yellow 33-43 (6) / (E) 93-103
Blue 34-44 (1) / (9) 94-104
Magenta 35-45 (5) / (D) 95-105
Cyan 36-46 (3) / (B) 96-106
White 37-47 (7) / (F) 97-107

COMBINATIONS
ESC[7;31m inverse red foreground color 0m

Note: use ESC[0m at the end of (every) line.

examples:
@ECHO OFF
ECHO Default Text
ECHO [7m"Text" Inversed Back-Fore colors (7m)[0m
ECHO [101m"Text" in Red Background (101m)[0m
ECHO [91m"Text" in Red Foreground (91m)[0m

============================================================

Also, I figured out that with this way can be change the way the Command Prompt looks like, temporarily or permanently.
The following TEXT code is an example of:

[![SET_PROMPT_Colored.PNG][2]][2]

> This is a .txt file.
>
> Antony's examples:
>
> prompt $Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$S $T$_ $P\$_$G
> gives something like that:
>
> ==================== 19:53:02,73
> C:\Windows\system32\
> >
>
> For All Users & Permanent:
> (if there is space between characters, must double quoted [""])
> SETX PROMPT /M $Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$S$S$T$_$_$S$P\$_$G$S
> gives something like that:
>
> ==================== 9:01:23,17
>
> C:\Windows\system32\
> >
>
> NOTE: Variables created or modified by SETX
> will be available at the next logon session.
>

Now let's give colors to the above examples. **_The result in the image above._**

COLORED PROMPT examples:

For only the current User:

prompt $E[91m$E[40m$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$S $T$E[93m$_ $P\$_$G$E[0m

or

For All Users and Permanently:

SETX PROMPT /M $E[91m$E[40m$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$Q$S$S$T$E[93m$_$_$S$P\$_$G$S$E[0m




[1]:

[2]:


Reply

#20
An option for non windows 10 users that doesn't require calling labels, avoiding the delays that go with doing so.

Below is a macro verison of a findstr colorprint routine

usage - where BF is replaced with the hex digit values of the background / Foreground colors:
%Col%{BF}{"string to print"}

```lang-bat
@Echo off & CD "%TEMP%"
For /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (set "DEL=%%a")
Set "Col=For %%l in (1 2)Do if %%l==2 (Set "_Str="&(For /F "tokens=1,2 Delims={}" %%G in ("!oline!")Do Set "C_Out=%%G" & Set "_Str=%%~H")&(For %%s in (!_Str!)Do Set ".Str=%%s")&( <nul set /p ".=%DEL%" > "!_Str!" )&( findstr /v /a:!C_Out! /R "^$" "!_Str!" nul )&( del " !_Str!" > nul 2>&1 ))Else Set Oline="
Setlocal EnableDelayedExpansion
rem /* concatenation of multiple macro expansions requires the macro to be expanded within it's own code block. */
(%Col%{02}{"green on black,"}) & (%Col%{10}{black on blue})
Echo/& (%Col%{04}{red on black}) & (%Col%{34}{" red on blue"})
Goto :Eof
```

A more robust version of the macro replete with error handling.

```lang-bat
@Echo off & PUSHD "%TEMP%"
rem /* Macro Definitions */
(Set \n=^^^
%= macro newline Do not modify =%
)
(Set LF=^


%= linefeed. Do not modify =%)
If "!![" == "[" (
Echo/%%COL%% macro must be defined prior to delayed expansion being enabled
Goto :end
)
For /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (set "DEL=%%a")
rem /* %hCol% - Alternate color macro; escaped for use in COL macro. No error checking. Usage: (%hCol:?=HEXVALUE%Output String) */
Set "hCol=For %%o in (1 2)Do if %%o==2 (^<nul set /p ".=%DEL%" ^> "!os!" ^& findstr /v /a:? /R "^$" "!os!" nul ^& del "!os!" ^> nul 2^>^&1 )Else Set os="
rem /* %TB% - used with substitution within COL macro to format help output; not fit for general use, */
Set "TB=^&^< nul Set /P "=.%DEL%!TAB!"^&"
rem /* %COL% - main color output macro. Usage: (%COL%{[a-f0-9][a-f0-9]}{String to Print}) */
Set COL=Set "_v=1"^&Set "Oline="^& For %%l in (1 2)Do if %%l==2 (%\n%
If not "!Oline!" == "" (%\n%
Set "_Str="%\n%
For /F "tokens=1,2 Delims={}" %%G in ("!oline!")Do (%\n%
Set "Hex=%%G"%\n%
Set "_Str=%%~H"%\n%
)%\n%
Echo/!Hex!^|findstr /RX "[0-9a-fA-F][0-9a-fA-F]" ^> nul ^|^| (Echo/^&(%hCol:?=04%Invalid - )%TB%(%hCol:?=06%Bad Hex value.)%TB%(%hCol:?=01%%%COL%%{!Hex!}{!_Str!})%TB:TAB=LF%(%hCol:?=02%!Usage!)^&Set "_Str="^&Set "_v=0")%\n%
If not "!_Str!" == "" (%\n%
^<nul set /p ".=%DEL%" ^> "!_Str!"%\n%
findstr /v /a:!Hex! /R "^$" "!_Str!" nul %\n%
del "!_Str!" ^> nul 2^>^&1%\n%
)Else If not !_v! EQU 0 (%\n%
Echo/^&(%hCol:?=04%Invalid -)%TB%(%hCol:?=06%Arg 2 absent.)%TB%(%hCol:?=01%%%COL%%!Oline!)%TB:TAB=LF%(%hCol:?=04%Input is required for output string.)%TB:TAB=LF%(%hCol:?=02%!Usage!)%\n%
)%\n%
)Else (Echo/^&(%hCol:?=04%Invalid -)%TB%(%hCol:?=06%No Args)%TB:TAB=!TAB!!TAB!%(%hCol:?=01%%%COL%%!Oline!)%TB:TAB=LF%(%hCol:?=02%!Usage!))%\n%
)Else Set Oline=
Set "usage=%%COL%%{[a-f0-9][a-f0-9]}{String to Print}"
For /F eol^=^%LF%%LF%^ delims^= %%A in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x09"') do Set "TAB=%%A"
rem /* removes escaping from macros to enable use outside of COL macro */
Set "hCol=%hCol:^=%"
Set "TB=%TB:^=%"
Setlocal EnableDelayedExpansion
rem /* usage examples */
(%COL%{02}{"green on black,"}) & (%COL%{10}{"black on blue"})
Echo/
(%COL%{04}{"red on black"}) & (%COL%{34}{" red on blue"})&(%COL%{40}{"black on red"})
Echo/& %COL%{03}{Demonstration of error handling-}
rem /* error handling */
Echo/%TB:TAB=!LF! % %hCol:?=20%Example 1 - No args
%COL%
Echo/%TB:TAB=!LF! % %hCol:?=20%Example 2 - Missing 2nd Arg
%COL%{ff}
Echo/%TB:TAB=!LF! % %hCol:?=20%Example 3 - Invalid hex value for 1st Arg
%COL%{HF}{string}
Echo/%TB:TAB=!LF! % %hCol:?=0d%Done
:end
POPD
Goto :Eof
```

[![enter image description here][1]][1]


[1]:
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through