2

How could I change the color of a specific part of text within a .bat file? I currently have this:

@echo off
:a
color 2
set /p command=$jp 
goto :%command%

which produces a green "$jp". The text I type after that, hovewer is green too, I'd like only the "$jp" to be green and the thing I type in to be white eg. color F

3

As noted by dbenham's comment, this is discussed by StackOverflow's how to have multiple colors in a batch file?

Reviewing that question, it is about Windows 7. The answers there are all requiring extra software, or using some batch file techniques which look... a bit complex.

However, this question doesn't have an operating system tagged, and I'd like to add new information relevant to a newer operating system. Windows 10 has added support for ANSI Escape sequences.

The beauty of ANSI escape sequences is that using them is conceptually easy. You just need to use ANSI color escape sequences (like those documented at Rob van der Woude's page on ANSI Color) and include them in an ECHO statement.

Unfortunately, using these sequences does get to be a little bit harder due to handling of the 27th ASCII character.

Now, the trick to using ANSI Escape sequences is to send the 27th ASCII character to the screen. The reason this is tricky is because the keyboard key that is most commonly related to ASCII 27 is the Esc key. (This is a main reason why these ANSI Escape sequences are often called "escape" sequences.) And a lot of software has special support for the Esc key. This special support often gets in the way of easily just sending a plain ASCII character number 27.

A lot of times people have worked around this by storing the 27th character in a file, and just copy-and-paste the character as needed. Then, making a file with lots of ANSI sequences involves using the 27th ASCII character a bunch of times, which may be doable using copy-and-paste.

However, there is a better approach. Just get the ASCII character into a file once, and get it into an environment variable. This works well in Windows 10 (and, from what I've read, UNIX terminals). Then you can just refer to that environment variable.

I managed to make a batch file using ssed. The batch file, mentioned some more below, simply sets a variable named ASCII 27 to contain that character.

Furthermore, you can create environment variables. So, instead of something like:

echo %ASCII27%[31m Lava %ASCII27%[37m Cyan/Aqua/Teal text %ASCII27%[33m Sun
echo %ASCII27%[34m Water %ASCII27%[37m Sky %ASCII27%[33m Sun

You can use:

SET ANSIREDFG=%ASCII27%[31m
SET ANSICYANFG=%ASCII27%[37m
SET ANSIYELLOWFG=%ASCII27%[33m
SET ANSIBLUEFG=%ASCII27%[34m
echo %ANSIREDFG% Lava %ANSICYANFG% Cyan/Aqua/Teal text %ANSIYELLOWFG% Sun
echo %ANSIBLUEFG% Water %ANSICYANFG% Sky %ANSIYELLOWFG% Sun

That might not look a whole lot shorter. The advantage here isn't necessarily making the file prettier, but so you can use friendlier names (which you decide upon) like %ANSIBLUEFG% to use BLUE as the Foreground color, instead of needing to remember which color is related to which number. That may be a lot faster to type out and debug (if you don't have the color numbers memorized).

I created a ZIP file containing:

  • OUT.ANS: a sample ANSI file to show this off, containing multiple color layouts per line
  • ANSICOL.BAT: a batch file that was used to create that ANSI file, demonstrating usage of variable names instead of frequent numeric references to the color numbers,
  • ASCII27.BAT: creates the ASCII27 variable containing the escape sequence

This ZIP file is at Windows 10 ANSI demonstration, version 1. The batch file was made to work well with Windows 10. Due to use of “%^%” this might not work quite as wonderfully in some older operating systems (but would probably work as expected if replacing “%^%” with “%  %”).

If you don't feel like messing with environment variables, you could try just copying and pasting the codes straight from the out.ans file (which may work marvelously well, if your text editor doesn't corrupt the ASCII 27 character).

  • There is no <BR> after the commands. – Black Thunder Oct 11 '18 at 2:38
  • @BlackThunder : True. Another user edited my answer making some pointless edits, some of which caused the <BR> tags to become visible text (instead of just moving onto the next line), causing the information to become technically incorrect (because it won't work with that version). Thank you for pointing out the problem. I didn't realize that had been done to my answer. And I don't have an option to rollback that edit. – TOOGAM Oct 11 '18 at 12:50
0

Try this script. This is a good option for 32bit systems. You can print any text with any color in any screen position:

http://pastebin.com/bzYhfLGc

  • 1
    Welcome to Super User. External links can break or be unavailable, in which case your answer would not be useful. Please include the essential information within your answer. Thanks. – fixer1234 Nov 11 '16 at 16:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.