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.
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
Post a Comment