Print colored text in Windows CMD
Dealing with bash in Linux environment is pretty easier than Windows, simple scripts or even any commands.
When I was writing a library release script using msbuild and zip compression, I needed a proper way of showing colors as feedbacks of build result or any error occurance.
There were lots of options, surprisingly๐ , but some of them are using powershell commands or the method doesnโt work with .bat file at vanila windows environment. And also, I needed to make it as functions so that I can reuse them at any time.
Here, this is the final result of my green and red color echo printing functions that works in any circumstances.
Please find other color options in Color palette
:PRINT_GREEN
echo [32m%~1[0m
EXIT /B 0
:PRINT_RED
echo [31m%~1[0m
EXIT /B 0
:CHECK_ERROR_LEVEL
if not %errorlevel%==0 goto RELEASE_FAILED
CALL :PRINT_GREEN "%~1"
EXIT /B 0
โEXIT /B 0โ means you exit the procedure of batch file with errorcode, zero.
which means, you use โCALLโ command to make a subroutine and exit the label at the end. Find extra details from this page.
At the end, used the functions this way.
CALL :CHECK_ERROR_LEVEL "C++ Example Project build successful"
CALL :CHECK_ERROR_LEVEL "C# Example Project build successful"
:RELEASE_FAILED
CALL :PRINT_RED "Build Failed"
goto END
Reference
Leave a comment