批处理文件编程_批处理文件编程简介

批处理文件编程

什么是批处理文件? (What is a Batch File?)

A batch file, which is also called batch program or script is a set of commands that simplify our repetitive and tedious tasks. Each line in a batch file is a command. These commands are logically grouped to form a script that is interpreted to carry out the intended task. A batch file is a simple text file saved with an extension “.bat”. These batch commands are case insensitive and easier to write and learn.

批处理文件(也称为批处理程序或脚本)是一组命令,可简化我们的重复繁琐的任务。 批处理文件中的每一行都是一个命令。 这些命令在逻辑上进行分组以形成脚本,该脚本被解释为执行预期的任务。 批处理文件是保存为扩展名“ .bat”的简单文本文件。 这些批处理命令不区分大小写,更易于编写和学习。

Introduction to Batch File Programming
Image Source
图片来源

如何创建批处理文件? (How to Create a Batch File?)

You don’t need a specialized IDE (Integrated Development Environment) to create these batch scripts but a simple text editor is more than enough.  Also it becomes boring to write, save and then run batch files if you write a lot of them. Debugging of these files becomes harder if you write very complex programs without an IDE. So, I suggest you to download and start using a simple IDE developed at code project. It’s very simple to use, you get used within few days with it (Don’t expect me to explain how to use of it). Follow below link to download it.

您不需要专门的IDE(集成开发环境)来创建这些批处理脚本,但是简单的文本编辑器就足够了。 如果编写了大量文件,那么编写,保存然后运行批处理文件也很无聊。 如果您在没有IDE的情况下编写非常复杂的程序,则调试这些文件将变得更加困难。 因此,我建议您下载并开始使用在代码项目中开发的简单IDE。 它非常简单易用,您会在几天之内习惯它(不要指望我解释如何使用它)。 请点击以下链接下载。

http://www.codeproject.com/Tips/888466/Batch-IDE

http://www.codeproject.com/Tips/888466/Batch-IDE

如何执行批处理文件? (How to Execute a Batch File?)

Batch file can be thought of as a Windows version of Unix Shell. Though Batch scripts are not as flexible as shell scripts, they appear to be similar in function. The shell program COMMAND.COM or cmd.exe reads the plain text file (with extension “.bat”) and executes its commands.

批处理文件可以视为Windows版本的Unix Shell。 尽管批处理脚本不像Shell脚本那样灵活,但是它们在功能上似乎相似。 外壳程序COMMAND.COM或cmd.exe读取纯文本文件(扩展名为“ .bat”)并执行其命令。

You can include any number of commands in a single batch file and there is no limit for it. Basically when we run a batch file in the command prompt the command line interface executes the commands in the batch file in a sequential order normally line by line. This means that the CLI (Command Line Interface) acts as an interpreter to the batch scripts. Batch files support various arithmetic, boolean, logical operations just like all other programming languages.

您可以在单个批处理文件中包括任意数量的命令,并且没有限制。 基本上,当我们在命令提示符下运行批处理文件时,命令行界面通常按行顺序按顺序执行批处理文件中的命令。 这意味着CLI(命令行界面)充当批处理脚本的解释器。 像所有其他编程语言一样,批处理文件支持各种算术,布尔,逻辑运算。

You may include any commands in a batch file. Even there are certain conditional commands like ‘goto’, ‘if’ that carries out commands based on the results of the condition, looping statements such as ‘for’ to iterate over a specific set of commands for a desired value of iterator, commands that allow you to control input, output and call to other batch files.

您可以在批处理文件中包含任何命令。 甚至有某些条件命令(例如“ goto”,“ if”)根据条件的结果执行命令,循环语句(例如“ for”)以迭代一组特定的命令以获得所需的迭代器值,这些命令允许您控制输入,输出和对其他批处理文件的调用。

批处理文件的操作模式 (Modes of operation of a Batch File)

There are two different modes that a batch file can operate, classified as

批处理文件可以使用两种不同的模式,分类为

1. Interactive Mode. 2. Batch Mode or Silent Mode

1.交互模式。 2.批处理模式或静音模式

互动模式 (Interactive Mode)

In this mode of operation, based on the user input, the script decides the further commands that are to be executed. The program waits till it receives an input from the user to proceed. For example, consider the ‘del’ command used for deleting files that reside inside ‘crazyprogrammer’ directory. Now I am writing a command to delete all the files inside a folder named ‘crazyprogrammer’.

在这种操作模式下,脚本根据用户输入来确定要执行的其他命令。 该程序一直等到它收到用户的输入后才能继续。 例如,考虑使用“ del”命令删除位于“ crazyprogrammer”目录中的文件。 现在,我正在编写命令以删除名为“ crazyprogrammer”的文件夹中的所有文件。

C:>del crazyprogrammer C:crazyprogrammer*, Are you sure (Y/N)? Y

C:> del crazyprogrammer C:crazyprogrammer *,您确定(是/否)? ÿ

When I executed the above command, it is interacting with me prompting “Are you sure (Y/N)?” confirming the deletion operation, and depending on the input given, it decides what to do. If I hit ‘Y’ then it will delete the directory, else if I hit ‘N’ then the script stops.

当我执行上述命令时,它正在与我交互,提示“您确定(是/否)?” 确认删除操作,并根据给出的输入来决定要执行的操作。 如果我按“ Y”,它将删除目录,否则,如果我按“ N”,则脚本停止。

批处理模式 (Batch Mode)

Batch mode is Quiet Mode of operation of the script. The commands that operate in batch mode will not interact with the user, instead it will take care of every operation by itself. For example, consider same ‘del’ command. There is a switch ‘/Q’ available for the ‘del’ command, which makes it to operate at silent mode.

批处理模式是脚本操作的安静模式。 以批处理方式运行的命令不会与用户交互,而是由用户自己完成所有操作。 例如,考虑相同的“ del”命令。 有一个用于“ del”命令的开关“ / Q”,使其可以在静默模式下运行。

C:>del /Q crazyprogrammer C:>

C:> del / Q crazyprogrammer C:>

In this case the command did not interact with me and deleted the directory directly. This mode is used to automate the task when the user interference is not required.

在这种情况下,该命令未与我互动,因此直接删除了该目录。 当不需要用户干预时,此模式用于自动执行任务。

命令分类 (Classification of Commands)

Commands that can be contained in a batch script are classified as two types

批处理脚本中可以包含的命令分为两种类型

1. Internal commands 2. External commands

1.内部命令 2.外部命令

内部命令 (Internal Commands)

Internal commands are the built-in commands that are designed along with the operating system, like echo, cls, del, dir were few of the useful internal commands.

内部命令是与操作系统一起设计的内置命令,例如echo,cls,del,dir等很少有用的内部命令。

外部命令 (External Commands)

External commands are created when a new application is installed and these commands mostly application specific calls such as javac to compile java files, firefox command to start Firefox.

外部命令是在安装新应用程序时创建的,这些命令主要是特定于应用程序的调用,例如用于编译Java文件的 javac,用于启动Firefox的firefox命令。

If your machine doesn’t have Firefox browser installed then these commands are of no use. So, they are named as external commands.

如果您的计算机未安装Firefox浏览器,则这些命令没有用。 因此,它们被命名为外部命令。

翻译自: https://www.thecrazyprogrammer.com/2015/06/introduction-to-batch-file-programming.html

批处理文件编程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
批处理的语句及用法 例 .bat是dos下的批处理文件 .cmd是nt内核命令行环境的另一种批处理文件 从 更广义的角度来看,unix的shell脚本以及其它操作系统甚至应用程序中由外壳进行解释执行的文本,都具有与批处理文件十分相似的作用,而且同样是由 专用解释器以行为单位解释执行,这种文本形式更通用的称谓是脚本语言。所以从某个程度分析,batch, unix shell, awk, basic, perl 等脚本语言都是一样的,只不过应用的范围和解释的平台各有不同而已。甚至有些应用程序仍然沿用批处理这一称呼,而其内容和扩展名与dos的批处理却又完全 不同。 =================================== 首先批处理文件是一个文本文件,这个文件的每一行都是一条DOS命令(大部分时候就好象我们在DOS提示符下执行的命令行一样),你可以使用DOS下的Edit或者Windows的记事本(notepad)等任何文本文件编辑工具创建和修改批处理文件。 ==== 注 =================== 批 处理文件中完全可以使用非dos命令,甚至可以使用不具有可执行特性的普通数据性文件,这缘于windows系统这个新型解释平台的涉入,使得批处理的应 用越来越"边缘化"。所以我们讨论的批处理应该限定在dos环境或者命令行环境中,否则很多观念和设定都需要做比较大的变动。 ======================== 其 次,批处理文件是一种简单的程序,可以通过条件语句(if)和流程控制语句(goto)来控制命令运行的流程,在批处理中也可以使用循环语句(for)来 循环执行一条命令。当然,批处理文件编程能力与C语言等编程语句比起来是十分有限的,也是十分不规范的。批处理的程序语句就是一条条的DOS命令(包括 内部命令和外部命令),而批处理的能力主要取决于你所使用的命令。 ==== 注 ================== 批 处理文件(batch file)也可以称之为批处理程序(batch program),这一点与编译型语言有所不同,就c语言来说,扩展名为c或者cpp的文件可以称之为c语言文件或者c语言源代码,但只有编译连接后的 exe文件才可以称之为c语言程序。因为批处理文件本身既具有文本的可读性,又具有程序的可执行性,这些称谓的界限是比较模糊的。 ===========================
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值