我怎样才能让一个多功能的批处理程序? [英] how can i make a multi-functional batch program?

查看:172
本文介绍了我怎样才能让一个多功能的批处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理脚本,可以在命令提示符下在同一行显示文本的两种或多种颜色。 (下同)

I have a batch script that can display two or more colors of text on the same line in the command prompt. (below)

@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"
)
echo say the name of the colors, don't read

call :ColorText 0a "blue"
call :ColorText 0C "green"
call :ColorText 0b "red"
echo(
call :ColorText 19 "yellow"
call :ColorText 2F "black"
call :ColorText 4e "white"
pause
goto :eof

:ColorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof

不过该文本必须在脚本前手通过编辑用记事本批处理文件输入。我想能够只是打开命令提示符,然后输入类似:

however that text must be entered in the script before-hand by editing the batch file with notepad. i'd like to be able to just open the command prompt and type something like:

cecho /blue hello world!

cecho blue "hello world!"

或简单的东西,我可以(pferably $ P $作为字符串并不出彩code)提供的颜色和文本(带或不带引号)。

or something simple where i can supply the color (preferably as a string not a color code) and text (with or without quotations).

我不知道这是否是任何使用你的,但它有可能保存脚本的这一部分:

I don't know if this is of any use to you but it is possible to save this part of the script:

echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof

(摘自:ColorText到脚本的结尾)
:\\ WINDOWS \\ SYSTEM32 C和它保存为ColorText.bat。然后在脚本中的另一半,你到处看到:

(from ":ColorText" to the end of the script) and save it as "ColorText.bat" in "C:\windows\system32". Then in the other half of the script, everywhere you see:

call :ColorText

将其更改为:

call ColorText

(省略冒号)
并保存脚本colors.bat在C:\\ WINDOWS \\ SYSTEM32。然后打开命令提示符,输入颜色。这是我希望它的功能;没有额外的命令,安装脚本,文件路径;只是所有的杂乱code的背景怎么回事(淡出人们的视线)简单的一两句话的功能。然而,上述思想仍然不会让我指定在命令提示符下我自己的文本或彩色....任何想法?

(Omit the colons) And save that script as colors.bat in "C:\windows\system32". Then open the command prompt and type "colors". This is how i want it to function; no additional commands, setup scripts, file paths; just a simple one or two word function with all that messy code going on in the background (out of sight). However the above idea still won't let me specify my own text or color from the command prompt.... any ideas?

推荐答案

编辑:取3

创建文件夹 C:\\公用事业。此文件夹添加到PATH环境变量,以便Windows查找有额外的脚本和命令。

Create the folder C:\Utilities. Add this folder to your Path environment variable so Windows looks there for additional scripts and commands.


  1. 开启控制面板,系统,高级系统设置(或高级,在Windows XP中),环境变量。

  2. 在系统变量列表中,选择路径变量。结果
    不要惹这些接下来的步骤了!

  3. preSS编辑。

  4. 将光标移动到行的末尾,并确保没有文本被选中。

  5. 添加文本; C:\\公用事业,包括分号。不要删除任何其他文本。

  6. preSS确定。结果
    松口气一次。

  7. preSS确定为在必要时关闭所有窗口很多次了。

  1. Open Control Panel, System, Advanced System Settings (or "Advanced" in Windows XP), Environment Variables.
  2. In the "System variables" list, select the "Path" variable.
    Do not mess these next steps up!
  3. Press Edit.
  4. Place the cursor at the end of the line and make sure no text is selected.
  5. Add the text ;C:\Utilities, including the semi-colon. Do not remove any other text.
  6. Press OK.
    Breathe easy again.
  7. Press OK as many times as necessary to close all windows.

以脚本后的:ColorText 标签,并将其保存到 C:\\工具\\ cecho.bat 。把一个 @ 前回声关闭来prevent 呼应了从剧本中出现的。

Take the script following the :ColorText label and save it to C:\Utilities\cecho.bat. Put an @ in front of echo off to prevent echo off from appearing during the script.

CEcho.bat

@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"
)
<Nul Set /p ".=%DEL%" > "%~2"
FindStr /v /a:%1 /R "^$" "%~2" Nul
Del "%~2" > Nul 2>&1
EndLocal

现在你可以从任何命令行或脚本使用此命令。用法:

Now you can use this command from any command line or script. Usage:

CEcho color "text"

编辑:在回答您的评论:

您可以通过插入以下行并更换 FINDSTR 行使用的颜色的话:

You can use words for colours by inserting the following lines and replacing the FindStr line:

Set Color=%1
If %1==blue Set Color=9
If %1==red Set Color=C
etc...
FindStr /v /a:%Color% /R "^$" "%~2" Nul

现在你可以输入:

CEcho red "apple"
CEcho blue "water"
CEcho A "grass"
CEcho 6 "dirt"
CEcho 26 "tree"

注意颜色词是区分大小写的。

Note that the color word is case sensitive.

这篇关于我怎样才能让一个多功能的批处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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