In this lesson, you will learn about variables. How to create them, modify them, and use them in your code. Variables are used to store strings of text. In batch, they are represented as the variable name, surrounded by percent symbols: %myVar%. To set a variable, use the set command like this:
set abc=1234
echo %abc%
This will create a variable called 'abc', that expands to the value '1234'. Then, the program will display the text '1234' through the echo command.
set abc=12
echo %abc%
set abc=34
echo %abc%
This program will display the text '12', then on a separate line, '34'.
You can also set the value of a variable to nul(nothing) like this:
set var=
This is different that setting the variable to 0 because this gives an output of nothing, not 0.
Parameters are parts of commands that describe the exact function that will take place. They are usually written as the command, then /x, where x is any letter or word. There are two parameters of the set command that you will learn in this lesson.
To create a user defined variable, use the set command, but with the parameter /p. This is done like this:
set /p var=
This will prompt the user to enter a value that will define the variable 'var'. The user types in whatever they would like to set the value as then presses enter. Try running this program:
echo off
cls
set /p test=
echo %test%
pause
To tell the user what to write in the prompt, do this:
set /p abc=Enter your name:
This will tell the user to enter their name, when they are prompted to type something in. It should look like this:
The user can then enter in their name and the program will set the value of variable 'abc' to their name.
Another parameter that can be used with the set command is /a. This is used to do simple arithmetic functions, such as adding or subtracting. The command cannot process decimals or return outputs that contain decimals but there are special ways to work around that which I will show in later lessons. The command is used like this:
set /a var=1+2
set /a abc=9/3
set /a test=%var%-%abc%
This code will first set %var% to '3', then set %abc% to '3', then set %test% to '3-3' which equals '0'.
You can combine all of this into something like a form that requests your name and age etc.
set year=2017
set /p name=Name:
set /p age=Age:
set /a year_of_birth=%year% - %age%
echo Your name is - %name%
echo Your age is - %age%
echo You were born in - %year_of_birth%
There are also environment variables, which are variables that are stored on the system that remain constant(sort of) throughout any code you use. Try typing in 'echo %username%' and the code will return the value with you username. You can also use %date% or %time% to get the exact date and time. Unlink some other environment variables, these ones are constantly changing and are called dynamic. One of the most useful one of thee dynamic variables is the %random% variable. This will output a random value that can be up to five digits(somewhere below 35000). While this isn't useful yet, it will become very useful in the future, when you know more about batch.
Try using some of the environment variables in your code:
echo %date% - %time%
echo %random%
echo %random%
echo %random%%username%%random%
echo %cd%
Here is a list of the most useful environment variables that you will likely use later in your code.
random = random number
cd = current directory
userprofile = C:\Users\username\
temp = C:\Users\tlc3\AppData\Local\Temp
tmp = C:\Users\tlc3\AppData\Local\Temp
errorlevel = the errorlevel (discuss later)
set abc=1234
echo %abc%
This will create a variable called 'abc', that expands to the value '1234'. Then, the program will display the text '1234' through the echo command.
set abc=12
echo %abc%
set abc=34
echo %abc%
This program will display the text '12', then on a separate line, '34'.
You can also set the value of a variable to nul(nothing) like this:
set var=
This is different that setting the variable to 0 because this gives an output of nothing, not 0.
Parameters are parts of commands that describe the exact function that will take place. They are usually written as the command, then /x, where x is any letter or word. There are two parameters of the set command that you will learn in this lesson.
To create a user defined variable, use the set command, but with the parameter /p. This is done like this:
set /p var=
This will prompt the user to enter a value that will define the variable 'var'. The user types in whatever they would like to set the value as then presses enter. Try running this program:
echo off
cls
set /p test=
echo %test%
pause
To tell the user what to write in the prompt, do this:
set /p abc=Enter your name:
This will tell the user to enter their name, when they are prompted to type something in. It should look like this:
The user can then enter in their name and the program will set the value of variable 'abc' to their name.
Another parameter that can be used with the set command is /a. This is used to do simple arithmetic functions, such as adding or subtracting. The command cannot process decimals or return outputs that contain decimals but there are special ways to work around that which I will show in later lessons. The command is used like this:
set /a var=1+2
set /a abc=9/3
set /a test=%var%-%abc%
This code will first set %var% to '3', then set %abc% to '3', then set %test% to '3-3' which equals '0'.
You can combine all of this into something like a form that requests your name and age etc.
set year=2017
set /p name=Name:
set /p age=Age:
set /a year_of_birth=%year% - %age%
echo Your name is - %name%
echo Your age is - %age%
echo You were born in - %year_of_birth%
There are also environment variables, which are variables that are stored on the system that remain constant(sort of) throughout any code you use. Try typing in 'echo %username%' and the code will return the value with you username. You can also use %date% or %time% to get the exact date and time. Unlink some other environment variables, these ones are constantly changing and are called dynamic. One of the most useful one of thee dynamic variables is the %random% variable. This will output a random value that can be up to five digits(somewhere below 35000). While this isn't useful yet, it will become very useful in the future, when you know more about batch.
Try using some of the environment variables in your code:
echo %date% - %time%
echo %random%
echo %random%
echo %random%%username%%random%
echo %cd%
Here is a list of the most useful environment variables that you will likely use later in your code.
random = random number
cd = current directory
userprofile = C:\Users\username\
temp = C:\Users\tlc3\AppData\Local\Temp
tmp = C:\Users\tlc3\AppData\Local\Temp
errorlevel = the errorlevel (discuss later)

Comments
Post a Comment