Skip to main content

Batch Lesson : Beginner #2

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)

Comments

Popular posts from this blog

Batch Lesson : Beginner #3

This lesson is where the use of variables starts to make sense.  In this lesson, you will be taught about two of the most useful commands in batch, the 'if ' and 'goto' command.  The 'if ' command has many different syntaxes and uses so I will only be teaching a few in this lesson.  The 'if ' command is used to compare two strings and carry out an action based on the result.  This is how the subsection of the command we are learning today is used:      if "string1" comparison "string2" command 'string1' and 'string2' can be replaced by any string.  'comparison' is replaced by one of the six comparison operators you will learn about.  Command can be replaced by any command you want to add at the end.  Here is a sample code:      set /p password=Enter the password:      if "%password%" EQU "12345" echo Correct password The six comparison operators are:  ...

Batch Lesson : Beginner #1

Batch programming is the basic command line scripting language used in windows.  Is uses simple MS-DOS commands to execute a variety of tasks and functions ranging from simple sorting of strings to modifying system settings and files.  It is a necessary language to know in almost any computer related job. In this lesson, you will be learning about a few basic commands used to display text in the command window. As batch is a command line programming language, all of the display is shown in a Command Prompt window that looks like this: To create a batch file, you must write all of your code into notepad, then save the file somewhere to your computer, as yourfile.bat . The 'echo' command is used to display text in the command window.  The command is used like this:      echo your text here If you opened your program that only contained the echo command, it would display the text but then instantly close.  To pause t...