如何在 Windows 批处理文件中有多种颜色? [英] How to have multiple colors in a Windows batch file?

查看:38
本文介绍了如何在 Windows 批处理文件中有多种颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能在 Windows 批处理文件的同一行上有不同颜色的文本,例如,如果它说

I was wondering if its possible to have different colored text on the same line in a Windows batch file, for example if it says

echo hi world

我希望hi"是一种颜色,而world"是另一种颜色.也许我可以将 COLOR 命令设置为变量:

I want "hi" to be one color, and "world" to be another color. Maybe I could set the COLOR command as a variable:

set color1= color 2
set color9= color A

然后将它们与

echo hi world

但我不知道我会怎么做.

but I don't know how I would do that.

推荐答案

实际上这可以在不创建临时文件的情况下完成.jeb 和 dbenham 描述的方法即使使用不包含退格的目标文件也能工作.关键点是 findstr.exe 识别的行不能以 CRLF 结尾.因此,要扫描的行不以 CRLF 结尾的明显文本文件是调用批处理本身,前提是我们以这样的行结尾!这是一个以这种方式工作的更新示例脚本......

Actually this can be done without creating a temporary file. The method described by jeb and dbenham will work even with a target file that contains no backspaces. The critical point is that the line recognized by findstr.exe must not end with a CRLF. So the obvious text file to scan with a line not ending with a CRLF is the invoking batch itself, provided that we end it with such a line! Here's an updated example script working this way...

对上一个示例的更改:

  • 在最后一行使用一个破折号作为可搜索字符串.(必须很短,并且不会出现在批处理中的其他任何地方.)
  • 将例程和变量重命名为更加面向对象:-)
  • 删除了一个调用级别,以稍微提高性能.
  • 添加了注释(以 :# 开头,看起来更像大多数其他脚本语言.)
@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.
-

附注.我的输出有问题!您在上一个示例中没有的字符.(或者至少你没有同样的症状.)有待调查.

PS. I'm having a problem with the output of the ! character that you did not have in the previous example. (Or at least you did not have the same symptoms.) To be investigated.

这篇关于如何在 Windows 批处理文件中有多种颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆