View unanswered posts | View active topics It is currently 26 Nov 2014 06:30



Post new topic Reply to topic  [ 27 posts ]  Go to page 1, 2  Next
Color function v19 
Author Message

Joined: 20 Aug 2010 13:57
Posts: 371
Location: Chile
Post Color function v19
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.



27 Nov 2012 15:13
Profile WWW

Joined: 10 Feb 2012 02:20
Posts: 4413
Post Re: Color function v11
It also works in Windows 8 - nice work carlos.


27 Nov 2012 15:53
Profile

Joined: 20 Aug 2010 13:57
Posts: 371
Location: Chile
Post Re: Color function v12
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.


27 Nov 2012 17:17
Profile WWW

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Color function v12
it doesn't work here, i use windows xp, and it give this error
Code:
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


27 Nov 2012 18:01
Profile

Joined: 23 Dec 2011 13:59
Posts: 2016
Post Re: Color function v12
Didn't we have a much smaller color text function on the forums not too long ago?


27 Nov 2012 18:33
Profile

Joined: 20 Aug 2010 13:57
Posts: 371
Location: Chile
Post Re: Color function v13
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.


27 Nov 2012 19:35
Profile WWW

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Color function v13
@ 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:
My TextThe filename, directory name, or volume label syntax is incorrect.


The error was at the last line as you said:
Code:
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.


27 Nov 2012 20:10
Profile

Joined: 20 Aug 2010 13:57
Posts: 371
Location: Chile
Post Re: Color function v14.1
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.


27 Nov 2012 22:08
Profile WWW

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Color function v14.1
This version works great,
I tested this special characters:
Quote:
! @ # $ % ^ & * ( ) _ + | } { " : ? > < , . / ' ; \ ] [ = - ~ `

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


Great job :)


28 Nov 2012 01:00
Profile

Joined: 18 Oct 2012 05:51
Posts: 80
Post Re: Color function v14.1
.
I can set my text the same color as the background...

Can we now use this to hide password characters somehow?


28 Nov 2012 03:31
Profile

Joined: 10 Feb 2012 02:20
Posts: 4413
Post Re: Color function v14.1
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.


28 Nov 2012 03:45
Profile

Joined: 18 Oct 2012 05:51
Posts: 80
Post Re: Color function v14.1
.
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:
@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


28 Nov 2012 03:56
Profile
Expert

Joined: 12 Feb 2011 21:02
Posts: 1275
Location: United States (east coast)
Post Re: Color function v14.1
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:
@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


28 Nov 2012 14:21
Profile

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Color function v14.1
It work here Windows XP sp3
Nice one dbenham :)

Quote:
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


28 Nov 2012 20:30
Profile

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Color function v14.1
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 %%%%%%


28 Nov 2012 20:46
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 27 posts ]  Go to page 1, 2  Next


Who is online

Users browsing this forum: Alexa [Bot], Baidu [Spider], Bing [Bot], penpen and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.