我知道color bf命令设置了整个命令行窗口的颜色,但我想打印不同颜色的单行。
当前回答
你可以用cecho..你也可以用它来嵌入到你的脚本中,这样你就不必随身携带。com或。exe文件了
http://www.codeproject.com/Articles/17033/Add-Colors-to-Batch-Files
其他回答
有一个公认的答案,已经有超过250个赞了。我仍然在贡献的原因是,许多编辑器(例如,我正在使用MS Code)不接受回显所需的转义字符,所有其他解决方案都需要一些第三方(非windows默认)软件。
只使用普通批处理命令的解决方法是使用PROMPT而不是ECHO。PROMPT命令以任何编辑器友好的方式接受转义字符作为$Echaracter序列。(简单地替换ASCII转义码中的Esc)与$E。
下面是一个演示代码:
@ECHO OFF
:: Do not pollute environment with the %prompt.bak% variable
:: ! forgetting ENDLOCAL at the end of the batch leads to prompt corruption
SETLOCAL
:: Old prompt settings backup
SET prompt.bak=%PROMPT%
:: Entering the "ECHO"-like section
:: Forcing prompt to display after every command (see below)
ECHO ON
:: Setting the prompt using the ANSI Escape sequence(s)
:: - Always start with $E[1A, otherwise the text would appear on a next line
:: - Then the decorated text follows
:: - And it all ends with $E30;40m, which makes the following command invisible
:: - assuming default background color of the screen
@ PROMPT $E[1A$E[30;42mHELLO$E[30;40m
:: An "empty" command that forces the prompt to display.
:: The word "rem" is displayed along with the prompt text but is made invisible
rem
:: Just another text to display
@ PROMPT $E[1A$E[33;41mWORLD$E[30;40m
rem
:: Leaving the "ECHO"-like section
@ECHO OFF
:: Or a more readable version utilizing the cursor manipulation ASCII ESC sequences
:: the initial sequence
PROMPT $E[1A
:: formating commands
PROMPT %PROMPT%$E[32;44m
:: the text
PROMPT %PROMPT%This is an "ECHO"ed text...
:: new line; 2000 is to move to the left "a lot"
PROMPT %PROMPT%$E[1B$E[2000D
:: formating commands fro the next line
PROMPT %PROMPT%$E[33;47m
:: the text (new line)
PROMPT %PROMPT%...spreading over two lines
:: the closing sequence
PROMPT %PROMPT%$E[30;40m
:: Looks like this without the intermediate comments:
:: PROMPT $E[1A
:: PROMPT %PROMPT%$E[32;44m
:: PROMPT %PROMPT%This is an "ECHO"ed text...
:: PROMPT %PROMPT%$E[1B$E[2000D
:: PROMPT %PROMPT%$E[33;47m
:: PROMPT %PROMPT%...spreading over two lines
:: PROMPT %PROMPT%$E[30;40m
:: show it all at once!
ECHO ON
rem
@ECHO OFF
:: End of "ECHO"-ing
:: Setting prompt back to its original value
:: - We prepend the settings with $E[37;40m in case
:: the original prompt settings do not specify color
:: (as they don't by default).
:: - If they do, the $E[37;40m will become overridden, anyway.
:: ! It is important to write this command
:: as it is with `ENDLOCAL` and in the `&` form.
ENDLOCAL & PROMPT $E[37;40m%prompt.bak%
EXIT /B 0
注意:唯一的缺点是,如果不明确地知道,这种技术与用户cmd颜色设置(颜色命令或设置)发生冲突。
——希望这能有所帮助,因为这是我在开头提到的原因唯一可以接受的解决方案。--
编辑:
根据评论,我附上另一个受@Jeb启发的片段。它:
演示如何获取和使用“Esc”字符运行时(而不是将其输入到编辑器中)(Jeb的解决方案) 使用“本机”ECHO命令 因此它不会影响本地PROMPT值 演示了给ECHO输出上色不可避免地会影响PROMPT颜色,所以颜色必须重置
@ECHO OFF
:: ! To observe color effects on prompt below in this script
:: run the script from a fresh cmd window with no custom
:: prompt settings
:: Only not to pollute the environment with the %\e% variable (see below)
:: Not needed because of the `PROMPT` variable
SETLOCAL
:: Parsing the `escape` character (ASCII 27) to a %\e% variable
:: Use %\e% in place of `Esc` in the [http://ascii-table.com/ansi-escape-sequences.php]
FOR /F "delims=#" %%E IN ('"prompt #$E# & FOR %%E IN (1) DO rem"') DO SET "\e=%%E"
:: Demonstrate that prompt did not get corrupted by the previous FOR
ECHO ON
rem : After for
@ECHO OFF
:: Some fancy ASCII ESC staff
ECHO [ ]
FOR /L %%G IN (1,1,10) DO (
TIMEOUT /T 1 > NUL
ECHO %\e%[1A%\e%[%%GC%\e%[31;43m.
ECHO %\e%[1A%\e%[11C%\e%[37;40m]
)
:: ECHO another decorated text
:: - notice the `%\e%[30C` cursor positioning sequence
:: for the sake of the "After ECHO" test below
ECHO %\e%[1A%\e%[13C%\e%[32;47mHELLO WORLD%\e%[30C
:: Demonstrate that prompt did not get corrupted by ECHOing
:: neither does the cursor positioning take effect.
:: ! But the color settings do.
ECHO ON
rem : After ECHO
@ECHO OFF
ENDLOCAL
:: Demonstrate that color settings do not reset
:: even when out of the SETLOCAL scope
ECHO ON
rem : After ENDLOCAL
@ECHO OFF
:: Reset the `PROMPT` color
:: - `PROMPT` itself is untouched so we did not need to backup it.
:: - Still ECHOING in color apparently collide with user color cmd settings (if any).
:: ! Resetting `PROMPT` color this way extends the `PROMPT`
:: by the initial `$E[37;40m` sequence every time the script runs.
:: - Better solution then would be to end every (or last) `ECHO` command
:: with the `%\e%[37;40m` sequence and avoid setting `PROMPT` altogether.
:: which makes this technique preferable to the previous one (before EDIT)
:: - I am keeping it this way only to be able to
:: demonstrate the `ECHO` color effects on the `PROMPT` above.
PROMPT $E[37;40m%PROMPT%
ECHO ON
rem : After PROMPT color reset
@ECHO OFF
EXIT /B 0
为非windows 10用户提供了一个不需要调用标签的选项,避免了这样做带来的延迟。
下面是findstr colorprint例程的宏版本
usage -其中BF被替换为背景/前景色的十六进制数字值: %Col%{BF}{"要打印的字符串"}
@Echo off & CD "%TEMP%"
For /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (set "DEL=%%a")
Set "Col=For %%l in (1 2)Do if %%l==2 (Set "_Str="&(For /F "tokens=1,2 Delims={}" %%G in ("!oline!")Do Set "C_Out=%%G" & Set "_Str=%%~H")&(For %%s in (!_Str!)Do Set ".Str=%%s")&( <nul set /p ".=%DEL%" > "!_Str!" )&( findstr /v /a:!C_Out! /R "^$" "!_Str!" nul )&( del " !_Str!" > nul 2>&1 ))Else Set Oline="
Setlocal EnableDelayedExpansion
rem /* concatenation of multiple macro expansions requires the macro to be expanded within it's own code block. */
(%Col%{02}{"green on black,"}) & (%Col%{10}{black on blue})
Echo/& (%Col%{04}{red on black}) & (%Col%{34}{" red on blue"})
Goto :Eof
一个更健壮的宏版本,充满了错误处理。
@Echo off & PUSHD "%TEMP%"
rem /* Macro Definitions */
(Set \n=^^^
%= macro newline Do not modify =%
)
(Set LF=^
%= linefeed. Do not modify =%)
If "!![" == "[" (
Echo/%%COL%% macro must be defined prior to delayed expansion being enabled
Goto :end
)
For /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (set "DEL=%%a")
rem /* %hCol% - Alternate color macro; escaped for use in COL macro. No error checking. Usage: (%hCol:?=HEXVALUE%Output String) */
Set "hCol=For %%o in (1 2)Do if %%o==2 (^<nul set /p ".=%DEL%" ^> "!os!" ^& findstr /v /a:? /R "^$" "!os!" nul ^& del "!os!" ^> nul 2^>^&1 )Else Set os="
rem /* %TB% - used with substitution within COL macro to format help output; not fit for general use, */
Set "TB=^&^< nul Set /P "=.%DEL%!TAB!"^&"
rem /* %COL% - main color output macro. Usage: (%COL%{[a-f0-9][a-f0-9]}{String to Print}) */
Set COL=Set "_v=1"^&Set "Oline="^& For %%l in (1 2)Do if %%l==2 (%\n%
If not "!Oline!" == "" (%\n%
Set "_Str="%\n%
For /F "tokens=1,2 Delims={}" %%G in ("!oline!")Do (%\n%
Set "Hex=%%G"%\n%
Set "_Str=%%~H"%\n%
)%\n%
Echo/!Hex!^|findstr /RX "[0-9a-fA-F][0-9a-fA-F]" ^> nul ^|^| (Echo/^&(%hCol:?=04%Invalid - )%TB%(%hCol:?=06%Bad Hex value.)%TB%(%hCol:?=01%%%COL%%{!Hex!}{!_Str!})%TB:TAB=LF%(%hCol:?=02%!Usage!)^&Set "_Str="^&Set "_v=0")%\n%
If not "!_Str!" == "" (%\n%
^<nul set /p ".=%DEL%" ^> "!_Str!"%\n%
findstr /v /a:!Hex! /R "^$" "!_Str!" nul %\n%
del "!_Str!" ^> nul 2^>^&1%\n%
)Else If not !_v! EQU 0 (%\n%
Echo/^&(%hCol:?=04%Invalid -)%TB%(%hCol:?=06%Arg 2 absent.)%TB%(%hCol:?=01%%%COL%%!Oline!)%TB:TAB=LF%(%hCol:?=04%Input is required for output string.)%TB:TAB=LF%(%hCol:?=02%!Usage!)%\n%
)%\n%
)Else (Echo/^&(%hCol:?=04%Invalid -)%TB%(%hCol:?=06%No Args)%TB:TAB=!TAB!!TAB!%(%hCol:?=01%%%COL%%!Oline!)%TB:TAB=LF%(%hCol:?=02%!Usage!))%\n%
)Else Set Oline=
Set "usage=%%COL%%{[a-f0-9][a-f0-9]}{String to Print}"
For /F eol^=^%LF%%LF%^ delims^= %%A in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x09"') do Set "TAB=%%A"
rem /* removes escaping from macros to enable use outside of COL macro */
Set "hCol=%hCol:^=%"
Set "TB=%TB:^=%"
Setlocal EnableDelayedExpansion
rem /* usage examples */
(%COL%{02}{"green on black,"}) & (%COL%{10}{"black on blue"})
Echo/
(%COL%{04}{"red on black"}) & (%COL%{34}{" red on blue"})&(%COL%{40}{"black on red"})
Echo/& %COL%{03}{Demonstration of error handling-}
rem /* error handling */
Echo/%TB:TAB=!LF! % %hCol:?=20%Example 1 - No args
%COL%
Echo/%TB:TAB=!LF! % %hCol:?=20%Example 2 - Missing 2nd Arg
%COL%{ff}
Echo/%TB:TAB=!LF! % %hCol:?=20%Example 3 - Invalid hex value for 1st Arg
%COL%{HF}{string}
Echo/%TB:TAB=!LF! % %hCol:?=0d%Done
:end
POPD
Goto :Eof
最简单的方法是像这样对powershell进行系统调用:
s=os.system('powershell Write-Host "I am so bored with this. Work already" -ForegroundColor Blue')
否则:
←[94mPff
Windows 10 - TH2及以上版本:
(a.k.a Version 1511, build 10586, release 2015-11-10)
在命令提示符:
echo ^[[32m HI ^[[0m
使用实际按键:echo Ctrl+[[32m HI Ctrl+[[0mEnter .
您应该在它下面看到一个绿色的“HI”。
代码可以在这里找到:
https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
记事本:
要保存到记事本,你可以用Alt+027和numpad键入ESC,然后[32m部分。当我使用笔记本电脑时,另一个技巧是将上面的行重定向到一个文件中,然后剪切和粘贴:
echo echo ^[[32m HI ^[[0m >> batch_file.cmd
你可以用cecho..你也可以用它来嵌入到你的脚本中,这样你就不必随身携带。com或。exe文件了
http://www.codeproject.com/Articles/17033/Add-Colors-to-Batch-Files
推荐文章
- Android-Facebook应用程序的键散列
- 如何在PowerShell中输出一些东西
- 从matplotlib中的颜色映射中获取单个颜色
- 更改UITextField和UITextView光标/插入符颜色
- 如何在命令提示符中使用空格?
- 在Python中如何在Linux和Windows中使用“/”(目录分隔符)?
- 命令行从操作系统级配置中删除环境变量
- 在特定的文件夹中打开Cygwin
- 命令行svn for Windows?
- Gulp命令未找到-安装Gulp后错误
- 如何找到并运行keytool
- 我的Windows应用程序的图标应该包括哪些大小?
- 如何使用xargs复制名称中有空格和引号的文件?
- 在Windows上设置Python simpleHTTPserver
- 如何从批处理文件运行PowerShell脚本