输出的文本不能含有特殊字符
@echo offSETLOCAL EnableDelayedExpansionfor /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (set "DEL=%%a")rem echo say the name of the colors, don't read:startrem clscall :ColorText 0a "blue"call :ColorText 0C "green"call :ColorText 0b "red"echo.call :ColorText 19 "yellow"call :ColorText 2F "black"call :ColorText 4e "white"echo.echo aaaaaapausegoto :eof:ColorTextecho off<nul set /p ".=%DEL%" > "%~2"findstr /v /a:%1 /R "^$" "%~2" nuldel "%~2" > nul 2>&1goto :eof
方法二
@echo offcall :colortheword "将着色的字符" 37 "使用默认颜色显示"goto :eofrem 在参数中<>为必选项,[]为可选项,调用参数:<str1=将着色的字符> [str2=颜色设置] [str3=正常显示字符]:colortheword <str1=将着色的字符> [str2=颜色设置] [str3=正常显示字符]set "objFile=%~1"set "objColor=07"&if not "%~2."=="." set "objColor=%~2"set "objMsg= "&if not "%~3."=="." set "objMsg=%~3"for /F %%a in ('"prompt $h & for %%b in (1) do rem"')do set /p="%%a%objMsg%"<nul>"%objFile%"findstr /a:%objColor% .* "%objFile%" nuldel /q "%objFile%" >nul 2>nulgoto :eof
