2

Recently I found out about coloring echo in batch files and everything looked good until I tried it on Windows XP. This bug(?) isn't reproducible on higher versions(at least on w7, w8)

What exactly causes . . to appear after a string when it's called with call :cecho <colors> "<string>"? How to get rid of it and still make it work on winXP and higher?

@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"
)

call :cecho 03 "I am colorful, yay" & echo.
pause
exit

:cecho
@echo off
<nul set /p "=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1

Output:

I am colorful, yay:. .
Press any key to continue . . .

Edit: PS: I'm not interested in external programs, vbs or powershell. Pure batch solution, please.

Edit2:

Thanks to @sokin I found a solution, however @jeb's solution every time creates a file, but doesn't delete it. Therefore simple del fixes it.

@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"
)
<nul > X set /p ".=."

call :color 1a "a"
call :color 1b "b"
call :color 1c "^!<>&| %%%%"*?"
del "%~dp0X"
pause
exit

:color
set "param=^%~2" !
set "param=!param:"=\"!"
findstr /p /A:%1 "." "!param!\..\X" nul
<nul set /p ".=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
Community
  • 1
  • 1
Peter Badida
  • 11,310
  • 10
  • 44
  • 90
  • Try NirCmd (download here: http://www.nirsoft.net/utils/nircmd.html). Use it like this: `nircmdc setconsolecolor 10` - subsequently written text will be printed in green color. –  Mar 12 '16 at 15:13
  • sorry, not interesting in another file for changing color. – Peter Badida Mar 12 '16 at 15:14
  • Sorry, won't have colored text. –  Mar 12 '16 at 15:30
  • Text _is_ already colored... – Peter Badida Mar 12 '16 at 15:32
  • 1
    As mentioned alsoin the linked post, the `.` is caused by `findstr` on WinXP, because the version of this tool outputs `.` for unprintable characters (also for the backspace which is used there to delete previous characters; on Vista for example, `findstr` truly deletes); so the used method does simply not work on WinXP unfortunately... I don't know if there is another reliable method in pure batch... – aschipfl Mar 12 '16 at 15:35
  • By the way, you _do_ use another file for changing color, _plus_ an extra utility (`findstr`). If you really want colored text, why not download a 100-kilobyte program, instead of creating a new temporary file every time you write text to console? –  Mar 12 '16 at 15:40
  • 1
    @gordon Because it's built-in in Windows and I don't have to provide a user with additional file, maybe? – Peter Badida Mar 12 '16 at 15:45
  • An user? Do you seriously provide actual users with batch files? Why not. But then things like this happen. Maybe you colud use VBScript for that matter, or PowerShell? Or just not use colored text. –  Mar 12 '16 at 15:49
  • I may actually provide users even with a ball of mud if it fulfills its function and works on most(in my case all) platforms they use. I haven't had problems with batch since ever. – Peter Badida Mar 12 '16 at 15:52
  • Bud don't you find it a bit annoying at times? Like when you have to create a temporary file, then call `findstr` and then delete the file, just to change the text color? –  Mar 12 '16 at 16:05
  • The only things I find annoying are unportability and too much provided stuff for a simple thing. – Peter Badida Mar 12 '16 at 16:08
  • 2
    @KeyWeeUsr FYI, I just tried jeb's answer from the link in your post, and it works just fine in XP. (The one in the edit section) – sokin Mar 12 '16 at 16:09
  • 1
    @sokin Oops, I missed it. Thank you, now it works. – Peter Badida Mar 12 '16 at 16:13
  • 4
    @KeyWeeUsr: Another annoying thing about using Batch files is the people that insist that you should change to other programming language just because they don't like it! **`;-)`** – Aacini Mar 12 '16 at 18:28

1 Answers1

1

As noted, the findstr command has some strange behaviour.
On XP it replaces the backspace character with dots.

But when the created temporary file contains a dot instead of backspaces, the problem can be solved.
Therefore I build the second solution, but as dbenham mentioned my original code stil can't handle some characters.

Carlos has improved my original idea to a fast and bullet proof version at Dostips: Color function v19

Community
  • 1
  • 1
jeb
  • 78,592
  • 17
  • 171
  • 225