Color function v19

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Color function v19

#1 Post by carlos » 27 Nov 2012 15:13

This is a function for print text in color using native findstr.exe. Handle fine all characters, and works in Windows XP, 7 y 8.

Note: new version 20 here viewtopic.php?f=3&t=4453
Last edited by carlos on 03 Apr 2013 21:08, edited 14 times in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Color function v11

#2 Post by foxidrive » 27 Nov 2012 15:53

It also works in Windows 8 - nice work carlos.

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: Color function v12

#3 Post by carlos » 27 Nov 2012 17:17

Thanks foxidrive for the comment and for the test.
I updated the code for last. Now, in version 12 if you put only the code under the label :Color in a batch file, you can run it even with extensions disabled.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Color function v12

#4 Post by abc0502 » 27 Nov 2012 18:01

it doesn't work here, i use windows xp, and it give this error

Code: Select all

The filename, directory name, or volume label syntax is incorrect.
FINDSTR: Cannot open My Text\..\'


it could be the last changes as foxidrive used it before the changes and i used it after

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Color function v12

#5 Post by Squashman » 27 Nov 2012 18:33

Didn't we have a much smaller color text function on the forums not too long ago?

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: Color function v13

#6 Post by carlos » 27 Nov 2012 19:35

abc0502 thanks for inform the error. Was because the code mount a volume that is not alphabetic and in windows xp Pushd and Cd /d not support it, unlike windows 7.
I posted the version 13 with the error fixed.
This version is more long in code because it support characters like: /\!" and works in windows xp, 7 y 8.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Color function v13

#7 Post by abc0502 » 27 Nov 2012 20:10

@ Squashman, you are correct there is about 3 functions, if you want them i can post them in a topic, but they also have there limitation in the special characters except one "i think"

@carlos
It now work but add extra error after the colored text

Code: Select all

My TextThe filename, directory name, or volume label syntax is incorrect.


The error was at the last line as you said:

Code: Select all

Cd /D "%#%" &EndLocal &Goto :Eof
when removing it and leave only the "goto :eof" it work fine, the directory "%#%" doesn't exist.
The CD /D "%#%" should be deleted to remove the error, but i don't know what this will affect

Nice code BTW :)
Edit
just a note, to start writing in new line end the previous line with: &Echo.

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: Color function v14.1

#8 Post by carlos » 27 Nov 2012 22:08

Thanks abc0502 for the support.
I posted the version 14.1 with fixes. I change the way of usage of the function, now is necessary pass a name of variable with the text.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Color function v14.1

#9 Post by abc0502 » 28 Nov 2012 01:00

This version works great,
I tested this special characters:
! @ # $ % ^ & * ( ) _ + | } { " : ? > < , . / ' ; \ ] [ = - ~ `

Only This 3 needs to be skipped:
% ---> must be skipped like this ---> %%
< ---> must be skipped like this ---> ^<
> ---> must be skipped like this ---> ^>


Great job :)

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Color function v14.1

#10 Post by Boombox » 28 Nov 2012 03:31

.
I can set my text the same color as the background...

Can we now use this to hide password characters somehow?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Color function v14.1

#11 Post by foxidrive » 28 Nov 2012 03:45

If someone hilites the screen with the mouse I think you'd see the password.

There are some solutions for password entry using ascii binaries, if you are interested.

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Color function v14.1

#12 Post by Boombox » 28 Nov 2012 03:56

.
Highlighting does not show the characters.

Yes. I would like to know more please Foxi.

You can't usually loop a change of color. But here it works fine. Why is that?

I mean, to be honest. I am struggling to follow any of this code :(

Code: Select all

@ECHO OFF
Set "text=DosTips.com"
:clear
set fg=0
set bg=9
:start
if %fg%==10 goto clear
if %bg% lss 0 goto clear
Call :Color  %bg%%fg% text
set /a fg+=1
set /a bg-=1
goto start

:Color
:: Version: 14.1
:: Example of usage: Call :Color 9b variableName
SetLocal EnableExtensions EnableDelayedExpansion
If Not Defined ' (Subst ': "%Temp%" >Nul
For /F "delims=;" %%$ in (
'"Prompt;$H;&For %%# in (1) Do Cd."') Do Set "'=%%$"
Set /P "=."<Nul >"%Temp%\'")
Pushd "%Cd%" &': &Set "l=-1" &Set "s="
Set "l=-1" &Set "b=%~1" &Set "t=!%~2!"
If Defined t (Set l=0 &Set "t=!t:^^=^!"
For %%# in (8192 4096 2048 1024
512 256 128 64 32 16 8 4 2 1) Do (Set /A "l|=%%#"
For %%$ In (!l!) Do If "!t:~%%$,1!"=="" Set /A "l&=~%%#"))
For /L %%# in (0,1,%l%) do (Set "c=!t:~%%#,1!" &Set "e=0"
If !c! Equ / (Set e=1) Else If !c! Equ \ (
Set e=1) Else If !c! Equ : (Set e=1)
If !e! Equ 0 (Set "s=!s!!c:"=\"!") Else (
If defined s (Findstr /A:%b% "." "!s!\..\'" Nul
Set /P "=%'%%'%%'%%'%%'%%'%%'%"<Nul &Set s=)
If !c! Neq : (Findstr /A:%b% "." "!c!..\'" Nul
Set /P "=%'%%'%%'%%'%%'%%'%"<Nul
) Else (Findstr /A:%b% "." "!c!\..\'" Nul
Set /P "=%'%%'%%'%%'%%'%%'%%'%"<Nul)))
If defined s (Findstr /A:%b% "." "!s!\..\'" Nul
Set /P "=%'%%'%%'%%'%%'%%'%%'%"<Nul)
Popd &Goto :Eof

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Color function v14.1

#13 Post by dbenham » 28 Nov 2012 14:21

v 14.1 fails if the input variable is "s", and probably fails with others as well.

I've developed a new version that is nearly 2 times faster and solves the above problem.

Code: Select all

@echo off
setlocal disableDelayedExpansion
set "text=Dave's version works in Win XP, 7 & 8 "Hurray!""
call :colorPrintVar 9b text /n
call :colorPrint 3F " /:\ Part 1 "
call :colorPrint 4F " Part 2 /:\" /n
echo Success!
exit /b


:colorPrint  Color  Str  [/n]
if not defined DEL call :initColorPrint
setlocal disableDelayedExpansion
set "s=%~2"
call :colorPrintVar %1 s %3
exit /b


:colorPrintVar  Color  StrVar  [/n]
if not defined DEL call :initColorPrint
setlocal enableDelayedExpansion
pushd .
':
cd \
set "s=!%~2!"
:: The single blank line within the following IN() clause is critical - DO NOT REMOVE
for %%n in (^"^

^") do (
  set "s=!s:\=%%~n\%%~n!"
  set "s=!s:/=%%~n/%%~n!"
  set "s=!s::=%%~n:%%~n!"
)
set "s=!s:"=\"!"
for /f delims^=^ eol^= %%s in ("!s!") do (
  if "!" equ "" setlocal disableDelayedExpansion
  if %%s==\ (
    findstr /a:%~1 "." "\'" nul
    <nul set /p "=%DEL%%DEL%%DEL%"
  ) else if %%s==/ (
    findstr /a:%~1 "." "/.\'" nul
    <nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%"
  ) else (
    findstr /a:%~1 "." "%%s\..\'" nul
    <nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
  )
)
if /i "%~3"=="/n" echo(
popd
exit /b


:initColorPrint
for /f %%A in ('"prompt $H&for %%B in (1) do rem"') do set "DEL=%%A %%A"
<nul >"%temp%\'" set /p "=."
subst ': "%temp%" >nul
exit /b


:cleanupColorPrint
2>nul del "%temp%\'"
>nul subst ': /d
exit /b

I eliminated the string length determination and single character processing by using search and replace to insert linefeed before and after the problematic characters: \ / and :

I then can use FOR /F to process each of the non-empty lines within the string.

I've renamed the function to :colorPrintVar, and created another function :colorPrint that accepts a string literal. I've also added :initColorPrint to enable explicit initialization, as well as the optional :cleanupColorPrint to remove the temp file and SUBST drive.

I've added an /N option that prints newline at the end of the string.

The :colorPrint version should handle most situations. The :colorPrintVar is for esoteric strings that cause quoting and escape problems.

I preserved the incredibally cryptic ': drive letter and ' filename. Put I couldn't bring myself to preserve the ' variable. I replaced it with DEL.

I've only had a chance to test on Windows 7, but it should work on XP and Vista.


Dave Benham

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Color function v14.1

#14 Post by abc0502 » 28 Nov 2012 20:30

It work here Windows XP sp3
Nice one dbenham :)

Can you make the /N option like the other programming lanuages "the option for the new line" \n :)

ok i made it, replaced /n in line 49 with \n very simple :D

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Color function v14.1

#15 Post by abc0502 » 28 Nov 2012 20:46

It can show all special chracter except : % < >
They must be escaped, < > can be escaped by adding ^ befor them, but % escaped by adding 5 percentage sign before it like this %%%%%%

Post Reply