如何使批处理文件的某一行与其他行的颜色不同? [英] How do I make one particular line of a batch file a different color then the others?

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

问题描述

我正在开发一个为 D&D 游戏生成当天天气的程序.我希望程序在产生风暴时以红色文本显示警告,以便 DM 知道这种天气并不典型.为了减少击键次数,它必须在与详细描述天气本身的文本相同的屏幕上执行此操作.

I'm working on a program which generates the day's weather for D&D games. I want the program to display a warning in red text when a storm is generated so the DM is aware that this weather is not typical. To reduce the number of keystrokes, it must do this on the same screen as the text detailing the weather itself.

目前我有一个风暴条目:

Currently for a storm's entry I have this:

:des4a
cls
type c:AutoDMWeatherGendataforcast1.txt
color 0c
echo     There is an Ashstorm!
color 0a
echo.
echo     It is %temp% degrees and the sky is invisible threw the thick 
echo     billowing clouds of ash.
echo.
echo         # Survival checks to light a fire are at +15. 
echo         # Small unprotected flames will be snuffed out.
echo         # Non-firearm ranged attacks are at a -8 to hit.
echo         # Preception checks take a -10 for every 10 feet of distance.
echo         # Survival checks to get along in the wild are at +15.
echo         # Stealth checks are at a +5.
echo.
echo     SPECIAL!
echo.
set /a die=6
set /a inches=%random%%%die+1
echo         # The ashstorm will deposit %inches% inches of ash durring its
echo           durration.
echo         # Tracking a target after an ashstorm is at a +15.
type c:AutoDMWeatherGendataforcast2.txt
echo.
echo.
pause
goto menu

类型命令调用包含每个条目的页眉和页脚的文本文档,以帮助程序看起来专业并提供边框以帮助自动换行.你无法删除.请不要建议使我无法使用当前存在的类型命令的内容.请理解,这条红色文本行将被添加到程序的许多不同部分,每次生成器中的每个生物群落都有风暴.我希望它不要超过 2 或 3 行代码(但如果只有一种方法可以做得很好......)

The type commands are calling text documents which contain a header and footer for each entry to help the program look professional and provide a border to assist with word wrap. Thy cannot be removed. DO not suggest something which would make me unable to use the type commands as they currently exist please. Please understand that this red text line will be added to a lot of different parts of the program, each time there is a storm for each and every biome in the generator. I would prefer it to not be more then just 2 or 3 lines of code (But if there is only one way to do it well...)

我可以让一条线为红色,其余为绿色吗?或者我应该让程序发出哔哔声来引起人们注意这一事实?你有更好更简单的想法吗?我该怎么做?

Can I have the one line in red and the rest in green? Or should I have the program beep to call attention to the fact? Do you have a better and simpler idea? How do I do this?

推荐答案

昨天我遇到了同样的问题,我做了一些研究,这个对我有用.这与另一个不是相同的代码.

I was having the same problem yesterday, I did some research and this worked for me. This is NOT the same code as the other one.

@Echo Off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do     rem"') do (
  set "DEL=%%a"
)
call :colorEcho 0a "This is colored green with a black background!"
echo.
call :colorEcho a0 "This is colored black with a green background!"
echo.
pause
exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i

它这样做:

你需要做的就是把从 SETLOCAL EnableDel... 到 ) 的部分放到你的代码的开头(如果你的代码以 @ 开头)echo off 然后把它放在那个下面),并将 :colorEcho 的部分放到 del %~2 到脚本的底部(没有任何东西!)

All you need to do to fit this is put the part from SETLOCAL EnableDel... to ) to the beginning of your code (If your code starts with @echo off then put it under that) and also put the part from :colorEcho to del %~2 at the exact bottom of your script (NOTHING UNDERNEATH!)

现在你注意到了

call :colorEcho 0a "This is colored green with a black background!"
echo.
call :colorEcho a0 "This is colored black with a green background!"
echo.
pause
exit

逐行解释:第一行 (call :colorEcho 0a "This is colour green with a black background!"): 这是一个 colored 回声,令人惊讶的是 This is coloured green黑色背景! in 0a(黑色背景,绿色文字)第二行 (echo.) 打印换行符,因为我们的彩色回显不打印换行符.

Explained line by line: First line (call :colorEcho 0a "This is colored green with a black background!"): This is a colored echo, it suprisingly says This is colored green with a black background! in 0a (Black Bg, Green Text) Second line (echo.) prints a newline, since our colored echo doesn't print one.

所以

假设您想说Hello World",Hello 为黄色,World 为绿色.例子!

Let's say you wanted to say "Hello World" in Hello being yellow and World being green. Example!

@Echo Off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do     rem"') do (
  set "DEL=%%a"
)
call :colorEcho 0e "Hello "
call :colorEcho 0a " World"
pause
exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i

它的作用是这样的:我希望这是可以理解的!

This is what it does: I hope this is understandable!

确保程序在到达 :colorEcho

这篇关于如何使批处理文件的某一行与其他行的颜色不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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