批处理⽂件变量_批处理⽂件变量
批处理⽂件 变量批处理文件怎么做
Any programming or scripting languages require variable to store the data and access them whenever we need the value. Using of variables becomes inevitable in mathematical computations. The variable contains some data whose value may change during the course of program computation.If you are familiar with traditional languages like C, C++ or java, you may assume that the variable must be created generally using the data type that defines the value it holds. But in batch files we don’t have any such data type like int, float blah blah. The command interpreter is intelligent enough to understand certain values given to variables.
Let us first understand how to create variables in batch file programming and go deeper into this concept.
Batch File Variables
SET [[/a [expression]]
[/p [variable=]] string]
1SET [[/a [expression]] [/p [variable=]] string]
So, the above syntax is used to evaluate arithmetic expressions and also create variables in batch programs. Basically we create variables to specify that the computer needs to remember certain values that are given a name and may be changed at any time until the end of the program.
Does this syntax seems weird?
Yes, I felt the same at first. If you are a novice programmer, you will also feel the same. Let us break it down and make it simple enough to understand. For few minutes, forget the syntax and just remember that we use SET command to evaluate arithmetic expressions and create variables.
Batch File SET Variable
Just we will start with the first word in the syntax that is SET. This word is enough for us to create a variable. It is a built in command in MS-DOS. Now the question is how to create a variable using this command. Have a glance at the example.
Creating Variables
This line creates a variable named myVar with its value as 1. The variable name is myVar and its value is an integer (Literally to say the interpreter doesn’t know that myVar holds an integer. You will know why as we move on further.)
I can use either a number or a string in the place of value like as shown below.
SET Num=1
SET Str=hello
1 2SET Num=1 SET Str=hello
Like all other languages, we also have certain restrictions on the variable name that the names must not be built in commands etc. You just need to follow them while creating a variable.
How to Print Variables in Batch File?
To print a message on the command prompt we already know that we use ECHO command.
Will “Echo myVar” prints the values of the variable on the console?
No, the interpreter doesn’t know that by writing myVar, you are referring to a variable. But, it assumes that myVar is a string that needs to be echoed back on the console. So, to tell it is a variable, we enclose it in ‘%’ signs.
Program #1
The above program prints the value of myVar on the command prompt. Give it a try and observe it. If we want to refer a variable that already exists before, we enclose the variable name in ‘%’ signs.
Now let us write a program to carry out addition of two variables.
So, you will be away to think that the value of the variable ‘res’ is 3. But that is not the case here. The interpreter assumes res as a string that is the concatenation of variables a and b. To specify that the computation is arithmetic but not the string concatenation, we use the flag ‘/a’. Using it, let us rewrite
the program.
Now the resultant value in ‘res’ will be 3.
How to save the user inputs in a variable?
How to save the user inputs in a variable? Immediately after the above program, you might be interested to get the variable values from the user rather than making a static calculation.
For this purpose, we use ‘/p’ flag. ‘/p’ flag is used to set the value of variable taken from the line of input. We will design a simple calculator that adds two numbers given by the user to make it clearer.
Batch Program for Addition of Two Numbers
First input given from the user will be stored in ‘num1’ and the next in ‘num2’. Then we carry out the addition and display the result. The output window is shown below.
More fun to experiment yourself 1. Write a batch file to add, multiply, divide and subtract two numbers. Observe the More fun to experiment yourself
results.
2. Use ‘goto’ statement in the above program to repeat the addition of two numbers any number of times. (If you don’t get this, nothing is there to worry. In the next tutorial, I’ll explain how about looping and conditional statements).
This was all about batch file variables. If you have any doubts then you can ask it by commenting below.
任何编程或脚本语⾔都需要变量来存储数据,并在需要此值时访问它们。 在数学计算中不可避免地要使⽤变量。 该变量包含⼀些数据,其值在程序计算过程中可能会发⽣变化。 如果您熟悉C,C ++或Java之类的传统语⾔,则可以假定必须通常使⽤定义变量值的数据类型来创建变量。 但是在批处理⽂件中,我们没有int,float blah blah这样的数据类型。 命令解释器⾜够智能,可以理解赋予变量的某些值。
让我们⾸先了解如何在批处理⽂件编程中创建变量,并更深⼊地了解这⼀概念。
批处理⽂件变量
SET [[/a [expression]]
[/p [variable=]] string]
1SET [ [ / a [ expression ] ] [ / p [ variable = ] ] string ]
因此,以上语法⽤于评估算术表达式并在批处理程序中创建变量。 基本上,我们创建变量来指定计算机需要记住给定名称的某些值,并且可以在程序结束之前随时对其进⾏更改。
这种语法看起来很奇怪吗?
是的,起初我有同样的感觉。 如果您是新⼿程序员,您也会有同样的感觉。 让我们对其进⾏分解,使其简单易懂。 ⼏分钟,忘记了语法,只记得我们使⽤SET命令评估算术表达式并创建变量。
批处理⽂件SET变量
只是我们将从SET语法中的第⼀个单词开始。 这个词⾜以让我们创建⼀个变量。 它是MS-DOS中的内
置命令。 现在的问题是如何使⽤此命令创建变量。 看看这个例⼦。
创建变量
这⾏代码创建⼀个名为myVar的变量,其值为1。变量名称为myVar,其值为整数(直译为解释器不知道myVar包含整数。您将继续研究为什么会这样。) )
我可以使⽤数字或字符串代替值,如下所⽰。
SET Num=1
SET Str=hello
1 2SET Num = 1 SET Str = hello
像所有其他语⾔⼀样,我们对变量名也有⼀定的限制,即不能在命令等中内置这些名称。只需要在创建变量时遵循它们即可。
如何在批处理⽂件中打印变量?
要在命令提⽰符下打印消息,我们已经知道我们使⽤了ECHO命令。
“ Echo myVar”会在控制台上打印变量的值吗?
不,解释器不知道通过编写myVar,您是在引⽤变量。 但是,它假定myVar是需要在控制台上回显的字符串。 因此,要说它是⼀个变量,我们将其括在“%”符号中。
程序#1
上⾯的程序在命令提⽰符下打印myVar的值。 试试看,观察⼀下。 如果要引⽤以前已经存在的变量,则将变量名称括在'%'符号中。
现在让我们编写⼀个程序来执⾏两个变量的加法运算。
因此,您可能会认为变量'res'的值为3。但是事实并⾮如此。 解释器将res假定为字符串,它是变量a和b的串联。 为了指定计算是算术运算,⽽不是字符串连接运算,我们使⽤标志“ / a”。 使⽤它,让我们重写程序。
现在,“ res”中的结果值为3。
如何将⽤户输⼊保存在变量中?
如何将⽤户输⼊保存在变量中? 在执⾏完上述程序之后,您可能有兴趣从⽤户那⾥获取变量值,⽽不是进⾏静态计算。
为此,我们使⽤“ / p”标志。 '/ p'标志⽤于设置从输⼊⾏获取的变量的值。 我们将设计⼀个简单的计算器,该计算器将⽤户给定的两个数字相加,以使其更清晰。
批处理程序的两个数字加法
⽤户输⼊的第⼀个输⼊将存储在“ num1”中,下⼀个存储在“ num2”中。 然后我们进⾏加法并显⽰结果。 输出窗⼝如下所⽰。
实验⾃⼰的乐趣更⼤ 。1.编写⼀个批处理⽂件,以对两个数字进⾏加,乘,除和减法。 观察结果。
实验⾃⼰的乐趣更⼤
2.在上⾯的程序中使⽤“ goto”语句将两个数字相加任意次数。 (如果您不明⽩这⼀点,则⽆需担⼼。在下⼀个教程中,我将说明循环和条件语句的⽅式)。
这都是关于批处理⽂件变量的。 如果您有任何疑问,可以通过下⾯的评论提出。
批处理⽂件 变量

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。