批处理文件命令_基本批处理文件命令

批处理文件命令

We are done with all the basic preliminary concepts like variables, operators, conditional, branching and looping commands. Now this tutorial deals with the basic batch file commands that are used generally to make a batch file more complicated.

Sub commands, Switches, Parameters

Before jumping into the commands, I’d like to give a brief overview of the parts of commands. A command in batch program may consist of sub commands and switches. The sub commands are the supportive commands that are accompanied with the main commands. These sub commands generally serve to improve the accuracy of the result that we are looking for. Like the below example,

This command is used to list all the users on a computer. As you can see here, ‘net’ is a main command and the sub command ‘users’ is used to narrow down the results.

Switches allow us to run different variations of commands. You might have seen the switches in the ‘set’ command like p,a. 

We can also pass parameters to the batch file which are nothing but the command line arguments. These parameters can be accessed by a number each in order. The syntax to pass the parameters from the command line is as shown below.

Eg: new HelloWorld!

Here ‘new’ is the batch file name and ‘Helloworld!’ is the argument that I will pass through the command line to the program. The parameters are delimited by spaces and commas, if I had given a space or a comma, Hello and world will be treated as two different parameters. So, here hello and world are treated as single parameter. Try out the below program to access the parameters through command line.

Program #1: To access the parameters given from command line

If you see the program you can easily say that the program prints ‘Welcome to (something in the place of %1%) program’. Here %1% is replaced with the first command line parameter. If you have a second and third one also, you can access it in the program by %2%, %3%, …. so on.

Output C:Usersuser1Desktop>new HelloWorld!! Welcome to HelloWorld!! program Press any key to continue . . .

Color Command

This command is used to set the color for the foreground as well as background of the command prompt window. The color is specified by the hexadecimal color codes. See the below image to list some color codes.

The foreground color is nothing but the text color that is set as default 7 and the background color is set to a default of 0.

Syntax

Program #2: To change the foreground and background of the prompt window

Output

批处理文件命令_基本批处理文件命令

Cls Command

Cls is the command that is used to clear the contents on the screen. There are many situations where you will be interacting with the user, displaying him a list of options and getting inputs and printing status dialogues. In such cases, you may eventually need to clear the contents on the screen. You can use this command to wipe of the screen in the current windows and start with a fresh window.

Syntax

Title Command

When you double click on the batch file and run it, you may have observed that the name on the title bar is path of the cmd.exe program located in the system32 folder of your windows directory. If you want to change the title to your desired title, this command is utilized. Title command sets the title to the current window.

Syntax

Program # 3: To illustrate title and cls commands

The output for this program must be shown in two windows as the screen wiped after executing the first echo command.

批处理文件命令_基本批处理文件命令

Observe that the title of the window has been changed to the text which we have given in the program. Now immediately after I press any key, ‘cls’ command is executed and the text in the prompt is wiped off.

Try it out! Develop at least two versions of programs using the color, title, cls commands along with some conditional commands.

我们已经完成了所有基本的初步概念,例如变量,运算符,条件,分支和循环命令。 现在,本教程将介绍基本的批处理文件命令,这些命令通常使批处理文件更复杂。

子命令,开关,参数

在进入命令之前,我想简要介绍一下命令的各个部分。 批处理程序中的命令可能包含子命令和开关。 子命令是与主命令一起提供的支持命令。 这些子命令通常用于提高我们正在寻找的结果的准确性。 像下面的例子一样,

此命令用于列出计算机上的所有用户。 如您在此处看到的,“ net”是一个主命令,而子命令“ users”用于缩小结果范围。

开关允许我们运行各种不同的命令。 您可能已经在'set'命令中看到了类似p,a的开关。

我们还可以将参数传递给批处理文件,这些参数不过是命令行参数。 这些参数可以依次按数字访问。 从命令行传递参数的语法如下所示。

例如:新的HelloWorld!

这里的“ new”是批处理文件名,“ Helloworld!”。 是我将通过命令行传递给程序的参数。 参数由空格和逗号分隔,如果我给定了空格或逗号,则Hello和world将被视为两个不同的参数。 因此,此处的hello和world被视为单个参数。 试用以下程序,通过命令行访问参数。

程序#1:通过命令行访问给定的参数

如果您看到该程序,则可以轻松地说该程序将打印“欢迎使用(代替%1%)程序”。 这里,%1%被替换为第一个命令行参数。 如果您还有第二个和第三个,则可以在程序中按%2%,%3%等访问它。 以此类推。

输出 C:Usersuser1Desktop>新的HelloWorld !! 欢迎来到HelloWorld! 程序 按任意键继续。

色彩指令

此命令用于设置命令提示符窗口的前景色和背景色。 颜色由十六进制颜色代码指定。 请参见下图列出一些颜色代码。

前景色不过是设置为默认值7的文本颜色,而背景色则设置为默认值0。

句法

程序2:更改提示窗口的前景和背景

输出量

批处理文件命令_基本批处理文件命令

Cls命令

Cls是用于清除屏幕内容的命令。 在许多情况下,您将与用户进行交互,向他显示选项列表并获取输入和打印状态对话框。 在这种情况下,您最终可能需要清除屏幕上的内容。 您可以使用此命令在当前窗口中擦除屏幕并从新窗口开始。

句法

标题命令

当您双击批处理文件并运行它时,您可能已经注意到标题栏上的名称是位于Windows目录的system32文件夹中的cmd.exe程序的路径。 如果要将标题更改为所需的标题,请使用此命令。 标题命令将标题设置为当前窗口。

句法

程序3:说明标题和cls命令

执行第一个echo命令后,该程序的输出必须在两个窗口中显示为屏幕擦拭。

批处理文件命令_基本批处理文件命令

观察到窗口的标题已更改为我们在程序中输入的文本。 现在,在我按任意键后,立即执行“ cls”命令,并且删除提示中的文本。

试试看! 使用color,title,cls命令以及一些条件命令来开发至少两个版本的程序。

翻译自: https://www.thecrazyprogrammer.com/2015/06/basic-batch-file-commands.html

批处理文件命令