Page 1 of 1

Color Change and Return to Default

Posted: 31 May 2021 13:35
by falcios
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.

Re: Color Change and Return to Default

Posted: 31 May 2021 15:04
by AR Coding
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

Re: Color Change and Return to Default

Posted: 31 May 2021 19:46
by T3RRY
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"

Re: Color Change and Return to Default

Posted: 31 May 2021 20:18
by AR Coding
Im sorry. I totally forgot about FINDSTR!
Example found at: viewtopic.php?t=2397
But findstr can become slow at times