Skip to main content

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:
   EQU = Equals
   NEQ = Not Equal
   GTR = Greater
   LSS = Less
   GEQ = Greater or Equal
   LEQ = Less or Equal
GTR, GEQ, LSS, and LEQ can only be used when comparing two numeric values, no letters.
The other command we will learn about today is the 'goto' command.  The 'goto' command is very simple and only has one usage.  It directs the processor to a label in the code.  Here is a demonstration:
     echo Hi
     goto abc
     echo Hello
     :abc
     echo Bye
     pause
You would think that the code would first say 'Hi', then 'Hello', then 'Bye', but it actually just says 'Hi', then 'Bye'.  This happens because there is a label called ':abc'.  Labels are represented by a colon, then a string (:start or :return).  The command 'goto abc' will send the code processing straight over to the label ':abc', skipping any lines in between.  Try out using the 'goto' command for yourself, here is a little demo code you can look at:
     echo off
     cls
     set value=0
     :loop
     set /a value=%value%+1
     if "%value%" GTR "100" goto exit
     echo %value%
     goto loop
     :exit
     echo Finished
     pause
This program will count from 1-100 and echo it all onto the screen.  Here is another cool program you can use:
     echo off
     cls
     :start
     set /p pass=Password:
     if "%pass%" EQU "secretcode" goto correct
     if "%pass%" NEQ "secretcode" exit
     :correct
     cls
     echo Password correct
     echo Opening secret folder...
     pause
The exit command, as used in this program, closes the batch script.  There are more advanced uses that you will learn about in later lessons.

Comments

Popular posts from this blog

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...