Color Change and Return to Default

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
falcios
Posts: 43
Joined: 02 Mar 2017 05:38

Color Change and Return to Default

#1 Post by falcios » 31 May 2021 13:35

This is an example of some commands in my batch file

copy c:\users\sfd\test1.txt d:\test

echo ---------------------------------
echo ----------COMPLETE----------
echo ---------------------------------

copy c:\users\sfd\test2.txt d:\test

I would like to have the 1st copy command line the default DOS color 06. The echo lines want to change to color 0a. The second copy line change back to DOS default color 06.

Is there a way to do this in DOS?

Thanks in advance.

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: Color Change and Return to Default

#2 Post by AR Coding » 31 May 2021 15:04

Hi, the only way to do that without external tools would be with VT100 which only came in, in Windows 10
here is an example:

Code: Select all

For /F %%a in ('echo Prompt $E ^| cmd') do set ESC=%%a
copy c:\users\sfd\test1.txt d:\test
echo %ESC%[32m --------------------------------- %ESC%[39m
echo %ESC%[32m ----------COMPLETE---------- %ESC%[39m
echo %ESC%[32m --------------------------------- %ESC%[39m
copy c:\users\sfd\test2.txt d:\test
or you could use AnsiSys.exe at viewtopic.php?f=3&t=5772&p=35898#p35898

T3RRY
Posts: 250
Joined: 06 May 2020 10:14

Re: Color Change and Return to Default

#3 Post by T3RRY » 31 May 2021 19:46

AR Coding wrote:
31 May 2021 15:04
Hi, the only way to do that without external tools would be with VT100 which only came in, in Windows 10
This statement is false. Findstr supports the output of colored strings, and unlike VT sequences is not limited to windows 10 v1909 or higher.
For a verbose function that simplifies repetative use of colored output, See Dave Benhams answer here: https://stackoverflow.com/a/10407642/12343998

Note also that VT sequences need only be used when a change in text formatting is required, not with each line of output.

Code: Select all

For /f %%e in ('echo(prompt $E^|cmd')Do Set \E=%%e
<nul set /P "=%\E%[32m"
echo ---------------------------------
echo ----------COMPLETE----------
echo ---------------------------------
<nul set /P "=%\E%[33m"

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: Color Change and Return to Default

#4 Post by AR Coding » 31 May 2021 20:18

Im sorry. I totally forgot about FINDSTR!
Example found at: viewtopic.php?t=2397
But findstr can become slow at times

Post Reply