批处理文件 变量_批处理文件变量

批处理文件 变量

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

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.

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? 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 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命令评估算术表达式并创建变量。

批处理文件SET变量

只是我们将从SET语法中的第一个单词开始。 这个词足以让我们创建一个变量。 它是MS-DOS中的内置命令。 现在的问题是如何使用此命令创建变量。 看看这个例子。

创建变量

这行代码创建一个名为myVar的变量,其值为1。变量名称为myVar,其值为整数(直译为解释器不知道myVar包含整数。您将继续研究为什么会这样。) )

我可以使用数字或字符串代替值,如下所示。

像所有其他语言一样,我们对变量名也有一定的限制,即不能在命令等中内置这些名称。只需要在创建变量时遵循它们即可。

如何在批处理文件中打印变量?

要在命令提示符下打印消息,我们已经知道我们使用了ECHO命令。

“ Echo myVar”会在控制台上打印变量的值吗?

不,解释器不知道通过编写myVar,您是在引用变量。 但是,它假定myVar是需要在控制台上回显的字符串。 因此,要说它是一个变量,我们将其括在“%”符号中。

程序#1

上面的程序在命令提示符下打印myVar的值。 试试看,观察一下。 如果要引用以前已经存在的变量,则将变量名称括在'%'符号中。

现在让我们编写一个程序来执行两个变量的加法运算。

因此,您可能会认为变量'res'的值为3。但是事实并非如此。 解释器将res假定为字符串,它是变量a和b的串联。 为了指定计算是算术运算,而不是字符串连接运算,我们使用标志“ / a”。 使用它,让我们重写程序。

现在,“ res”中的结果值为3。

如何将用户输入保存在变量中? 在执行完上述程序之后,您可能有兴趣从用户那里获取变量值,而不是进行静态计算。

为此,我们使用“ / p”标志。 '/ p'标志用于设置从输入行获取的变量的值。 我们将设计一个简单的计算器,该计算器将用户给定的两个数字相加,以使其更清晰。

批处理程序的两个数字加法

用户输入的第一个输入将存储在“ num1”中,下一个存储在“ num2”中。 然后我们进行加法并显示结果。 输出窗口如下所示。

实验自己的乐趣更大 。1.编写一个批处理文件,以对两个数字进行加,乘,除和减法。 观察结果。

2.在上面的程序中使用“ goto”语句将两个数字相加任意次数。 (如果您不明白这一点,则无需担心。在下一个教程中,我将说明循环和条件语句的方式)。

这都是关于批处理文件变量的。 如果您有任何疑问,可以通过下面的评论提出。

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

批处理文件 变量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值