
批處理輸出彩色字符的函數(shù)colstr此colstr.bat對非彩色字符,合理使用可處理任何字符對于彩色字符也可以最大限度的顯示出來(除了非法文件名的9個(gè)字符/*?<:>|其他皆可彩色顯示)
:_colstr [^<attr^>:<col-str>^|<com-str>^|^<esc-Char^>];... //輸出彩色字符(if %1.==. exit/b)&setlocal enabledelayedexpansion&pushd %tmp%for /f tokens=1* delims=: %%a in (%~1)do (if %%a:%%b.==%%a:%%~b. (set col=%%a(for %%z in (Gray=8;LBlue=9;LGreen=A;LAqua=B;LRed=C;LPurple=D;LYellow=ELWhite=F;Black=0;Blue=1;Green=2;Aqua=3;Red=4;Purple=5;Yellow=6;White=7)do set col=!col:%%~z!)&(if exist %%~b? del/a/q %%~b?2>nul)set/p= <nul>%%~b2>nul&findstr/a:!col! .* %%~b?2>nul 3>&2) else if %1==n (echo.) else if %1==b (set/p=<nul) else if %1==q (set/p=^<nul) else if %1==t (set/p= <nul) else (set/p=%~1<nul))&(if %2. neq . (shift&endlocal&goto:_colstr))&exit/b
下面為colstr的幫助信息,存為bat文件雙擊即可(colstr子過程的代碼稍排版了下便于閱讀)關(guān)于思路,大家都知道set/p= <nul>file + findstr/a:attr .* file?原帖:http://www.cn-dos.net/forum/viewthread.php?tid=38940
@if %1.==. if not %prompt:~-4%==$s$h (cmd /s/kprompt %prompt%$s$h&%0/?&exit)@echo offif %1.==. exit/bif %1.==/?. goto:usageif %1==/1 goto:attr1 if %1==/2 goto:attr2if %1==/4 goto:attr4:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::_colstr [^<colorcode^>:<color str>^|<common str>^|^<escape Char^>];... (if %1.==. exit/b)&setlocal enabledelayedexpansion&pushd %tmp%for /f tokens=1* delims=: %%a in (%~1)do (if %%a:%%b.==%%a:%%~b. (set col=%%afor %%z in (LBlue=9;LGreen=A;LAqua=B;LRed=C;LPurple=D;LYellow=E;LWhite=F;Black=0;Blue=1;Green=2;Aqua=3;Red=4;Purple=5;Yellow=6;White=7;Gray=8; )do set col=!col:%%~z!if exist %%~b? del/a/q %%~b?2>nulset/p= <nul>%%~b2>nul&findstr/a:!col! .* %%~b?2>nul 3>&2) else if %1==n (echo.) else if %1==b (set/p=<nul) else if %1==q (set/p=^<nul) else if %1==t (set/p= <nul &rem 注意=后面不是空格,是制表符) else (set/p=%~1<nul))&(if %2. neq . (shift&endlocal&goto:_colstr))&exit/b::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::usageecho.colstr V1.1 code By plp626 On 09-5-30echo.usage:echo.%~n0 [^<colorcode^>:<color str>^|<common str>^|^<escape Char^>];...echo.echo.^<colorcode^>: echo. 參看findstr /?的a參數(shù),這里顏色屬性16進(jìn)制數(shù)字,echo. 這里的顏色代碼與它后面16進(jìn)制顏色屬性數(shù)字是等效的echo. Black=0 Blue=1 Green=2 Aqua=3 Red=4 Purple=5 echoYellow=6 White=7 Gray=8 LBlue=9 LGreen=AechoLAqua=B LRed=C LPurple=D LYellow=E LWhite=Fecho.^<color string^>:echo. 除了非法文件名外的所有字符echo. 另外,不可以是純“空格”、純“.” 不可以“空格”、“.”結(jié)尾 echo. 如果含有,;=^其中之一給前面加上^^轉(zhuǎn)義(命令行下)echo. 如果含有驚嘆號!需要給它前面加上6個(gè)^^轉(zhuǎn)義(命令行下) echo.^<common string^>:echo. 除了外的所有字符, 注意對驚嘆號的處理要在前面加上^echo.^<escape char^>:echo. q(雙引號)、t(制表符)、b(退格)、n(換行)cmd/c %~n0 c:[注意];多個(gè)字符串時(shí)用;q;c:^;;q;隔開;necho.exapmple:&echo命令行下鍵入:echo. %~n0歡迎發(fā)email:; graygreen:[[email protected]][email protected][/email]; 交流^!;n;echo. echo 將顯示:&echo.cmd/c %~n0 歡迎發(fā)email:; graylgreen:[[email protected]][email protected][/email]; 交流^!;n;n;echo.鍵入 %~n0/1 查看一位數(shù)字的顏色屬性效果echo.鍵入 %~n0/2 查看兩位數(shù)字的顏色屬性效果echo.鍵入 %~n0/4 查看四位數(shù)字的顏色屬性效果&exit/b:attr1for %%b in (LBlue LGreen LAqua LRed LPurple LYellow LWhite Black : Blue Green Aqua Red Purple Yellow White Gray )do if %%b==: (echo.) else cmd/c %~n0 ;%%b:%%becho&&exit/b:attr2setlocal&set attr=0 1 2 3 4 5 6 7 8 9 a b c d e ffor %%a in (%attr%)do echo.&for %%b in (%attr%)do cmd/c %~n0 %%a%%b:[%%a%%b]echo.&endlocal&exit/b:attr4echo 當(dāng)attr為3-4位時(shí),低兩位的作用不變,高兩位則輸出相應(yīng)的邊緣線:cmd/c %~n0 n; 0000 =&for %%b in (1 2 3 : 4 5 6 7 : 8 9 A B : C D E F)do (if %%b==: (echo&echo) elsecmd/c %~n0 0%%b00 = ; 0%%b00:0;)echo.&exit/b