Tuesday, 24 February 2015

Command Line Scripting Tutorial Series Part 4 # If - Else Conditions.




Command Line Scripting Tutorial Series
(Part'4') # If - Else Conditions

Hi Everybody, It's The Fourth Tutorial Of Command Line Scripting Tutorial Script, And In This Tutorial We Are Gonna Learn About (If - Else) Conditions, So Let's Have A Look On What Is A (If - Else) Condition And (If - Else) Condition In Command Prompt.


If - Else Conditions: The 'If' Command Is Used For Testing A Condition That If The Condition Is True Then Do Some Job After It, And 'Else' Command Is Used After 'If', If The Condition Of 'If' Is False Than The Codes After The 'Else' Are Gonna Execute, Otherwise They Are Ignored By The Program, Logical Operators  Are Used After 'If' To Make A Condition Like (If A = B Xyz Else Abc) The "Equal" Sign Define That 'A = B' So If 'A = B' Is True Then The Code After The 'If' Is Gonna Execute Otherwise The Code After 'Else' Is Gonna Execute.

If - Else Conditions In Command Prompt: If - Else Conditions Are Same In Every Language(s), It Does The Same Job In Every Language(s), The Only Difference Is The Syntax And The Method To Use It, In Command Prompt The Syntax Of 'If - Else' Condition Is :

If 'Condition' 'Code' Else 'Code'

OR

If 'Condition' (
                       'Code'
)

Else (
         'Code'
)

Now We Are Going To Make A Command Line Script That Use If - Else Conditions.

Labels: Before Making A Command Line Script, Let Me Introduce You To Labels And Goto, Labels Are Used For Making Checkpoints In Command Line Scripting They Are Declared By Using ':' And Used By It's Name, While Goto Is Used For Getting The Program To A Checkpoint For Example:

:Start
Cls
Echo Using Label
Pause
Goto Start



Making A Command Line Script That Use If - Else Conditions:
Make A New Cmd/Batch File, Edit It And Type The Following Code.

:Start
@echo off
cls
set /p Password="Enter Password: "
If %
Password%==123 (
Goto Continue
)
Else (
Goto Denied
)
:Denied
cls
Echo Sorry, Access Is Denied.
Pause
Goto Start
:Continue
cls
Echo Access Is Granted.
Pause
Goto Start

The Double '==' 'Equals' Signs Define That %Password%==123, You Can Use 'NOT' In The Before '==' For Checking That If The Condition Is False For E.g:

If NOT %Password%==123 Goto Denied Else Goto Continue

OK, That's Enough For Today See You Guys Tomorrow With The Next Tutorial Of Command Line Scripting Tutorial Series.

Thanks For Visiting.
Hope It Helps, If It Then Please Share.
Stay Connected For More Updates.
If You Got Any Question Or Problem Or Want Me To Make Tutorial On Something Then Please Just Let Me Know.


No comments:

Post a Comment