Page 1 of 3

Color Function 21 22 23c

Posted: 26 Aug 2013 23:03
by carlos
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


Re: Color Function 21

Posted: 26 Aug 2013 23:32
by foxidrive
Working here in Windows 8.

Re: Color Function 21

Posted: 27 Aug 2013 02:10
by penpen
It's running with no problems under Windows XP home & prof versions, too.

penpen

Re: Color Function 21

Posted: 29 Aug 2013 04:47
by brinda
thanks carlos. :D

Re: Color Function 21

Posted: 17 Feb 2014 16:30
by Alfaralf
great post.
Thanks for the tip :wink:

Re: Color Function 21

Posted: 20 Feb 2014 12:25
by miskox
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

Re: Color Function 21

Posted: 20 Feb 2014 17:56
by carlos
@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


Re: Color Function 21 and Color Function 22

Posted: 20 Apr 2015 14:50
by ebloch
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?

Re: Color Function 23

Posted: 08 May 2015 08:34
by carlos
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


Re: Color Function 21 22 23

Posted: 08 May 2015 13:14
by Ben Mar
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

Re: Color Function 21 22 23

Posted: 08 May 2015 15:14
by carlos
@Ben Mar, thanks.
Seems that is fixed replacing this line:

Code: Select all

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

by:

Code: Select all

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

Re: Color Function 21 22 23

Posted: 08 May 2015 15:44
by Ben Mar
Yes, with the new fixed, that bug is taken care of.

Thanks Carlos.

Re: Color Function 21 22 23

Posted: 09 May 2015 07:00
by brinda
thanks carlos

Re: Color Function 21 22 23c

Posted: 09 May 2015 08:11
by carlos
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:& | > < ^

Re: Color Function 21 22 23c

Posted: 10 May 2015 07:59
by miskox
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