0% found this document useful (0 votes)
155 views

Batch Scripting

This document provides an overview of common commands and programming structures used in batch scripting. It covers: 1) Variables, arithmetic, echoing, calling other files/labels, and comments. 2) If/then/else conditional statements for comparing variables. 3) Parameters that can be passed into scripts, and looping with FOR. 4) Getting user input with CHOICE and SET /P, and calling other scripts/functions. 5) Constructing functions using labels, and using SETLOCAL/ENDLOCAL to create local scopes.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views

Batch Scripting

This document provides an overview of common commands and programming structures used in batch scripting. It covers: 1) Variables, arithmetic, echoing, calling other files/labels, and comments. 2) If/then/else conditional statements for comparing variables. 3) Parameters that can be passed into scripts, and looping with FOR. 4) Getting user input with CHOICE and SET /P, and calling other scripts/functions. 5) Constructing functions using labels, and using SETLOCAL/ENDLOCAL to create local scopes.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Commands

Variables: SET variable = value, %variable%: value of variable


Arithmetic: SET /A variable = expression (eg. set /a "_num=_num+5")
Echoing: ECHO %variable% / string
Calling: CALL filename / ":label

Comments
Rem Remarsks
Example:
@echo off
Rem This program just displays Hello World
set message=Hello World
echo %message%

Alternatively
:: Remarks
Example:
@echo off
:: This program just displays Hello World
set message = Hello World
echo %message%

If- Then - Else


if test(command) else (command)

Example:
if exists filename.txt(
echo deleting filename.txt
del filename.txt
)else(
echo This file was not found.
)

Comparison Operators
EQU:Equal
NEQ:Not Equal
LSS: Less than <
LEQ: Less than or Equal <=
GTR: Greater than >
GEQ: Greater than or equal >=
NOT: (executes the command if false)

Parameters
Values than can be passed into our script when we run it
These can be accessed similar to variables however they have no trailing% symbol
Parameters can be accessed from %1-%9 program param1 parama2 param3 etc

Looping
FOR /L %%G IN (start, step, end) DO command
eg FOR /L %%G IN (1,1,10) DO %%G

Getting User Input


We can get input from our user in two ways
1) CHOICE - this commandonly allows us to get 1 character from the user. Usually Y
or N
2) SET /p answer=Prompt:
Prompt is the message to show the user. And answer is the variable to store into
CALL command
We can use the CALL command to run another script, commmand or function
The key feature of the CALL command ia that once the called script, command or
function finishes
it will return to the place CALL was used and continue running the script
This is most useful for functions
CALL :MyFunction param1 param2

Functions
Now that we know how to call functions, let's look at how to construct them
Functions are slightly faked in batch scripting, if you have ever worked in other
programming
languages, you will be aware that functions can return results, have their own
local variables
and can have parameters passed in
:FunctionsUseLabels

Setlocal & endlocal


We can use ythe commands SETLOCAL and ENDLOCAL to create a temporary local scope
where we can have
new variables
Example:
Set num1=5<-Global num1
:MyFunction
SETLOCAL
set num1=10<-Local num1(different variable)
ENDLOCAL
However, this causes issues for returning a result

You might also like