How to have multiple colors in a Windows batch file? -
i wondering if possible have different colored text on same line in windows batch file, example if says
echo hi world i want "hi" 1 color, , "world" color. maybe set color command variable:
set color1= color 2 set color9= color and deploy them both on same line along
echo hi world but don't know how that.
actually can done without creating temporary file. method described jeb , dbenham work target file contains no backspaces. critical point line recognized findstr.exe must not end crlf. obvious text file scan line not ending crlf invoking batch itself, provided end such line! here's updated example script working way...
changes previous example:
- uses single dash on last line searchable string. (must short , not appear anywhere else in batch.)
- renamed routines , variables little more object-oriented :-)
- removed 1 call level, improve performance.
- added comments (beginning :# more other scripting languages.)
@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 string, final path still refers 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 script directory , search trailing - pushd "%echo.dir%" findstr /p /r /a:%~1 "^^-" "!str!\..\!echo.file!" nul popd :# remove name of script output. (dependant on length.) /l %%n in (1,1,12) if not "!echo.file:~%%n!"=="" <nul set /p "=%echo.del%" :# remove other unwanted characters "\..\: -" <nul set /p "=%echo.del%%echo.del%%echo.del%%echo.del%%echo.del%%echo.del%%echo.del%" :# append 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 store backspace variable. (actually backspace+space+backspace) /f "tokens=1 delims=#" %%a in ('"prompt #$h# & echo on & %%b in (1) rem"') 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 :# following line must last , not end crlf. - ps. i'm having problem output of ! character did not have in previous example. (or @ least did not have same symptoms.) investigated.
Comments
Post a Comment