批处理⽂件编程_批处理⽂件编程简介
批处理⽂件编程
什么是批处理⽂件? (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”的简单⽂本⽂件。 这些批处理命令不区分⼤⼩写,更易于编写和学习。
如何创建批处理⽂件? (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。 它⾮常简单易⽤,您会在⼏天之内习惯它(不要指望我解释如何使⽤它)。 请点击以下链接下载。
如何执⾏批处理⽂件? (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 reads the plain text file (with extension “.bat”) and executes its commands.
批处理⽂件可以视为Windows版本的Unix Shell。 尽管批处理脚本不像Shell脚本那样灵活,但是它们在功能上似乎相似。 外壳程序COMMAND.COM或读取纯⽂本⽂件(扩展名为“ .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 (Com
mand 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 , firefox command to start Firefox.
外部命令是在安装新应⽤程序时创建的,这些命令主要是特定于应⽤程序的调⽤,例如⽤于 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浏览器,则这些命令没有⽤。 因此,它们被命名为外部命令。
批处理⽂件编程
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论