我知道color bf命令设置了整个命令行窗口的颜色,但我想打印不同颜色的单行。
当前回答
I'm adding an answer to address an issue noted in some comments above: that inline ansi color codes can misbehave when inside a FOR loop (actually, within any parenthesized block of code). The .bat code below demonstrates (1) the use of inline color codes, (2) the color failure that can occur when inline color codes are used in a FOR loop or within a parenthesized block of code, and (3) a solution to the problem. When the .bat code executes, tests 2 and 3 demonstrate the colorcode failure, and test 4 shows no failure because it implements the solution.
[编辑2020-04-07:我发现了另一个解决方案,可能比调用子例程更有效。将FINDSTR短语括在括号中,如下所示:
echo success | (findstr /R success)
ENDEDIT]
注意:根据我的(有限的)经验,颜色代码问题只有在输入被管道输送到代码块中的FINDSTR之后才会出现。这就是下面的.bat重现问题的方式。颜色代码问题可能比输送到FINDSTR之后更普遍。如果有人能解释问题的本质,如果有更好的解决方法,我会很感激。
@goto :main
:resetANSI
EXIT /B
rem The resetANSI subroutine is used to fix the colorcode
rem bug, even though it appears to do nothing.
:main
@echo off
setlocal EnableDelayedExpansion
rem Define some useful colorcode vars:
for /F "delims=#" %%E in ('"prompt #$E# & for %%E in (1) do rem"') do set "ESCchar=%%E"
set "green=%ESCchar%[92m"
set "yellow=%ESCchar%[93m"
set "magenta=%ESCchar%[95m"
set "cyan=%ESCchar%[96m"
set "white=%ESCchar%[97m"
set "black=%ESCchar%[30m"
echo %white%Test 1 is NOT in a FOR loop nor within parentheses, and color works right.
echo %yellow%[Test 1] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
echo %magenta%This is magenta and FINDSTR found and displayed 'success'.%yellow%
echo %green%This is green.
echo %cyan%Test 1 completed.
echo %white%Test 2 is within parentheses, and color stops working after the pipe to FINDSTR.
( echo %yellow%[Test 2] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
echo %magenta%This is supposed to be magenta and FINDSTR found and displayed 'success'.
echo %green%This is supposed to be green.
)
echo %cyan%Test 2 completed.
echo %white%Test 3 is within a FOR loop, and color stops working after the pipe to FINDSTR.
for /L %%G in (3,1,3) do (
echo %yellow%[Test %%G] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
echo %magenta%This is supposed to be magenta and FINDSTR found and displayed 'success'.
echo %green%This is supposed to be green.
)
echo %cyan%Test 3 completed.
echo %white%Test 4 is in a FOR loop but color works right because subroutine :resetANSI is
echo called after the pipe to FINDSTR, before the next color code is used.
for /L %%G in (4,1,4) do (
echo %yellow%[Test %%G] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
call :resetANSI
echo %magenta%This is magenta and FINDSTR found and displayed 'success'.
echo %green%This is green.
)
echo %cyan%Test 4 completed.%white%
EXIT /B
其他回答
我刚从Win 7 Home转换到Win 10 Pro,想要替换我从其他批次调用的批量,以颜色回显信息。回顾上面讨论的内容,我使用以下内容,这将直接取代我的前一批。注意在消息中添加“~”,可以使用带有空格的消息。我不用记住代码,而是用字母来代表我需要的颜色。
如果%2包含空格,则需要“…” 黑色底色:R=红色G=绿色Y=黄色W=白色
ECHO OFF
IF "%1"=="R" ECHO ^[91m%~2[0m
IF "%1"=="G" ECHO ^[92m%~2[0m
IF "%1"=="Y" ECHO ^[93m%~2[0m
IF "%1"=="W" ECHO ^[97m%~2[0m
我想用不同的颜色打印一行。
使用ANSI转义序列。
Windows 10之前-在控制台上不支持ANSI颜色
对于低于10的Windows版本,Windows命令控制台默认情况下不支持输出着色。您可以安装Cmder, ConEmu, ANSICON或Mintty(在GitBash和Cygwin中默认使用)来为Windows命令控制台添加着色支持。
Windows 10 -命令行颜色
从Windows 10开始,Windows控制台默认支持ANSI转义序列和一些颜色。该功能于2015年11月随Threshold 2更新一起发布。
MSDN文档
更新(05-2019):ColorTool允许您更改控制台的配色方案。这是微软终端项目的一部分。
Demo
批处理命令
win10colors。cmd是由Michele Locati写的:
下面的文本去掉了特殊字符,不能正常工作。你必须从这里复制。
@echo off
cls
echo [101;93m STYLES [0m
echo ^<ESC^>[0m [0mReset[0m
echo ^<ESC^>[1m [1mBold[0m
echo ^<ESC^>[4m [4mUnderline[0m
echo ^<ESC^>[7m [7mInverse[0m
echo.
echo [101;93m NORMAL FOREGROUND COLORS [0m
echo ^<ESC^>[30m [30mBlack[0m (black)
echo ^<ESC^>[31m [31mRed[0m
echo ^<ESC^>[32m [32mGreen[0m
echo ^<ESC^>[33m [33mYellow[0m
echo ^<ESC^>[34m [34mBlue[0m
echo ^<ESC^>[35m [35mMagenta[0m
echo ^<ESC^>[36m [36mCyan[0m
echo ^<ESC^>[37m [37mWhite[0m
echo.
echo [101;93m NORMAL BACKGROUND COLORS [0m
echo ^<ESC^>[40m [40mBlack[0m
echo ^<ESC^>[41m [41mRed[0m
echo ^<ESC^>[42m [42mGreen[0m
echo ^<ESC^>[43m [43mYellow[0m
echo ^<ESC^>[44m [44mBlue[0m
echo ^<ESC^>[45m [45mMagenta[0m
echo ^<ESC^>[46m [46mCyan[0m
echo ^<ESC^>[47m [47mWhite[0m (white)
echo.
echo [101;93m STRONG FOREGROUND COLORS [0m
echo ^<ESC^>[90m [90mWhite[0m
echo ^<ESC^>[91m [91mRed[0m
echo ^<ESC^>[92m [92mGreen[0m
echo ^<ESC^>[93m [93mYellow[0m
echo ^<ESC^>[94m [94mBlue[0m
echo ^<ESC^>[95m [95mMagenta[0m
echo ^<ESC^>[96m [96mCyan[0m
echo ^<ESC^>[97m [97mWhite[0m
echo.
echo [101;93m STRONG BACKGROUND COLORS [0m
echo ^<ESC^>[100m [100mBlack[0m
echo ^<ESC^>[101m [101mRed[0m
echo ^<ESC^>[102m [102mGreen[0m
echo ^<ESC^>[103m [103mYellow[0m
echo ^<ESC^>[104m [104mBlue[0m
echo ^<ESC^>[105m [105mMagenta[0m
echo ^<ESC^>[106m [106mCyan[0m
echo ^<ESC^>[107m [107mWhite[0m
echo.
echo [101;93m COMBINATIONS [0m
echo ^<ESC^>[31m [31mred foreground color[0m
echo ^<ESC^>[7m [7minverse foreground ^<-^> background[0m
echo ^<ESC^>[7;31m [7;31minverse red foreground color[0m
echo ^<ESC^>[7m and nested ^<ESC^>[31m [7mbefore [31mnested[0m
echo ^<ESC^>[31m and nested ^<ESC^>[7m [31mbefore [7mnested[0m
Solution for changing the foreground and background colors and writing without new lines. It does not create any temporary files. No special editors are required, so Notepad can be used for editing. The first parameter for the :color subroutine is the color code, the rest of the (optional) parameters are the text to display. If the last parameter is $ then a new line is written at the end. The color codes are the same as for the color command. The :echo subroutine can be used to display a text without new line (unlike regular echo).
@echo off
call :color 4
call :echo Red foreground
call :color 7 " and "
call :color 4f
echo Red background
call :color
echo Back to normal
call :color 70 "Black "
call :color 1 "Blue "
call :color 2 "Green "
call :color 3 "Aqua "
call :color 4 "Red "
call :color 5 "Purple "
call :color 6 "Yellow "
call :color 7 "White "
call :color 8 "Gray "
call :color 9 "LightBlue" $
call :color a "LightGreen "
call :color b "LightAqua "
call :color c "LightRed "
call :color d "LightPurple "
call :color e "LightYellow "
call :color f "BrightWhite " $
call :color 1f Blue back
call :color 2f Green back
call :color 3f Aqua back
call :color 4f Red back
call :color 5f Purple back
call :color 6f Yellow back
call :color 7f White back
call :color 8f Gray back
call :color 9f "LightBlue back" $
call :color a0 LightGreen back
call :color b0 LightAqua back
call :color c0 LightRed back
call :color d0 LightPurple back
call :color e0 LightYellow back
call :color f0 LightWhite back $
call :color
echo %ESC%[4mUnderline%ESC%[0m.
pause
goto :eof
:: Displays a text without new line at the end (unlike echo)
:echo
@<nul set /p ="%*"
@goto :eof
:: Change color to the first parameter (same codes as for the color command)
:: And display the other parameters (write $ at the end for new line)
:color
@echo off
IF [%ESC%] == [] for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
SET color=0%1
IF [%color%] == [0] SET color=07
SET fore=%color:~-1%
SET back=%color:~-2,1%
SET color=%ESC%[
if %fore% LEQ 7 (
if %fore% == 0 SET color=%ESC%[30
if %fore% == 1 SET color=%ESC%[34
if %fore% == 2 SET color=%ESC%[32
if %fore% == 3 SET color=%ESC%[36
if %fore% == 4 SET color=%ESC%[31
if %fore% == 5 SET color=%ESC%[35
if %fore% == 6 SET color=%ESC%[33
if %fore% == 7 SET color=%ESC%[37
) ELSE (
if %fore% == 8 SET color=%ESC%[90
if %fore% == 9 SET color=%ESC%[94
if /i %fore% == a SET color=%ESC%[92
if /i %fore% == b SET color=%ESC%[96
if /i %fore% == c SET color=%ESC%[91
if /i %fore% == d SET color=%ESC%[95
if /i %fore% == e SET color=%ESC%[93
if /i %fore% == f SET color=%ESC%[97
)
if %back% == 0 (SET color=%color%;40) ELSE (
if %back% == 1 SET color=%color%;44
if %back% == 2 SET color=%color%;42
if %back% == 3 SET color=%color%;46
if %back% == 4 SET color=%color%;41
if %back% == 5 SET color=%color%;45
if %back% == 6 SET color=%color%;43
if %back% == 7 SET color=%color%;47
if %back% == 8 SET color=%color%;100
if %back% == 9 SET color=%color%;104
if /i %back% == a SET color=%color%;102
if /i %back% == b SET color=%color%;106
if /i %back% == c SET color=%color%;101
if /i %back% == d SET color=%color%;105
if /i %back% == e SET color=%color%;103
if /i %back% == f SET color=%color%;107
)
SET color=%color%m
:repeatcolor
if [%2] NEQ [$] SET color=%color%%~2
shift
if [%2] NEQ [] if [%2] NEQ [$] SET color=%color% & goto :repeatcolor
if [%2] EQU [$] (echo %color%) else (<nul set /p ="%color%")
goto :eof
你可以用cecho..你也可以用它来嵌入到你的脚本中,这样你就不必随身携带。com或。exe文件了
http://www.codeproject.com/Articles/17033/Add-Colors-to-Batch-Files
这是一个自编译的bat/.net混合物(应该保存为。bat),可以在任何安装了。net框架的系统上使用(即使是最古老的XP/2003安装,也很少看到没有。net框架的windows)。它使用jscript.net编译器创建一个exe,能够打印字符串与不同的背景/前景色仅为当前行。
@if (@X)==(@Y) @end /* JScript comment
@echo off
setlocal
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do (
set "jsc=%%v"
)
if not exist "%~n0.exe" (
"%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0"
)
%~n0.exe %*
endlocal & exit /b %errorlevel%
*/
import System;
var arguments:String[] = Environment.GetCommandLineArgs();
var newLine = false;
var output = "";
var foregroundColor = Console.ForegroundColor;
var backgroundColor = Console.BackgroundColor;
var evaluate = false;
var currentBackground=Console.BackgroundColor;
var currentForeground=Console.ForegroundColor;
//http://stackoverflow.com/a/24294348/388389
var jsEscapes = {
'n': '\n',
'r': '\r',
't': '\t',
'f': '\f',
'v': '\v',
'b': '\b'
};
function decodeJsEscape(_, hex0, hex1, octal, other) {
var hex = hex0 || hex1;
if (hex) { return String.fromCharCode(parseInt(hex, 16)); }
if (octal) { return String.fromCharCode(parseInt(octal, 8)); }
return jsEscapes[other] || other;
}
function decodeJsString(s) {
return s.replace(
// Matches an escape sequence with UTF-16 in group 1, single byte hex in group 2,
// octal in group 3, and arbitrary other single-character escapes in group 4.
/\\(?:u([0-9A-Fa-f]{4})|x([0-9A-Fa-f]{2})|([0-3][0-7]{0,2}|[4-7][0-7]?)|(.))/g,
decodeJsEscape);
}
function printHelp( ) {
print( arguments[0] + " -s string [-f foreground] [-b background] [-n] [-e]" );
print( " " );
print( " string String to be printed" );
print( " foreground Foreground color - a " );
print( " number between 0 and 15." );
print( " background Background color - a " );
print( " number between 0 and 15." );
print( " -n Indicates if a new line should" );
print( " be written at the end of the ");
print( " string(by default - no)." );
print( " -e Evaluates special character " );
print( " sequences like \\n\\b\\r and etc ");
print( "" );
print( "Colors :" );
for ( var c = 0 ; c < 16 ; c++ ) {
Console.BackgroundColor = c;
Console.Write( " " );
Console.BackgroundColor=currentBackground;
Console.Write( "-"+c );
Console.WriteLine( "" );
}
Console.BackgroundColor=currentBackground;
}
function errorChecker( e:Error ) {
if ( e.message == "Input string was not in a correct format." ) {
print( "the color parameters should be numbers between 0 and 15" );
Environment.Exit( 1 );
} else if (e.message == "Index was outside the bounds of the array.") {
print( "invalid arguments" );
Environment.Exit( 2 );
} else {
print ( "Error Message: " + e.message );
print ( "Error Code: " + ( e.number & 0xFFFF ) );
print ( "Error Name: " + e.name );
Environment.Exit( 666 );
}
}
function numberChecker( i:Int32 ){
if( i > 15 || i < 0 ) {
print("the color parameters should be numbers between 0 and 15");
Environment.Exit(1);
}
}
if ( arguments.length == 1 || arguments[1].toLowerCase() == "-help" || arguments[1].toLowerCase() == "-help" ) {
printHelp();
Environment.Exit(0);
}
for (var arg = 1; arg <= arguments.length-1; arg++ ) {
if ( arguments[arg].toLowerCase() == "-n" ) {
newLine=true;
}
if ( arguments[arg].toLowerCase() == "-e" ) {
evaluate=true;
}
if ( arguments[arg].toLowerCase() == "-s" ) {
output=arguments[arg+1];
}
if ( arguments[arg].toLowerCase() == "-b" ) {
try {
backgroundColor=Int32.Parse( arguments[arg+1] );
} catch(e) {
errorChecker(e);
}
}
if ( arguments[arg].toLowerCase() == "-f" ) {
try {
foregroundColor=Int32.Parse(arguments[arg+1]);
} catch(e) {
errorChecker(e);
}
}
}
Console.BackgroundColor = backgroundColor ;
Console.ForegroundColor = foregroundColor ;
if ( evaluate ) {
output=decodeJsString(output);
}
if ( newLine ) {
Console.WriteLine(output);
} else {
Console.Write(output);
}
Console.BackgroundColor = currentBackground;
Console.ForegroundColor = currentForeground;
下面是帮助信息:
例子:
Coloroutput.bat -s "aa\nbb\n\u0025cc" -b 10 -f 3 -n -e
您还可以在这里找到这个脚本。
你也可以检查卡洛斯的颜色功能-> http://www.dostips.com/forum/viewtopic.php?f=3&t=4453
推荐文章
- 在Windows上设置Python simpleHTTPserver
- 如何从批处理文件运行PowerShell脚本
- 在单行命令行中执行多行语句
- 使用“start”命令并将参数传递给已启动的程序
- 查找名称包含字符串的所有文件
- 无法在打开用户映射区段的文件上执行所请求的操作
- 如何编写多行命令?
- 在安装了Resharper的Visual Studio中,键盘快捷键不活跃
- 解析命令行参数的最佳方法?
- 如何配置OpenFileDialog来选择文件夹?
- 复制粘贴在Bash上Ubuntu在Windows上
- 编写器安装错误——当它实际启用时需要ext_curl
- 找到文件名不是在Unix上的特定扩展名结束?
- 如何将字符串复制到剪贴板?
- Nodejs无法在Windows上找到已安装的模块