Skip to content

Instantly share code, notes, and snippets.

@davidruhmann
Last active May 6, 2023 23:32
Show Gist options
  • Save davidruhmann/5040040 to your computer and use it in GitHub Desktop.
Save davidruhmann/5040040 to your computer and use it in GitHub Desktop.
[Batch] Pure Batch Color Routine by David Ruhmann based of DosTips' Carlos
@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
:: Version 19 Plus by David Ruhmann
:: Fixed Unicode usage and cleaned up calls.
@echo off
set "s=°version 19°"
call :Color e0 s \n
pause >nul
exit /b
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::UI/IO
:Color <HexColor> <Variable> [\n]
setlocal EnableExtensions EnableDelayedExpansion
subst `: "!Temp!" >nul &pushd . &`:
if not exist ` (cmd /a /c set /p "=."<nul >` &for /f "delims=;" %%# in (
'"prompt;$H;&for %%_ in (1) do rem"') do cmd /a /c echo set b=%%#>`.bat
for %%# in ("(set n=^" "" ")") do cmd /a /c echo(%%~#>>`.bat)
call ` &set "t=!%~2!" &for %%# in ("!n!"
) do for %%_ in (\ / :) do set "t=!t:%%_=%%~#%%_%%~#!"
for /f usebackq^ delims^=^ eol^= %%# in ('!t:"=\"!') do (
if `==`! setlocal DisableDelayedExpansion
if \==%%# (findstr /a:%~1 . \` nul
set /p "=%b%%b%%b%"<nul
) else if /==%%# (findstr /a:%~1 . /.\` nul
set /p "=%b%%b%%b%%b%%b%"<nul
) else (cmd /a /c echo %%#\..\`>`.dat
findstr /f:`.dat /a:%~1 .
set /p "=%b%%b%%b%%b%%b%%b%%b%"<nul))
if "\n"=="%~3" (echo()
if not `==`! endlocal
popd &subst `: /d >nul &endlocal &exit /b 0
:: Version 20 Plus by David Ruhmann
@echo off
call :Color 9 "##################" \n
call :Color F ""
call :Color A "Final"
call :Color B " Version"
call :Color C " 20+"
call :Color F "" \n
call :Color 9 "##################" \n
pause >nul
exit /b 0
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::UI/IO
:: Version 20 Plus
:: Works in Windows XP, 7, 8.
:: In XP, extended ASCII characters are printed as dots.
:: To print quotes, use empty text "".
:Color <HexColor> <Text> [\n]
setlocal EnableExtensions EnableDelayedExpansion
set "Text=%~2"
if not defined Text (set Text=^")
subst `: "!Temp!" >nul &`: &pushd \
if not exist B7 (
for /f "delims=;" %%A in ('"prompt $H;&for %%B in (_) do rem"') do (
>B3 set /p "=%%A%%A%%A"<nul
>B5 set /p "=%%A%%A%%A%%A%%A"<nul
>B7 set /p "=%%A%%A%%A%%A%%A%%A%%A"<nul)
>N echo.|(pause >nul &findstr "^")
>>N set /p "=."<nul
)
set /p "LF=" <N &set "LF=!LF:~0,1!"
for %%A in ("!LF!") do for %%B in (\ / :) do set "Text=!Text:%%B=%%~A%%B%%~A!"
for /f delims^=^ eol^= %%A in ("!Text!") do (
if #==#! setlocal DisableDelayedExpansion
if \==%%A (
findstr /a:%~1 . \N nul
type B3
) else if /==%%A (
findstr /a:%~1 . /.\N nul
type B5
) else (echo %%A\..\`>_
findstr /f:_ /a:%~1 .
type B7
))
if "\n"=="%~3" echo.
if not #==#! endlocal
popd &subst `: /d >nul &endlocal &exit /b 0
:: Version 21 Plus by David Ruhmann
:: Based off script by Carlos at DosTips
@echo off
call :Color A "######" \n E "" C " 21 " E "!" \n B "######" \n A "\/\:/:" \n
call :Color C "~!@#$&*()_+`-=[]\{}|;':,./<>?" \n
pause >nul
exit /b 0
:: ChangeLog
:: - Replaced CD with PUSHD so that the working directory change is reverted.
:: - Removed existing file check to prevent any bad data scenarios.
:: - The added overhead is worth the data assurance.
:: - Also the overhead is mitigated by proper usage of the parameter overload.
:: - Renamed variables and files to be clearer in meaning and readability.
:: - Added Performance Boost by using Backspace variables rather than files.
:: - Disk IO is much slower even with SSDs than Memory.
:: - Aligned parentheses so that Notepad++ detects matching pairs.
:: - Adjusted spacing and formatting for readability. Max line length = 80
:: - Cleaned up by closing what I opened. ENDLOCAL and POPD
:: - Added exit code to the routine.
:: Notes and Limitations
:: - The \n check still presents a failure point with odd quotations or the non escaped.
:: - ^ will not output as doubled when in a double quoted string.
:: - % will be parsed out and may break the routine.
:: - Odd number of " will break the routine.
:: - Only supports ASCII command line sessions at this time.
:: - I am not totally happy with the SUBST using a non alpha since it is an undocumented hack.
:: ToDo
:: - Add Unicode support.
:: - The Backspace and NewLine pipe and outputs need to be ASCII
:: - Fix the % ^ and " poison characters.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::IO:UI
:: Version 21 Plus
:: Works in Windows 7, 8. Need to re-validate XP
:: In XP, extended ASCII characters are displayed as dots.
:: To print a double quote, pass in an empty string.
:Color <HexColor> [String=" [\n]] ...
setlocal DisableDelayedExpansion
subst `: "%Temp%" >nul &`: &pushd \
for /f "delims=;" %%A in ('"prompt $H;&for %%B in (_) do rem"') do (
set "B3=%%A%%A%%A"
set "B5=%%A%%A%%A%%A%%A"
set "B7=%%A%%A%%A%%A%%A%%A%%A")
echo.|(pause >nul &findstr "^") >N
set /p "=." <nul >>N
:__Color
set "Text=%~2"
if not defined Text (set Text=^")
setlocal EnableDelayedExpansion
set /p "LF=" <N & set "LF=!LF:~0,1!"
for %%A in ("!LF!") do for %%B in (\ / :) do set "Text=!Text:%%B=%%~A%%B%%~A!"
for /f delims^=^ eol^= %%A in ("!Text!") do (
if #==#! endlocal
if \==%%A (
findstr /A:%~1 . \N nul
set /p "=%B3%" <nul
) else if /==%%A (
findstr /A:%~1 . /.\N nul
set /p "=%B5%" <nul
) else (
echo %%A\..\N>_
findstr /F:_ /A:%~1 .
set /p "=%B7%" <nul)
)
if /i "\n"=="%~3" (
shift
echo.
)
shift & shift
set "1=%~1"
if defined 1 goto :__Color
popd & subst `: /d >nul & endlocal & exit /b 0
@echo off
setlocal disableDelayedExpansion
::
:: The following image was adapted from joan stark's ASCII Art Gallery
:: http://www.geocities.com/SoHo/7373/indexjava.htm
::
set q=^"
echo(
echo(
call :c 0E " , .-;" /n
call :c 0E " , |\ / / __," /n
call :c 0E " |\ '.`-.| |.'.-'" /n
call :c 0E " \`'-: `; : /" /n
call :c 0E " `-._'. \'|" /n
call :c 0E " ,_.-=` ` ` ~,_" /n
call :c 0E " '--,. "&call :c 0c ".-. "&call :c 0E ",=!q!." /n
call :c 0E " / "&call :c 0c "{ "&call :c 0A "* "&call :c 0c ")"&call :c 0E "`"&call :c 06 ";-."&call :c 0E "}" /n
call :c 0E " | "&call :c 0c "'-' "&call :c 06 "/__ |" /n
call :c 0E " / "&call :c 06 "\_,\|" /n
call :c 0E " | (" /n
call :c 0E " "&call :c 0c "__ "&call :c 0E "/ ' \" /n
call :c 02 " /\_ "&call :c 0c "/,'`"&call :c 0E "| ' "&call :c 0c ".-~!q!~~-." /n
call :c 02 " |`.\_ "&call :c 0c "| "&call :c 0E "/ ' , "&call :c 0c "/ \" /n
call :c 02 " _/ `, \"&call :c 0c "| "&call :c 0E "; , . "&call :c 0c "| , ' . |" /n
call :c 02 " \ `, "&call :c 0c "| "&call :c 0E "| , , "&call :c 0c "| : ; : |" /n
call :c 02 " _\ `, "&call :c 0c "\ "&call :c 0E "|. , "&call :c 0c "| | | | |" /n
call :c 02 " \` `. "&call :c 0c "\ "&call :c 0E "| ' "&call :c 0A "|"&call :c 0c "\_|-'|_,'\|" /n
call :c 02 " _\ `, "&call :c 0A "`"&call :c 0E "\ ' . ' "&call :c 0A "| | | | | "&call :c 02 "__" /n
call :c 02 " \ `, "&call :c 0E "| , ' "&call :c 0A "|_/'-|_\_/ "&call :c 02 "__ ,-;` /" /n
call :c 02 " \ `, "&call :c 0E "\ . , ' .| | | | | "&call :c 02 "_/' ` _=`|" /n
call :c 02 " `\ `, "&call :c 0E "\ , | | | | |"&call :c 02 "_/' .=!q! /" /n
call :c 02 " \` `, "&call :c 0E "`\ \/|,| ;"&call :c 02 "/' .=!q! |" /n
call :c 02 " \ `, "&call :c 0E "`\' , | ; "&call :c 02 "/' =!q! _/" /n
call :c 02 " `\ `, "&call :c 05 ".-!q!!q!-. "&call :c 0E "': "&call :c 02 "/' =!q! /" /n
call :c 02 " jgs _`\ ;"&call :c 05 "_{ ' ; "&call :c 02 "/' =!q! /" /n
call :c 02 " _\`-/__"&call :c 05 ".~ `."&call :c 07 "8"&call :c 05 ".'.!q!`~-. "&call :c 02 "=!q! _,/" /n
call :c 02 " __\ "&call :c 05 "{ '-."&call :c 07 "|"&call :c 05 ".'.--~'`}"&call :c 02 " _/" /n
call :c 02 " \ .=!q!` "&call :c 05 "}.-~!q!'"&call :c 0D "u"&call :c 05 "'-. '-..' "&call :c 02 "__/" /n
call :c 02 " _/ .!q! "&call :c 05 "{ -'.~('-._,.'"&call :c 02 "\_,/" /n
call :c 02 " / .!q! _/'"&call :c 05 "`--; ; `. ;" /n
call :c 02 " .=!q! _/' "&call :c 05 "`-..__,-'" /n
call :c 02 " __/'" /n
echo(
exit /b
:c
setlocal enableDelayedExpansion
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:colorPrint Color Str [/n]
setlocal
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!"
)
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 (
>colorPrint.txt (echo %%s\..\')
findstr /a:%~1 /f:colorPrint.txt "."
<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%\'"
2>nul del "%temp%\colorPrint.txt"
>nul subst ': /d
exit /b
:: COLORDEMO.CMD ::::::::::::::::
@Echo OFF
SetLocal
If NOT DEFINED %0 (
Set "%0=1"
Start CMD /k%0
Goto :EOF
)
MODE CON: COLS=80 LINES=59
::CLS
Title. Coloring The
Console
Set "Color.A="
Set "Color.B= Coloring The Console"
Set "Color.C="
Call :ColorPrint 0F
Echo. A Demonstration
Set "Color.B= Frank P. Westlake, 2007"
Call :ColorPrint 06
Echo.
Echo.
Set "Color.B=GENERAL"
Call :ColorPrint 0B
Set "Wrap=FINDSTR and SET/P can be used to print text in a selected"
Set "Wrap=%Wrap% color and in the default color both on the same
line."
Set "Wrap=%Wrap% For example:"
Call :Wrap
Set "Wrap="
Echo.
Set "Color.A= Optional text, "
Set "Color.B=color text"
Set "Color.C=, optional text."
Call :ColorPrint 0D
Echo.
Set "Wrap=But because of the nasty colon this technique is probably"
Set "Wrap=%Wrap% only useful when a colon is useful, such as the"
Set "Wrap=%Wrap% following:"
Call :Wrap
Set "Wrap="
Echo.
Set "Color.A="
Set "Color.B= Enabled"
Set "Color.C= Wireless network interface."
Call :ColorPrint 0A
Set "Color.B= Disabled"
Set "Color.C= LAN interface."
Call :ColorPrint 0C
Echo.
Set "Color.A="
Set "Color.B=PROCEDURE"
Set "Color.C="
Call :ColorPrint 0B
Set "Wrap=The programmer sets text into one, two, or three variables
as"
Set "Wrap=%Wrap% follows:"
Call :Wrap
Echo.
Set "Color.B= VARIABLE CONTENTS"
Call :ColorPrint 0B
Set "Wrap.Hang= "
Set "Wrap= Color.A Optional text to be printed in the default
color"
Set "Wrap=%Wrap% preceding the color text."
Call :Wrap
Set "Wrap= Color.B Text to be printed in a selected color."
Call :Wrap
Set "Wrap= Color.C Optional text to be printed in the default
color"
Set "Wrap=%Wrap% following the color text."
Call :Wrap
Set "Wrap.Hang="
Echo.
Set "Wrap=The programmer then calls the :ColorPrint routine with a"
Set "Wrap=%Wrap% color value. For example:"
Call :Wrap
Set "Wrap="
Echo.
Set Wrap= Set "Color.A= Optional text, "
Call :Wrap
Set Wrap= Set "Color.B=color text"
Call :Wrap
Set Wrap= Set "Color.C=, optional text."
Call :Wrap
Set "Wrap= Call :ColorPrint 0D"
Call :Wrap
Echo.
Echo.The output:
Echo.
Set "Color.A= Optional text, "
Set "Color.B=color text"
Set "Color.C=, optional text."
Call :ColorPrint 0D
Echo.
Set "Wrap=The :ColorPrint routine creates a subdirectory in %%TEMP%%"
Set "Wrap=%Wrap% with the name of this script. This is so that there"
Set "Wrap=%Wrap% will be only one file in the directory. Then a file"
Set "Wrap=%Wrap% is created in that directory with it's name being
the"
Set "Wrap=%Wrap% string in the variable Color.B. The file is written"
Set "Wrap=%Wrap% with the contents of the variable Color.C, or only a"
Set "Wrap=%Wrap% newline if that variable is undefined. SET/P is used"
Set "Wrap=%Wrap% to print Color.A without a newline, then FINDSTR is"
Set "Wrap=%Wrap% called to search the file for a line. FINDSTR prints"
Set "Wrap=%Wrap% the filename in the color specified in the CALL"
Set "Wrap=%Wrap% statement, then the contents of the file in the"
Set "Wrap=%Wrap% default color. The file and subdirectory are then
both"
Set "Wrap=%Wrap% deleted."
Call :Wrap
Set "Wrap="
Echo.
Set "Wrap=One undesirable attribute of this technique is that it"
Set "Wrap=%Wrap% writes a file each time the print routine is called."
Call :Wrap
Set "Wrap="
Echo.
Call :Wrap The following characters may not be used in color strings:
Set Invalid="\|*/?:"
Set Invalid
Echo.
Set "Wrap=Experienced CMD script programmers might prefer to call"
Set "Wrap=%Wrap% SET/P and FINDSTR directly as needed."
Call :Wrap
Set "Wrap="
Set "Color.A="
Set "Color.B=Press any key to continue"
Set "Color.C="
Call :ColorPrint CF
PAUSE>NUL:
Exit
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::ColorPrint
::ColorPrint ColorValue
Prints lines in the color specified on the command line with a nasty
colon.
INPUT VARIABLES:
Color.A First portion of line printed without color.
May be undefined.
Color.A Middle portion of line printed with the color specified
on the command line. Must be defined.
Color.C Final portion of line printed without color.
May be undefined.
CHARACTERS:
Valid: 0-9 A-Z a-z `~!@#^$%&()-_+=[]{};',
Invalid: \|*/?:
EXAMPLE:
Set "Color.A="
Set "Color.B=Enabled"
Set "Color.C= Wireless network interface."
Call :ColorPrint 0A
:ColorPrint
MD %Temp%\%~n0
Pushd %Temp%\%~n0
Echo.%Color.C%>"%Color.B%"
Set /P =%Color.A%<NUL:
FindStr /A:%1 /R "^" "%Color.B%*"
Popd
RD /S /Q %Temp%\%~n0
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::Wrap
::Wrap
Reads text on the command line and performs word wrap printing to
STDOUT.
Do NOT use tab characters. A tab is a single character when
determining
line length but it could be up to eight columns when printing; which
might destroy the wordwrap feature for that line.
INPUT VARIABLES:
Wrap Read if there is no text on the command line. Use this
variable instead of the command line when leading spaces
or special characters need to be preserved. Escape
special characters with '^'. For example:
Call :Wrap Pipe COMMAND1 to COMMAND2 using the
syntax:
SET "Wrap= COMMAND1 ^| COMMAND2"
Call :Wrap
Wrap.Indent Set this variable with the amount of spaces necessary
to indent each paragraph. Indent is first line only. For
example:
Set "Wrap.Indent= "
Call :Wrap Two leading spaces.
Wrap.Hang Set this variable with amount of spaces necessary to
indent the paragraph after the first line. For Example:
Set "Wrap.Hang= "
Call :Wrap Paragraph hang indented two spaces.
:Wrap
SetLocal
For /F "tokens=2" %%a in ('MODE^|Find "Columns"') Do (
Set /A Cols=%%a -2)
If "%*" NEQ "" Set "Wrap=%*"
If NOT DEFINED Wrap (
For /F "delims=" %%W in ('MORE') Do Call :Wrap %%W
Goto :EOF
)
Set "Wrap=%Wrap.Indent%%Wrap%"
:Wrap.Loop
CALL Set "prt=%%Wrap:~0,%cols%%%"
CALL Set "Test=%%Wrap:~%cols%%%"
Set /A i=cols
:Adjust
Set /A i=i-1
If NOT DEFINED TEST Goto :Print
If "%prt:~-1,1%" NEQ " " (
Set "prt=%prt:~0,-1%"
Goto :Adjust
)
Set /A i=i+1
:Print
CALL Set "Wrap=%%Wrap:~%i%%%"
Echo.%prt%
If DEFINED Wrap (
Set "Wrap=%Wrap.Hang%%Wrap%"
Goto :Wrap.Loop
)
EndLocal
Goto :EOF
:: END OF COLORDEMO.CMD :::::::::::::::::::::
@echo off
setlocal
call :Echo.Color.Init
goto main
:Echo.Color %1=Color %2=Str [%3=/n]
setlocal enableDelayedExpansion
set "str=%~2"
:Echo.Color.2
:# Replace path separators in the string, so that the final path still refers to the current path.
set "str=a%ECHO.DEL%!str:\=a%ECHO.DEL%\..\%ECHO.DEL%%ECHO.DEL%%ECHO.DEL%!"
set "str=!str:/=a%ECHO.DEL%/..\%ECHO.DEL%%ECHO.DEL%%ECHO.DEL%!"
set "str=!str:"=\"!"
:# Go to the script directory and search for the trailing -
pushd "%ECHO.DIR%"
findstr /p /r /a:%~1 "^^-" "!str!\..\!ECHO.FILE!" nul
popd
:# Remove the name of this script from the output. (Dependant on its length.)
for /l %%n in (1,1,12) do if not "!ECHO.FILE:~%%n!"=="" <nul set /p "=%ECHO.DEL%"
:# Remove the other unwanted characters "\..\: -"
<nul set /p "=%ECHO.DEL%%ECHO.DEL%%ECHO.DEL%%ECHO.DEL%%ECHO.DEL%%ECHO.DEL%%ECHO.DEL%"
:# Append the optional CRLF
if not "%~3"=="" echo.
endlocal & goto :eof
:Echo.Color.Var %1=Color %2=StrVar [%3=/n]
if not defined %~2 goto :eof
setlocal enableDelayedExpansion
set "str=!%~2!"
goto :Echo.Color.2
:Echo.Color.Init
set "ECHO.COLOR=call :Echo.Color"
set "ECHO.DIR=%~dp0"
set "ECHO.FILE=%~nx0"
set "ECHO.FULL=%ECHO.DIR%%ECHO.FILE%"
:# Use prompt to store a backspace into a variable. (Actually backspace+space+backspace)
for /F "tokens=1 delims=#" %%a in ('"prompt #$H# & echo on & for %%b in (1) do rem"') do set "ECHO.DEL=%%a"
goto :eof
:main
call :Echo.Color 0a "a"
call :Echo.Color 0b "b"
set "txt=^" & call :Echo.Color.Var 0c txt
call :Echo.Color 0d "<"
call :Echo.Color 0e ">"
call :Echo.Color 0f "&"
call :Echo.Color 1a "|"
call :Echo.Color 1b " "
call :Echo.Color 1c "%%%%"
call :Echo.Color 1d ^"""
call :Echo.Color 1e "*"
call :Echo.Color 1f "?"
:# call :Echo.Color 2a "!"
call :Echo.Color 2b "."
call :Echo.Color 2c ".."
call :Echo.Color 2d "/"
call :Echo.Color 2e "\"
call :Echo.Color 2f "q:" /n
echo(
set complex="c:\hello world!/.\..\\a//^<%%>&|!" /^^^<%%^>^&^|!\
call :Echo.Color.Var 74 complex /n
exit /b
:# The following line must be last and not end by a CRLF.
-
@echo off
set "s=°version 19 mark 2°"
call :Color e0 s \n
pause >nul
exit /b
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::IO
:: NOTE: Comments must be removed before using.
:: LIMITATION: Currently, only works in ASCII Mode.
:Color <HexColor> <Variable> [\n]
:: Supported in windows XP, 7, 8.
:: In XP, extended ascii characters are printed as dots.
setlocal EnableExtensions EnableDelayedExpansion
:: Create a drive ` and set the working directory to temp?
:: TODO Why is pushd . &`: used over just pushd `: ?
subst `: "!Temp!" >nul &pushd . &`:
:: Create a file named ` containing just a . in the working directory.
:: Create a bat file named `.bat containing variable set commands for the
:: backspace [b] and newline [n] characters.
:: for /f "delims=;" %# in ('"prompt;$H;&for %_ in (1) do rem"') do @echo set b=%#&for %# in ("(set n=^" "" ")") do @echo(%~#
if not exist ` (set /p "=."<nul >` &for /f "delims=;" %%# in (
'"prompt;$H;&for %%_ in (1) do rem"') do echo set b=%%#>`.bat
for %%# in ("(set n=^" "" ")") do echo(%%~#>>`.bat)
:: Run the `.bat creating the two variables b and n.
:: Pull in the variable value into the local variable t.
:: Surround all occurrences of the file name special characters [\ / :] with the
:: newline character. \ becomes n\n
call ` &set "t=!%~2!" &for %%# in ("!n!"
) do for %%_ in (\ / :) do set "t=!t:%%_=%%~#%%_%%~#!"
:: Escape quotations with a backslash.
for /f usebackq^ delims^=^ eol^= %%# in ('!t:"=\"!') do (
rem Check for Delayed Expansion, if on, the exclamation mark ! will be evaluted
rem into nothing, therefore causing the expression to be true.
if `==`! setlocal DisableDelayedExpansion
rem Special handling for the backslash on a single line.
rem TODO figure out why the nul parameter is an undocumented effect.
rem The null is required to force the display of the file path and name.
rem Backspace the file contents . the findstr colon : and the file name `
if \==%%# (findstr /a:%~1 . \` nul
set /p "=%b%%b%%b%"<nul
rem Special handling for the forwardslash on a single line.
rem Backspace the file contents . the findstr colon : the file name ` and the
rem file path characters .\
) else if /==%%# (findstr /a:%~1 . /.\` nul
set /p "=%b%%b%%b%%b%%b%"<nul
rem For all other characters write them out to the `.dat file and then display
rem the file list with findstr.
rem Backspace the ` file contents . the findstr colon : the file name ` and the
rem file path characters \..\
) else (echo %%#\..\`>`.dat
findstr /f:`.dat /a:%~1 .
set /p "=%b%%b%%b%%b%%b%%b%%b%"<nul))
:: NewLine Display option.
if "\n"=="%~3" (echo()
:: Cleanup and Exit
:: TO BE TESTED close the local scopes
if not `==`! endlocal
popd &subst `: /d >nul &endlocal &exit /b 0