Solved Colour code TEXT in a batch file!

Paul Black

I WANT TO BELIEVE!
Guru
VIP
Local time
6:28 PM
Messages
3,823
Location
Planet Earth VGhlIFgtRmlsZXM=
Good afternoon,

I have written a batch file which works great!
However, I would like to colour several pieces of text within the batch file.
I have spent a LONG time researching this on Google and I am almost there.

This is what I have at the top of the code...

Code:
@echo off
 
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)

This is what I have as an example that I want to colour red [which it does]...

Code:
call :ColorText 0c "     IMPORTANT - Numbers 2 blah blah."
call :ColorText 0c "                    If you blah blah!" & set /p ".= " < nul
echo.

This is what I have as the :ColorText code...

Code:
:ColorText
rem echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
GOTO :EOF

The questions I have are...

[1] If I put a colon [:] after Important, is doesn't work, hence the dash [-]?
[2] Why will it not show the exclamation [!] at the end of the line although it is there?
[3] Why will it not show the full stop [.] at the end of the line although it is there?
[4] Is there a way to split the call :ColorText 0c " IMPORTANT - Numbers 2 blah blah." over two lines so it makes it esaier to read and also lines up in the script with the code below it. I tried several options, all to no avail!
[5] As you see from the middle piece of code, the Numbers 2 and If you are not lined up, but they are in the consol!
[6] Why doesn't it put a blank row after even though there is a echo. after the text to be coloured, but it does if I use & set /p ".= " < nul end & echo..
[7] It will also not accept forward slash [/]!

Thanks in advance.
 
Last edited:

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Fujitsu LIFEBOOK
OS
Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
CPU
Intel(R) Pentium(R) CPU P6200 @ 2.13GHz
Motherboard
FUJITSU FJNBB06
Memory
4.00 GB
Graphics Card(s)
Intel(R) Graphics Media Accelerator HD
Sound Card
[1] Realtek High Definition Audio [2] Intel(R) Display Audio
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 59 Hz
Hard Drives
TOSHIBA MK5076GSX
Antivirus
AVG FREE
Does color 2 work


0 = Black 8 = Gray 1 = Blue 9 = Light Blue 2 = Green A = Light Green 3 = Aqua B = Light Aqua 4 = Red C = Light Red 5 = Purple D = Light Purple 6 = Yellow E = Light Yellow 7 = White F = Bright White
 

My Computer

Computer type
PC/Desktop
OS
win 8 32 bit
Hi samuria, thanks for the reply,

Does color 2 work

0 = Black 8 = Gray 1 = Blue 9 = Light Blue 2 = Green A = Light Green 3 = Aqua B = Light Aqua 4 = Red C = Light Red 5 = Purple D = Light Purple 6 = Yellow E = Light Yellow 7 = White F = Bright White

The colour is not the problem!

I think it is the parameters that are stopping what I have posted in the numbered comments/questions!
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Fujitsu LIFEBOOK
OS
Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
CPU
Intel(R) Pentium(R) CPU P6200 @ 2.13GHz
Motherboard
FUJITSU FJNBB06
Memory
4.00 GB
Graphics Card(s)
Intel(R) Graphics Media Accelerator HD
Sound Card
[1] Realtek High Definition Audio [2] Intel(R) Display Audio
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 59 Hz
Hard Drives
TOSHIBA MK5076GSX
Antivirus
AVG FREE
Paul, the Command line DOS language used to write batch files has a number of "protected" or System Characters. There are ways to get around these limitations

Take a look here as a starting point to future research Batch files - Escape Characters also check the left menu for good tips and suggestions for Batch scripting

You may also find a specific editor better for writing code than Notepad Notepad++ is an old one Notepad++ v7.6.6 - Current Version that can be setup to format code and comments to show more clearly

EDIT

Batch Editor download | SourceForge.net is an opensource specific Batch File Editor
 

My Computers

System One System Two

  • Computer type
    PC/Desktop
    Computer Manufacturer/Model Number
    ChillBlast - Custom to my design
    OS
    Windows 11 Pro x64 [Latest Release and Release Preview]
    CPU
    Ryzen 9 5950X, 3.8 - 5.2 MHz
    Motherboard
    Asus Prime X570-Pro
    Memory
    64GB [2 x 32GB] DDR4 3200MHz
    Graphics Card(s)
    4GB NVIDIA GEFORCE GTX 1650 Ti
    Sound Card
    On-board SPDIF to 5.1 System + HDMI [5.1 system]
    Monitor(s) Displays
    32" UHD 32 Bit HDR Monitor + 43" UHD 4K 32Bit HDR TV
    Screen Resolution
    2 x 3840 x 2160 @60Hz
    Hard Drives
    1TB M2 SSD OS, 500GB Fast Access SSD, 2 x 8TB Data + Various Externals from 1TB to 4TB, 10TB NAS
    PSU
    NZXT C750 80 PLUS Gold 750W Modular PSU
    Case
    Workstation Case [Matt Black]
    Cooling
    NZXT Kraken X63 280mm CPU Cooler +2x Quiet Case fans
    Keyboard
    Logitech Wireless MX Keys & K400 + others
    Mouse
    Logitech Wireless MX Master 3S
    Internet Speed
    920 MB Down 50 MB Up
    Antivirus
    BitDefender Total Security Pro
    Browser
    Chrome (always run latest Non-Beta)
    Other Info
    Also run ...
    Laptop - Quad 8GB - Windows 10 Pro x64
    Nexus 7 Android tablet x2
    Samsung 10.2" tablet
    Blackview TAB 8 4G Android Tablet c/w Keyboard
    Wacom Intuos Pro Medium Pen Pad
    Wacom Intuos Pro Small Pen Pad
    Wacom Expresskeys Remote
    Loopdeck+ Graphics Controller
    Shuttle Pro v2 Control
  • Computer type
    Laptop
    System Manufacturer/Model Number
    Dell XPS 17 10750H
    OS
    Windows 11 Pro x64 Latest RP
    CPU
    Intel I7 10750H 5.0GHz
    Motherboard
    Dell XPS
    Memory
    32GB [2x16GB] DDR4 2933 MHz
    Graphics Card(s)
    nVidia GTX1650Ti 4 GB GDDR6
    Sound Card
    Stock [Realtek] 4 Speaker
    Monitor(s) Displays
    17" IPS UHD+ Infinity Edge Touchscreen
    Screen Resolution
    3840 x 2400
    Hard Drives
    2TB M2 NVMe, 4TB External + various 500GB & 1TB External NVMe (also have access to spinner HDD from
    PSU
    Stock
    Case
    Stock XPS Aluminium & Carbon Fibre
    Cooling
    Stock - Active Fan Control
    Keyboard
    Backlit + Various Logitech
    Mouse
    Stock Track Pad + Logitech MX Trackball
    Internet Speed
    72 MB Down 18MB Up
    Browser
    Chrome
    Other Info
    Also run ...
    Laptop - Quad 8GB - Windows 10 Pro x64
    Nexus 7 Android tablet x2
    10.2" tablet
    Sony Z3 Android Smartphone
    Wacom Intuos Pro Medium Pen Pad
    Wacom Intuos Pro Small Pen Pad
    Wacom Expresskeys Remote
    Loopdeck+ Graphics Controller
    Shuttle Pro v2 Control Pad
    10TB NAS
Hi Barman58, thanks for the reply, it is appreciated!

Paul, the Command line DOS language used to write batch files has a number of "protected" or System Characters. There are ways to get around these limitations.

I have had a quick look at the batch link you posted but I need to look at it in more detail.

I only have about a dozen lines of text that I actually want to colour. The thing is, I need exclamation [!], colon [:] and full stop [.] to be accepted within the colour parameters because they are included in the lines of text which are at present after the echo. It is harder to achieve this than I thought it would be.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Fujitsu LIFEBOOK
OS
Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
CPU
Intel(R) Pentium(R) CPU P6200 @ 2.13GHz
Motherboard
FUJITSU FJNBB06
Memory
4.00 GB
Graphics Card(s)
Intel(R) Graphics Media Accelerator HD
Sound Card
[1] Realtek High Definition Audio [2] Intel(R) Display Audio
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 59 Hz
Hard Drives
TOSHIBA MK5076GSX
Antivirus
AVG FREE
You could try a single quote as a delimiter either end of the text string that should designate everything between the quotes as a part of the text or maybe use the ASCII code for the symbols with a delimiter - you may need to use one of the modifiers mentioned in the link -

I wish I could remember how exactly I wrote the Batch Files back in the 80's - used to produce menus for old pre-windows systems to allow the typists to access various programs, without needing to remember the names
 

My Computers

System One System Two

  • Computer type
    PC/Desktop
    Computer Manufacturer/Model Number
    ChillBlast - Custom to my design
    OS
    Windows 11 Pro x64 [Latest Release and Release Preview]
    CPU
    Ryzen 9 5950X, 3.8 - 5.2 MHz
    Motherboard
    Asus Prime X570-Pro
    Memory
    64GB [2 x 32GB] DDR4 3200MHz
    Graphics Card(s)
    4GB NVIDIA GEFORCE GTX 1650 Ti
    Sound Card
    On-board SPDIF to 5.1 System + HDMI [5.1 system]
    Monitor(s) Displays
    32" UHD 32 Bit HDR Monitor + 43" UHD 4K 32Bit HDR TV
    Screen Resolution
    2 x 3840 x 2160 @60Hz
    Hard Drives
    1TB M2 SSD OS, 500GB Fast Access SSD, 2 x 8TB Data + Various Externals from 1TB to 4TB, 10TB NAS
    PSU
    NZXT C750 80 PLUS Gold 750W Modular PSU
    Case
    Workstation Case [Matt Black]
    Cooling
    NZXT Kraken X63 280mm CPU Cooler +2x Quiet Case fans
    Keyboard
    Logitech Wireless MX Keys & K400 + others
    Mouse
    Logitech Wireless MX Master 3S
    Internet Speed
    920 MB Down 50 MB Up
    Antivirus
    BitDefender Total Security Pro
    Browser
    Chrome (always run latest Non-Beta)
    Other Info
    Also run ...
    Laptop - Quad 8GB - Windows 10 Pro x64
    Nexus 7 Android tablet x2
    Samsung 10.2" tablet
    Blackview TAB 8 4G Android Tablet c/w Keyboard
    Wacom Intuos Pro Medium Pen Pad
    Wacom Intuos Pro Small Pen Pad
    Wacom Expresskeys Remote
    Loopdeck+ Graphics Controller
    Shuttle Pro v2 Control
  • Computer type
    Laptop
    System Manufacturer/Model Number
    Dell XPS 17 10750H
    OS
    Windows 11 Pro x64 Latest RP
    CPU
    Intel I7 10750H 5.0GHz
    Motherboard
    Dell XPS
    Memory
    32GB [2x16GB] DDR4 2933 MHz
    Graphics Card(s)
    nVidia GTX1650Ti 4 GB GDDR6
    Sound Card
    Stock [Realtek] 4 Speaker
    Monitor(s) Displays
    17" IPS UHD+ Infinity Edge Touchscreen
    Screen Resolution
    3840 x 2400
    Hard Drives
    2TB M2 NVMe, 4TB External + various 500GB & 1TB External NVMe (also have access to spinner HDD from
    PSU
    Stock
    Case
    Stock XPS Aluminium & Carbon Fibre
    Cooling
    Stock - Active Fan Control
    Keyboard
    Backlit + Various Logitech
    Mouse
    Stock Track Pad + Logitech MX Trackball
    Internet Speed
    72 MB Down 18MB Up
    Browser
    Chrome
    Other Info
    Also run ...
    Laptop - Quad 8GB - Windows 10 Pro x64
    Nexus 7 Android tablet x2
    10.2" tablet
    Sony Z3 Android Smartphone
    Wacom Intuos Pro Medium Pen Pad
    Wacom Intuos Pro Small Pen Pad
    Wacom Expresskeys Remote
    Loopdeck+ Graphics Controller
    Shuttle Pro v2 Control Pad
    10TB NAS
Hi Barman58,

You could try a single quote as a delimiter either end of the text string that should designate everything between the quotes as a part of the text or maybe use the ASCII code for the symbols with a delimiter - you may need to use one of the modifiers mentioned in the link.

Thanks again for the reply.

I have tried numerous combinations of characters etc to try and get this working. I just can't seem to get the correct delimiter!
I will look at the link again later and do some more investigating!
I could live with just getting the full stop to work at the end of the sentences!
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Fujitsu LIFEBOOK
OS
Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
CPU
Intel(R) Pentium(R) CPU P6200 @ 2.13GHz
Motherboard
FUJITSU FJNBB06
Memory
4.00 GB
Graphics Card(s)
Intel(R) Graphics Media Accelerator HD
Sound Card
[1] Realtek High Definition Audio [2] Intel(R) Display Audio
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 59 Hz
Hard Drives
TOSHIBA MK5076GSX
Antivirus
AVG FREE
Hi everyone,

I am still no nearer to resolving this!
Has anyone got any other ideas please?

Thanks in advance.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Fujitsu LIFEBOOK
OS
Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
CPU
Intel(R) Pentium(R) CPU P6200 @ 2.13GHz
Motherboard
FUJITSU FJNBB06
Memory
4.00 GB
Graphics Card(s)
Intel(R) Graphics Media Accelerator HD
Sound Card
[1] Realtek High Definition Audio [2] Intel(R) Display Audio
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 59 Hz
Hard Drives
TOSHIBA MK5076GSX
Antivirus
AVG FREE
As far as I know, what you are trying to do isn't natively supported.Unless you want to use some kind of software add on, you are wasting your time.
 

My Computers

System One System Two

  • Computer type
    PC/Desktop
    OS
    7 X64
    CPU
    i5 8400
    Motherboard
    gigabyte b365m ds3h
    Memory
    2x8gb 3200mhz
    Hard Drives
    various
    PSU
    pure power 11 400w cm
    Case
    Coolermaster
    Cooling
    cryorig m9i
  • Computer type
    PC/Desktop
    OS
    7x64
    CPU
    g5400
    Motherboard
    ga b365m ds3h
    Memory
    8gb ddr4 2400
    PSU
    xfx pro 450w
Hi SIW2,

As far as I know, what you are trying to do isn't natively supported. Unless you want to use some kind of software add on, you are wasting your time.

OK, thanks SIW2, that is good enough for me. It is not important but it would have been nice! I don't want to use some kind of software add on so I will mark this thread as solved.
Thanks again :thumbsup:.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Fujitsu LIFEBOOK
OS
Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
CPU
Intel(R) Pentium(R) CPU P6200 @ 2.13GHz
Motherboard
FUJITSU FJNBB06
Memory
4.00 GB
Graphics Card(s)
Intel(R) Graphics Media Accelerator HD
Sound Card
[1] Realtek High Definition Audio [2] Intel(R) Display Audio
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 59 Hz
Hard Drives
TOSHIBA MK5076GSX
Antivirus
AVG FREE
As far as I know, what you are trying to do isn't natively supported.Unless you want to use some kind of software add on, you are wasting your time.

You are correct; partially.

Batch file scripting (cmd.exe is the process executed) does not allow for individually colored text/background.
The command console (cmd.exe) is just another Windows program and does allow for the standardized native Win32 API calls to individually color (both intense and non-intense) of foreground and background characters.

Strictly using the batch file language will only allow for coloring all of the characters at once (foreground or background).
Without using external programs: I believe there is one method that allows you to link against the DLLs and call them; but the hack's not worth mentioning here.
 

My Computer

Computer type
Laptop
OS
Windows 7 Lite: Professional 64-bit
CPU
Intel amd64
Memory
4GB DDR3
Screen Resolution
1366 x 768
Hard Drives
5400 RPM
Browser
Google Chrome
Other Info
Computer programmer enthusiast and hobbyist
Then why mention it.
 

My Computers

System One System Two

  • Computer type
    PC/Desktop
    OS
    7 X64
    CPU
    i5 8400
    Motherboard
    gigabyte b365m ds3h
    Memory
    2x8gb 3200mhz
    Hard Drives
    various
    PSU
    pure power 11 400w cm
    Case
    Coolermaster
    Cooling
    cryorig m9i
  • Computer type
    PC/Desktop
    OS
    7x64
    CPU
    g5400
    Motherboard
    ga b365m ds3h
    Memory
    8gb ddr4 2400
    PSU
    xfx pro 450w
Batch file scripting hackery (without the use of external programs other than the native Windows console and batch file scripting) does make it possible.

A simple correction for anyone who may decide to investigate further.
 

My Computer

Computer type
Laptop
OS
Windows 7 Lite: Professional 64-bit
CPU
Intel amd64
Memory
4GB DDR3
Screen Resolution
1366 x 768
Hard Drives
5400 RPM
Browser
Google Chrome
Other Info
Computer programmer enthusiast and hobbyist
If you have come across an obscure workaround, then instead of being obtuse, you could link to it.
 

My Computers

System One System Two

  • Computer type
    PC/Desktop
    OS
    7 X64
    CPU
    i5 8400
    Motherboard
    gigabyte b365m ds3h
    Memory
    2x8gb 3200mhz
    Hard Drives
    various
    PSU
    pure power 11 400w cm
    Case
    Coolermaster
    Cooling
    cryorig m9i
  • Computer type
    PC/Desktop
    OS
    7x64
    CPU
    g5400
    Motherboard
    ga b365m ds3h
    Memory
    8gb ddr4 2400
    PSU
    xfx pro 450w
I wasn't trying to be obtuse.

Firstly: I was unaware of whether or not outside links were against the rules.
Secondly: The forum they're on is not open (linking would be senseless).

If the latter were not true: I'd be happy to oblige and post it for the OP.
 

My Computer

Computer type
Laptop
OS
Windows 7 Lite: Professional 64-bit
CPU
Intel amd64
Memory
4GB DDR3
Screen Resolution
1366 x 768
Hard Drives
5400 RPM
Browser
Google Chrome
Other Info
Computer programmer enthusiast and hobbyist
Good afternoon,

I have written a batch file which works great!
However, I would like to colour several pieces of text within the batch file.
I have spent a LONG time researching this on Google and I am almost there.

This is what I have at the top of the code...

Code:
@echo off
 
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)

This is what I have as an example that I want to colour red [which it does]...

Code:
call :ColorText 0c "     IMPORTANT - Numbers 2 blah blah."
call :ColorText 0c "                    If you blah blah!" & set /p ".= " < nul
echo.

This is what I have as the :ColorText code...

Code:
:ColorText
rem echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
GOTO :EOF

The questions I have are...

[1] If I put a colon [:] after Important, is doesn't work, hence the dash [-]?
[2] Why will it not show the exclamation [!] at the end of the line although it is there?
[3] Why will it not show the full stop [.] at the end of the line although it is there?
[4] Is there a way to split the call :ColorText 0c " IMPORTANT - Numbers 2 blah blah." over two lines so it makes it esaier to read and also lines up in the script with the code below it. I tried several options, all to no avail!
[5] As you see from the middle piece of code, the Numbers 2 and If you are not lined up, but they are in the consol!
[6] Why doesn't it put a blank row after even though there is a echo. after the text to be coloured, but it does if I use & set /p ".= " < nul end & echo..
[7] It will also not accept forward slash [/]!

Thanks in advance.

How to have multiple colors in a Windows batch file | Code Q&A Solved

What are the undocumented features and limitations of the Windows findstr command
 

My Computer

Computer type
PC/Desktop
OS
Windows 7 x64, Vista x64, 8.1 smartphone
CPU
Intel E8400 65W 64-bit
Motherboard
Gigabyte EP45-UD3LR
Memory
DDR2 2 x 2GB, 1GB x 2
Graphics Card(s)
XFX Radeon HD5750
Sound Card
AMD High Definition Audio; Realtek High Definition Audio
Monitor(s) Displays
iiyama prolite X2377HDS
Screen Resolution
1920 x 1080
Hard Drives
500GB 7200 rpm Seagate ST3500413AS 16MB, 500GB 5400 rpm Toshiba MQ02ABF050H 32MB, 200GB 7200 rpm Seagate ST3200820AS 8MB, 2TB 7200 rpm Western Digital WD20EZRX 64MB
PSU
Enermax Liberty Modular
Case
Antec P193 Midi Tower
Keyboard
Mionix ZIBAL 60
Mouse
Razer USB 2.0 Diamondback Mouse or Huion Graphics Tablet
Browser
Internet Explorer, Lunascape, Firefox, Opera, Avast Safezone
Back
Top