批处理文件-有没有办法插入类似分页符的东西来获得不同颜色的文本?

批处理文件-有没有办法插入类似分页符的东西来获得不同颜色的文本?

我正在编写一个批处理文件,想知道是否有办法用不同的文本行来区分颜色以突出显示某些内容。批处理文件必须能够在不同的 PC 上运行,因此无需下载任何东西,只需在批处理文件中使用命令使其他文本行变为不同的颜色,而无需更改屏幕上所有该颜色的文本

颜色 04 (黑色背景和红色文本)

例子:

请选择一个选项<---这一行将是红色文本,黑色背景(颜色O4)

请选择 1 到 xxxxx <---- 接下来的这两行文本为蓝色(颜色 01)

请选择 2 至 xxxxx<------ 此行文字也是蓝色

答案1

对于 Windows 10 PC,以下宏可用于编写菜单脚本,该脚本提供选项列表的彩色输出,并能够突出显示所选选项。对于非 Windows 10 PC,仍可使用该宏,但将禁用彩色输出。

有关在其他 Windows 版本上支持彩色菜单输出的替代方案,请参阅我在 stackoverflow 上回答了一个类似的问题。

==================================================================
@Echo off
:# [Menu] Macro Author: T3RRY = Version: 1.0.5
:# Supports Coloring of menu on Windows 10
:# IMPORTANT - RESERVED VARIABLES: \n [Menu]*
:#  - [Menu] macro variables prefixed with [Menu] to avoid potential conflict with other script variables.
:#  - \n is a standard definition of an escaped linefeed for creating multiline Macro variables.

:# Menu macro escaped for Definition with DelayedExpansion Enabled. Ensures correct Environment:
 If not "!" == "" (
  Setlocal EnableExtensions EnableDelayedExpansion
 )

:# Version control. Assigns flag true if system is windows 10. Flag used to apply color with Virtual terminal codes.
 Set "[menu]Win10="
 Ver | Findstr /LIC:" 10." > nul && Set "[menu]Win10=true"

:# Test if virtual terminal codes enabled ; enable if false
:# removes [menu]win10 flag definition if version does not support Virtual Terminal sequences
 If defined [menu]Win10 (
  Reg Query HKCU\Console | %SystemRoot%\System32\findstr.exe /LIC:"VirtualTerminalLevel    REG_DWORD    0x1" > nul || (
    Reg Add HKCU\Console /f /v VirtualTerminalLevel /t REG_DWORD /d 1
  ) > Nul || Set "[menu]Win10="
 )

(Set \n=^^^

%= Newline var \n for multi-line macro definition - Do not modify. =%)

:# Key index list Allows maximum 36 menu options [ 0 indexed ]. Component of Menu Macro
 Set "[menu]Key_List=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

:# Get console width for dividing line
 for /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| %SystemRoot%\System32\findstr.exe /LIC:"Columns"`) do Set /A "[menu]Console_Width=%%W"

:# Build Menu option color array [ RGB colormix ; red biased scaling darker. ]
 If defined [menu]Win10 For /F %%E in ('Echo prompt $E^|cmd')Do Set "\E=%%E"

 Set "[menu]i=0"
 If defined [menu]Win10 (
  Set "[Menu]Color_Off=%\E%[0m"
  For /L %%i in (232 -4 96)Do (
   Set /A "[menu]BB=(%%i/4)+50,[menu]GG=%%i-[menu]BB,[menu]RR=[menu]GG+[menu]BB+(%%i/2)"
   Set "[menu]Color[![menu]i!]=%\E%[38;2;![menu]RR!;![menu]GG!;![menu]BB!m"
   Set /A [menu]i+=1
 ))
 Set "[menu]i="

:# Define highlight color to use for a selected option
 If defined [menu]Win10 (
  Set "[menu]Highlight_Color=%\E%[0m%\E%[48;2;50;150;200m%\E%[31m%\E%[K"
 )

:# Assign Flag true to enable Highlighting of selected option.
:# - Clears screen before menu is output in order to count line position of options.
:# - Highlight will only be seen if the screen output is visible for a noticible length of time after
:# - expansion of the [Menu] macro. Use 'Pause', 'Timeout' or another method of effecting a delay if the screen buffer
:# - will be refreshed or scrolled by subsequent output immediately after menu selection to allow highlight to be seen.
:# - If not defined as true, screen will not be cleared prior to menu output and selection will not be highlighted.
 If defined [menu]Win10 (
  Set "[menu]Highlight=true"
 )

:# Force console line dimensions to a size that supports highlight mode by preventing buffer scroll
 If /I "%[menu]Highlight%" == "true" (
  for /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| %SystemRoot%\System32\findstr.exe /LIC:"Lines"`) do If %%W LSS 40 Mode ![menu]Console_Width!,40
 )

:# Build dividing line for menu output.
 Set "[menu]Divider=" & For /L %%i in (2 1 %[menu]Console_Width%)Do Set "[menu]Divider=![menu]Divider!-"

:# Define dividing line Color
 If defined [menu]Win10 (
  Set "[menu]Divider=%\E%[33m%\E%[4m![menu]Divider!%\E%[0m"
 )

:# Menu macro Usage: %Menu% "quoted" "list of" "options"
:# Supports 36 options [ options are zero indexed ]

     Set [Menu]=For %%n in (1 2)Do if %%n==2 (%\n%
%= Clear screen if highlight flag true  =%  If /I "^![menu]Highlight^!" == "true" CLS%\n%
%= Output Dividing Line                 =%  Echo(^^![menu]Divider^^!%\n%
%= Reset Index value for Opt[#]         =%  Set "[menu]Index=0"%\n%
%= Undefine choice option key list      =%  Set "[menu]Choice_Keys="%\n%
%= For Each in list;                    =%  For %%G in (^^![menu]Options^^!)Do If not ^^![menu]Index^^! GTR 35 (%\n%
%= For Option Index value               =%   For %%i in (^^![menu]Index^^!)Do (%\n%
%= Build the Choice key list and Opt[#] =%    Set "[menu]Choice_Keys=^![menu]Choice_Keys^!^![menu]Key_List:~%%i,1^!"%\n%
%= arrays using the character at the    =%    Set "[menu]Opt[^![menu]Key_List:~%%i,1^!]=%%~G"%\n%
%= current substring index.             =%    Set "[menu]Option_Output=%%~G"%\n%
%= Display ; removing # variable prefix =%    Echo(^^![menu]Color[%%i]^^![^^![menu]Key_List:~%%i,1^^!] ^^![menu]Option_Output:#=^^!![menu]Color_Off!%\n%
%= Store line number of options         =%    Set "[menu]Line#^![menu]Key_List:~%%i,1^!=%%i"%\n%
%= Increment Opt[#] Index var 'Index'   =%    Set /A "[menu]Index+=1"%\n%
%= Close Index loop                     =%   )%\n%
%= Close Options loop                   =%  )%\n%
%= Output Dividing Line                 =%  Echo(^^![menu]Divider^^!%\n%
%= Select option by character index     =%  For /F "Delims=" %%o in ('%SystemRoot%\System32\Choice.exe /N /C:^^![menu]Choice_Keys^^!')Do (%\n%
%= Assign return var 'OPTION' with the  =%   Set "[menu]Selection=^![menu]Opt[%%o]^!"%\n%
%= Highlight selected option with Line# =%   If /I "^![menu]Highlight^!" == "true" (For /F "Delims=" %%X in ("^![menu]Line#%%o^!")Do For /F "Delims=" %%Y in ('Set /A "%%X+2"')Do Echo(%\E%[%%Y;1H^^![menu]Highlight_Color^^!{^^![menu]Key_List:~%%X,1^^!} ^^![menu]Opt[%%o]^^!![menu]Color_Off!)%\n%
%= value selected from Opt[#] array.    =%   If /I "^![menu]Selection^!" == "Exit"   CLS ^& Exit /B 1%\n%
%= Exit type determines Errorlevel.     =%   If /I "^![menu]Selection^!" == "Previous" CLS ^& Exit /B 0%\n%
%= Return to previous script on Exit    =%  )%\n%
%= Move cursor to end of menu field     =%  If /I "^![menu]Highlight^!" == "true" For /F "Delims=" %%Y in ('Set /A [menu]Index + 2')Do Echo(%\E%[%%Y;1H%\n%
%= Capture Macro input - Options List   =% )Else Set [menu]Options=
========================================== :# End Definition of Menu Macro

:# Usage example
:Menu
 %[Menu]% Exit Demo list of options to select from. Each Displayed with different color tone and index key for selecting the desired choice. "Doublequote multiword options"
 Pause
Goto :Menu
:# Selected option returned in '[menu]Selection' variable. 

答案2

@T3RR0R

非常感谢。您是专门为我输入这些内容的吗?谢谢。你们中的一些人在这方面处于不同的水平,我发现这真的很神奇和鼓舞人心。再次感谢。我花了数周时间尝试或学习的事情,你们似乎在几分钟内就做到了。
我现在快要当老人了,事情越来越难掌握,但我不想放弃,因为我喜欢这个爱好。使用计算机并修复它们是一回事,但进行任何类型的编码都是另一个世界,而且很难。我刚刚意识到我已经使用 Windows 超过 30 年了。天哪!我应该知道如何制作 BAT 文件,但我不知道。我只懂一行命令。
我从 BAT 文件开始,因为它看起来最简单,但我想知道我是否应该专注于 Powershell。侄子的一个孩子告诉我,批处理文件几乎已经过时了,Powershell 更胜一筹。它们看起来很相似,但我还没有跳入游泳池。问题是我应该切换到 Powershell 吗?Bat 文件完成了吗?

相关内容