Page 1 of 2
Color function v19
Posted: 27 Nov 2012 15:13
by carlos
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
Re: Color function v11
Posted: 27 Nov 2012 15:53
by foxidrive
It also works in Windows 8 - nice work carlos.
Re: Color function v12
Posted: 27 Nov 2012 17:17
by carlos
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.
Re: Color function v12
Posted: 27 Nov 2012 18:01
by abc0502
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
Re: Color function v12
Posted: 27 Nov 2012 18:33
by Squashman
Didn't we have a much smaller color text function on the forums not too long ago?
Re: Color function v13
Posted: 27 Nov 2012 19:35
by carlos
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.
Re: Color function v13
Posted: 27 Nov 2012 20:10
by abc0502
@ 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:
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
Editjust a note, to start writing in new line end the previous line with: &Echo.
Re: Color function v14.1
Posted: 27 Nov 2012 22:08
by carlos
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.
Re: Color function v14.1
Posted: 28 Nov 2012 01:00
by abc0502
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
Re: Color function v14.1
Posted: 28 Nov 2012 03:31
by Boombox
.
I can set my text the same color as the background...
Can we now use this to hide password characters somehow?
Re: Color function v14.1
Posted: 28 Nov 2012 03:45
by foxidrive
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.
Re: Color function v14.1
Posted: 28 Nov 2012 03:56
by Boombox
.
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
Re: Color function v14.1
Posted: 28 Nov 2012 14:21
by dbenham
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
Re: Color function v14.1
Posted: 28 Nov 2012 20:30
by abc0502
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
Re: Color function v14.1
Posted: 28 Nov 2012 20:46
by abc0502
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 %%%%%%