M BUZZ CRAZE NEWS
// news

Unable to echo with different colors for batch file

By Daniel Rodriguez

When i run the batch file on VS code, the color does not appear and shows the raw code.

echo off
echo %ESC%[42mCan support unscramble your logs?%ESC%[0m

When i run the code manually, it does not show the color and show the raw code as well.

echo %ESC%[42mCan support unscramble your logs?%ESC%[0m

enter image description here

Any ideas? I'm on Windows 10, without admin rights.

Yesterday i tried to run this batch file on VS code and the color changed.

However for cmd.exe, it will still display the raw code without any colors.

Been referencing to multiple questions e.g.

2 Answers

You need to define the ESC environment variable:

for /F %a in ('echo prompt $E ^| cmd') do @set "ESC=%a"

Then you can use it, e.g. as follows:

echo ^<ESC^>[4m %ESC%[4mUnderline Underline%ESC%[0m
echo ^<ESC^>[32m %ESC%[32mGreen Green Green%ESC%[0m
echo ^<ESC^>[33m %ESC%[33mYellow Yellow Yellow%ESC%[0m

Result:

enter image description here

Test from a batch script using the following code snippet:

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
for /F %%a in ('echo prompt $E ^| cmd') do ( set "ESC=%%a"
)
echo ^<ESC^>[4m %ESC%[4mUnderline DisableDelayedExpansion%ESC%[0m
SETLOCAL EnableDelayedExpansion
echo !ESC![101;93m STYLES !ESC![0m
echo ^<ESC^>[4m !ESC![4mUnderline!ESC![0m
echo ^<ESC^>[0m !ESC![0mReset!ESC![0m
echo ^<ESC^>[1m !ESC![1mBold!ESC![0m
echo ^<ESC^>[7m !ESC![7mInverse!ESC![0m
echo.
echo !ESC![101;93m NORMAL FOREGROUND COLORS !ESC![0m
echo ^<ESC^>[30m !ESC![30mBlack!ESC![0m (black)
echo ^<ESC^>[31m !ESC![31mRed!ESC![0m
echo ^<ESC^>[32m !ESC![32mGreen!ESC![0m
echo ^<ESC^>[33m !ESC![33mYellow!ESC![0m
echo ^<ESC^>[34m !ESC![34mBlue!ESC![0m
echo ^<ESC^>[35m !ESC![35mMagenta!ESC![0m
echo ^<ESC^>[36m !ESC![36mCyan!ESC![0m
echo ^<ESC^>[37m !ESC![37mWhite!ESC![0m
echo.
echo !ESC![101;93m NORMAL BACKGROUND COLORS !ESC![0m
echo ^<ESC^>[40m !ESC![40mBlack!ESC![0m
echo ^<ESC^>[41m !ESC![41mRed!ESC![0m
echo ^<ESC^>[42m !ESC![42mGreen!ESC![0m
echo ^<ESC^>[43m !ESC![43mYellow!ESC![0m
echo ^<ESC^>[44m !ESC![44mBlue!ESC![0m
echo ^<ESC^>[45m !ESC![45mMagenta!ESC![0m
echo ^<ESC^>[46m !ESC![46mCyan!ESC![0m
echo ^<ESC^>[47m !ESC![47mWhite!ESC![0m (white)
echo.
echo !ESC![101;93m STRONG FOREGROUND COLORS !ESC![0m
echo ^<ESC^>[90m !ESC![90mWhite!ESC![0m
echo ^<ESC^>[91m !ESC![91mRed!ESC![0m
echo ^<ESC^>[92m !ESC![92mGreen!ESC![0m
echo ^<ESC^>[93m !ESC![93mYellow!ESC![0m
echo ^<ESC^>[94m !ESC![94mBlue!ESC![0m
echo ^<ESC^>[95m !ESC![95mMagenta!ESC![0m
echo ^<ESC^>[96m !ESC![96mCyan!ESC![0m
echo ^<ESC^>[97m !ESC![97mWhite!ESC![0m
echo.
echo !ESC![101;93m STRONG BACKGROUND COLORS !ESC![0m
echo ^<ESC^>[100m !ESC![100mBlack!ESC![0m
echo ^<ESC^>[101m !ESC![101mRed!ESC![0m
echo ^<ESC^>[102m !ESC![102mGreen!ESC![0m
echo ^<ESC^>[103m !ESC![103mYellow!ESC![0m
echo ^<ESC^>[104m !ESC![104mBlue!ESC![0m
echo ^<ESC^>[105m !ESC![105mMagenta!ESC![0m
echo ^<ESC^>[106m !ESC![106mCyan!ESC![0m
echo ^<ESC^>[107m !ESC![107mWhite!ESC![0m
echo.
echo !ESC![101;93m COMBINATIONS !ESC![0m
echo ^<ESC^>[31m !ESC![31mred foreground color!ESC![0m
echo ^<ESC^>[7m !ESC![7minverse foreground ^<-^> background!ESC![0m
echo ^<ESC^>[7;31m !ESC![7;31minverse red foreground color!ESC![0m
echo ^<ESC^>[7m and nested !ESC![31m !ESC![7mbefore !ESC![31mnested!ESC![0m
echo ^<ESC^>[31m and nested !ESC![7m !ESC![31mbefore !ESC![7mnested!ESC![0m
9

there is of course many ways you can make use of ascii escape codes for color output, including using RGB color schemes. here is one such example.

@Echo Off & Setlocal DisableDelayedExpansion
mode 170,40
::: { Creates variable /AE = Ascii-27 escape code.
::: - %/AE% can be used with and without DelayedExpansion. For /F %%a in ('echo prompt $E ^| cmd')do set "/AE=%%a"
::: }
(Set \n=^^^
%=Newline DNR=%
)
::: / Color Print Macro -
::: Usage: %Print%{RRR;GGG;BBB}text to output
::: \n at the end of the string echo's a new line
::: valid range for RGB values: 0 - 255 Set Print=For %%n in (1 2)Do If %%n==2 (%\n% For /F "Delims=" %%G in ("!Args!")Do (%\n% For /F "Tokens=1 Delims={}" %%i in ("%%G")Do Set "Output=%/AE%[0m%/AE%[38;2;%%im!Args:{%%~i}=!"%\n% ^< Nul set /P "=!Output:\n=!%/AE%[0m"%\n% If "!Output:~-2!"=="\n" (Echo/^&Endlocal)Else (Endlocal)%\n% )%\n% )Else Setlocal EnableDelayedExpansion ^& Set Args=
::: / Erase Macro -
::: Usage: %Erase%{string of the length to be erased} Set Erase=For %%n in (1 2)Do If %%n==2 (%\n% For /F "Tokens=1 Delims={}" %%G in ("!Args!")Do (%\n% Set "Nul=!Args:{%%G}=%%G!"%\n% For /L %%# in (0 1 100) Do (If Not "!Nul:~%%#,1!"=="" ^< Nul set /P "=%/AE%[D%/AE%[K")%\n% )%\n% Endlocal%\n% )Else Setlocal EnableDelayedExpansion ^& Set Args=
rem // usage example %Print%{150;75;50}This is a Demo %Print%{140;60;120} of same line multicolor output.\n %Print%{75;190;150}Includes End Of Line marker. Timeout 1 /NoBreak > Nul %Erase%{marker.} %Print%{150;150;80}marker and erase macro.\n

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy