Color Function 21 22 23c

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 21 22 23c

#1 Post by carlos » 26 Aug 2013 23:03

Note: the latest version 23c is found here: http://www.dostips.com/forum/viewtopic.php?p=41155#p41155

Also I write a utility called out.exe that bypass some limitations of the function color here: http://www.dostips.com/forum/viewtopic.php?p=41335#p41335

Hello, recently I patched the version 20 of the function color. But I found another problem related with the ! symbol. Because this I write the version 21 that have solved the problem and also I add a nice feature: overload parameters for speedy and save lines.

Image

color21.cmd

Code: Select all

@Echo Off
Call :Color A "######" \n E "" C " 21 " E "!" \n B "######" \n
Pause >Nul
Exit /B

:Color
:: v21
:: Arguments: hexColor text [\n] ...
:: \n -> newline ... -> repeat
:: Supported in windows XP, 7, 8.
:: In XP extended ascii characters are printed as dots.
:: For print quotes, use empty text.
SetLocal EnableExtensions EnableDelayedExpansion
Subst `: "!Temp!" >Nul &`: &Cd \
SetLocal DisableDelayedExpansion
If Not Exist `.7 (
Echo(|(Pause >Nul &Findstr "^" >`)
Set /P "=." >>` <Nul
For /F "delims=;" %%# In (
'"Prompt $H;&For %%_ In (_) Do Rem"') Do (
Set /P "=%%#%%#%%#" <Nul >`.3
Set /P "=%%#%%#%%#%%#%%#" <Nul >`.5
Set /P "=%%#%%#%%#%%#%%#%%#%%#" <Nul >`.7))
:__Color
Set "Text=%~2"
If Not Defined Text (Set Text=^")
SetLocal EnableDelayedExpansion
Set /P "LF=" <` &Set "LF=!LF:~0,1!"
For %%# in ("!LF!") Do For %%_ In (
\ / :) Do Set "Text=!Text:%%_=%%~#%%_%%~#!"
For /F delims^=^ eol^= %%# in ("!Text!") Do (
If #==#! EndLocal
If \==%%# (Findstr /A:%~1 . \` Nul
Type `.3) Else If /==%%# (Findstr /A:%~1 . /.\` Nul
Type `.5) Else (Echo %%#\..\`>`.dat
Findstr /F:`.dat /A:%~1 .
Type `.7))
If "\n"=="%~3" (Shift
Echo()
Shift
Shift
If ""=="%~1" Goto :Eof
Goto :__Color

Last edited by carlos on 19 May 2015 14:57, edited 7 times in total.

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

Re: Color Function 21

#2 Post by foxidrive » 26 Aug 2013 23:32

Working here in Windows 8.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Color Function 21

#3 Post by penpen » 27 Aug 2013 02:10

It's running with no problems under Windows XP home & prof versions, too.

penpen

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: Color Function 21

#4 Post by brinda » 29 Aug 2013 04:47

thanks carlos. :D

Alfaralf
Posts: 1
Joined: 17 Feb 2014 16:26

Re: Color Function 21

#5 Post by Alfaralf » 17 Feb 2014 16:30

great post.
Thanks for the tip :wink:

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: Color Function 21

#6 Post by miskox » 20 Feb 2014 12:25

I see that it leaves temporary files.

Code: Select all

`
`.3
`.5
`.7
`.dat


Can you update the code (to v22) so it deletes temporary files, please (so you add it in the right place)?

Code: Select all

@Echo Off
Call :Color A "######" \n E "" C " 21 " E "!" \n B "######" \n
Pause >Nul
Exit /B

:Color
:: v21
:: Arguments: hexColor text [\n] ...
:: \n -> newline ... -> repeat
:: Supported in windows XP, 7, 8.
:: In XP extended ascii characters are printed as dots.
:: For print quotes, use empty text.
SetLocal EnableExtensions EnableDelayedExpansion
Subst `: "!Temp!" >Nul &`: &Cd \
SetLocal DisableDelayedExpansion
If Not Exist `.7 (
Echo(|(Pause >Nul &Findstr "^" >`)
Set /P "=." >>` <Nul
For /F "delims=;" %%# In (
'"Prompt $H;&For %%_ In (_) Do Rem"') Do (
Set /P "=%%#%%#%%#" <Nul >`.3
Set /P "=%%#%%#%%#%%#%%#" <Nul >`.5
Set /P "=%%#%%#%%#%%#%%#%%#%%#" <Nul >`.7))
:__Color
Set "Text=%~2"
If Not Defined Text (Set Text=^")
SetLocal EnableDelayedExpansion
Set /P "LF=" <` &Set "LF=!LF:~0,1!"
For %%# in ("!LF!") Do For %%_ In (
\ / :) Do Set "Text=!Text:%%_=%%~#%%_%%~#!"
For /F delims^=^ eol^= %%# in ("!Text!") Do (
If #==#! EndLocal
If \==%%# (Findstr /A:%~1 . \` Nul
Type `.3) Else If /==%%# (Findstr /A:%~1 . /.\` Nul
Type `.5) Else (Echo %%#\..\`>`.dat
Findstr /F:`.dat /A:%~1 .
Type `.7))
If "\n"=="%~3" (Shift
Echo()
Shift
Shift
If ""=="%~1" del ` `.3 `.5 `.7 `.dat&Goto :Eof
Goto :__Color


Did I make correct modifications?

Saso

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

Re: Color Function 21

#7 Post by carlos » 20 Feb 2014 17:56

@miskox: you are right.

But for post a new version I also fix a problem in all previous versions (all previous versions not works with you use cmd /u).
I fixed in the new version 22 that as you request delete the temp files.

color22.cmd

Code: Select all

@Echo Off
Call :Color A "######" \n E "" C " 22 " E "!" \n B "######" \n
Pause >Nul
Exit /B

:Color
:: v22
:: Arguments: hexColor text [\n] ...
:: \n -> newline ... -> repeat
:: Supported in windows XP, 7, 8.
:: This version works using Cmd /U
:: In XP extended ascii characters are printed as dots.
:: For print quotes, use empty text.
SetLocal EnableExtensions EnableDelayedExpansion
Subst `: "!Temp!" >Nul &`: &Cd \
SetLocal DisableDelayedExpansion
Echo(|(Pause >Nul &Findstr "^" >`)
Cmd /A /D /C Set /P "=." >>` <Nul
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do (
Cmd /A /D /C Set /P "=%%# %%#" <Nul >`.1
Copy /Y `.1 /B + `.1 /B + `.1 /B `.3 /B >Nul
Copy /Y `.1 /B + `.1 /B + `.3 /B `.5 /B >Nul
Copy /Y `.1 /B + `.1 /B + `.5 /B `.7 /B >Nul
)
:__Color
Set "Text=%~2"
If Not Defined Text (Set Text=^")
SetLocal EnableDelayedExpansion
Set /P "LF=" <` &Set "LF=!LF:~0,1!"
For %%# in ("!LF!") Do For %%_ In (
\ / :) Do Set "Text=!Text:%%_=%%~#%%_%%~#!"
For /F delims^=^ eol^= %%# in ("!Text!") Do (
If #==#! EndLocal
If \==%%# (Findstr /A:%~1 . \` Nul
Type `.3) Else If /==%%# (Findstr /A:%~1 . /.\` Nul
Type `.5) Else (Cmd /A /D /C Echo %%#\..\`>`.dat
Findstr /F:`.dat /A:%~1 .
Type `.7))
If "\n"=="%~3" (Shift
Echo()
Shift
Shift
If ""=="%~1" Del ` `.1 `.3 `.5 `.7 `.dat &Goto :Eof
Goto :__Color


ebloch
Posts: 3
Joined: 20 Apr 2015 14:09

Re: Color Function 21 and Color Function 22

#8 Post by ebloch » 20 Apr 2015 14:50

Win 7 Home Premium SP-1 64 bit

I may have found an error/bug:

- - After the following two commands:
SET xc=%Time: =0% Start Test
CALL :COLOR C "%xc%" \n

- - The following command works:
ECHO %Linex% %Time: =0% & ECHO(

- - The following does NOT work:
(ECHO %Linex% %Time: =0%) & ECHO(

Because I frequently use (...) for clarity with & this is a problem that turned up.

Any solution?

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

Re: Color Function 23

#9 Post by carlos » 08 May 2015 08:34

Hello.
I found a little bug con the version 22, when you use special characters like & it fails.
This is the version 23c that have the fix:

Image

Code: Select all

@Echo Off
Call :Color A "######" \n E "" C " 23 " E "!" \n B "#&calc" \n
Pause >Nul
Exit /B


:Color
:: v23c
:: Arguments: hexColor text [\n] ...
:: \n -> newline ... -> repeat
:: Supported in windows XP, 7, 8.
:: This version works using Cmd /U
:: In XP extended ascii characters are printed as dots.
:: For print quotes, use empty text.
SetLocal EnableExtensions EnableDelayedExpansion
Subst `: "!Temp!" >Nul &`: &Cd \
SetLocal DisableDelayedExpansion
Echo(|(Pause >Nul &Findstr "^" >`)
Cmd /A /D /C Set /P "=." >>` <Nul
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do (
Cmd /A /D /C Set /P "=%%# %%#" <Nul >`.1
Copy /Y `.1 /B + `.1 /B + `.1 /B `.3 /B >Nul
Copy /Y `.1 /B + `.1 /B + `.3 /B `.5 /B >Nul
Copy /Y `.1 /B + `.1 /B + `.5 /B `.7 /B >Nul
)
:__Color
Set "Text=%~2"
If Not Defined Text (Set Text=^")
SetLocal EnableDelayedExpansion
For %%_ In ("&" "|" ">" "<"
) Do Set "Text=!Text:%%~_=^%%~_!"
Set /P "LF=" <` &Set "LF=!LF:~0,1!"
For %%# in ("!LF!") Do For %%_ In (
\ / :) Do Set "Text=!Text:%%_=%%~#%%_%%~#!"
For /F delims^=^ eol^= %%# in ("!Text!") Do (
If #==#! EndLocal
If \==%%# (Findstr /A:%~1 . \` Nul
Type `.3) Else If /==%%# (Findstr /A:%~1 . /.\` Nul
Type `.5) Else (Cmd /A /D /C Echo %%#\..\`>`.dat
Findstr /F:`.dat /A:%~1 .
Type `.7))
If "\n"=="%~3" (Shift
Echo()
Shift
Shift
If ""=="%~1" Del ` `.1 `.3 `.5 `.7 `.dat &Goto :Eof
Goto :__Color

Last edited by carlos on 09 May 2015 08:09, edited 1 time in total.

Ben Mar
Posts: 22
Joined: 03 May 2015 10:51

Re: Color Function 21 22 23

#10 Post by Ben Mar » 08 May 2015 13:14

I think I found a bug for version 23:
For the character "^" it prints out double "^^"
Unless I don't use any quotes and put double ^^ then it prints out one ^

@Echo Off

rem this prints out 2 ^^
Call color23.cmd A "######" \n E "^" C " 21 " E "!" \n B "######" \n

rem this prints out 4 ^^^^
Call color23.cmd A "######" \n E "^^" C " 21 " E "!" \n B "######" \n

rem this prints out 1 ^
Call color23.cmd A "######" \n E ^^ C " 21 " E "!" \n B "######" \n

Exit /B

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

Re: Color Function 21 22 23

#11 Post by carlos » 08 May 2015 15:14

@Ben Mar, thanks.
Seems that is fixed replacing this line:

Code: Select all

For %%_ In ("^" "&" "|"

by:

Code: Select all

For %%_ In ("&" "|"

Ben Mar
Posts: 22
Joined: 03 May 2015 10:51

Re: Color Function 21 22 23

#12 Post by Ben Mar » 08 May 2015 15:44

Yes, with the new fixed, that bug is taken care of.

Thanks Carlos.

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: Color Function 21 22 23

#13 Post by brinda » 09 May 2015 07:00

thanks carlos

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

Re: Color Function 21 22 23c

#14 Post by carlos » 09 May 2015 08:11

Thanks, also I forgotten support the special characters > and <
I updated the code to version 23c that have all the fixes supporting the special characters:& | > < ^

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: Color Function 21 22 23c

#15 Post by miskox » 10 May 2015 07:59

Please see this:

Code: Select all

@echo off
call :color 0E "                                            SOME TEXT TO DISPLAY HERE IN COLOR"
echo.
echo.
echo.
call :color 0E "                                      SOME TEXT TO DISPLAY HERE IN COLOR"
echo.
echo.
echo.
call :color 0E "                                       SOME TEXT TO DISPLAY HERE IN COLOR"

goto :EOF

:Color
:: v23c
:: Arguments: hexColor text [\n] ...
:: \n -> newline ... -> repeat
:: Supported in windows XP, 7, 8.
:: This version works using Cmd /U
:: In XP extended ascii characters are printed as dots.
:: For print quotes, use empty text.
SetLocal EnableExtensions EnableDelayedExpansion
Subst `: "!Temp!" >Nul &`: &Cd \
SetLocal DisableDelayedExpansion
Echo(|(Pause >Nul &Findstr "^" >`)
Cmd /A /D /C Set /P "=." >>` <Nul
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do (
Cmd /A /D /C Set /P "=%%# %%#" <Nul >`.1
Copy /Y `.1 /B + `.1 /B + `.1 /B `.3 /B >Nul
Copy /Y `.1 /B + `.1 /B + `.3 /B `.5 /B >Nul
Copy /Y `.1 /B + `.1 /B + `.5 /B `.7 /B >Nul
)
:__Color
Set "Text=%~2"
If Not Defined Text (Set Text=^")
SetLocal EnableDelayedExpansion
For %%_ In ("&" "|" ">" "<"
) Do Set "Text=!Text:%%~_=^%%~_!"
Set /P "LF=" <` &Set "LF=!LF:~0,1!"
For %%# in ("!LF!") Do For %%_ In (
\ / :) Do Set "Text=!Text:%%_=%%~#%%_%%~#!"
For /F delims^=^ eol^= %%# in ("!Text!") Do (
If #==#! EndLocal
If \==%%# (Findstr /A:%~1 . \` Nul
Type `.3) Else If /==%%# (Findstr /A:%~1 . /.\` Nul
Type `.5) Else (Cmd /A /D /C Echo %%#\..\`>`.dat
Findstr /F:`.dat /A:%~1 .
Type `.7))
If "\n"=="%~3" (Shift
Echo()
Shift
Shift
If ""=="%~1" Del ` `.1 `.3 `.5 `.7 `.dat &Goto :Eof
Goto :__Color


Output:

Code: Select all

C:\color_test.cmd
                                            SOME TEXT TO DISPLAY HERE IN COLOR\.



                                      SOME TEXT TO DISPLAY HERE IN COLOR


                                       SOME TEXT TO DISPLAY HERE IN COLOR\..\`:.

C:\>


Also notice different number of empy lines.

This error occurs in previuos versions, too.

Any ideas?

Thanks.
Saso

Post Reply