Categories
Scripts Windows Batch

Functions

In general, Function is a block of code which can be called multiple times from any point in program. Function may receive input and it may return output after processing.

  1. Function with No input and no output returned
::To demonstrate the functions

@echo off


::call to function
call :helloworld
::return after function call
exit /B %ERRORLEVEL% 



::function definition starts
:helloworld
echo Hello World
pause 
exit /B 0
::function definition ends




Output

2. Function that receives input but returns no output

::To demonstrate the functions

@echo off


::call to function with 2 parameters
call :WishMessage Peter Indiana
exit /B %ERRORLEVEL%


::Function definition starts
:WishMessage
::%~n is used to refer input parameters
echo Hello Good Morning %~1 %~2 
pause 
exit /B 0
::Function definition ends


output

3. Function that receives input and returns some output

::To demonstrate the functions

@echo off



call :WishMessage Peter Indiana value1
echo today is %value1%
exit /B %ERRORLEVEL%



:WishMessage
echo Hello Good Morning %~1 %~2
set value1=Monday 
pause 
exit /B 0

Output

Categories
Scripts Windows Batch

Return Code

10ReturnCodes.bat

::This program is used to demonstrate the return statement
::Every comamnd or batch file returns an integer to indicate result of program
:: if returned code is XERO, in general it is agreed as succesful execution
:: in case of error it supposed to return NON-ZERO integer, to indicate error specific

@echo off

:: ERRORLEVEL is in-built variable which is popped up with return code from last executed command or program
ping foo.com
if %ERRORLEVEL%==0 (echo foo ping was successful) else (echo foo ping was having issue)
 
ping google.com
if %ERRORLEVEL%==0 (echo google ping was successful) else (echo google ping was having issue)


echo "program will exit with 0 return code, after you press enter"
pause

::at time of return, program should return with error code
exit 0

Output

Categories
Scripts Windows Batch

For Loop

09ForLoop.bat

::This program is used to demonstrate the for loop structure

@echo off

::For loop to iterate over list of values
echo print the list by for loop
for %%v in (11 22 33 44 55) do echo "v :: %%v"

::For loop to iterate through content of directory
echo print list of files in current directory
FOR %%v IN (.\*) DO ECHO %%v


echo To iterate over range from 10 to 100 with increment of 20 each iteration
::For loop to iterate over range
:: /L signifies to iterate over range
for /L %%v IN (10, 20, 100) Do echo "v :: %%v"



output

Categories
Scripts Windows Batch

If Else structures

08IfElse.bat

::This program is used to demonstrate the if else decision making structures


@echo off
set /A var_1=10
set /A var_2=20

::apply if else to check if two variables are same or different
if %var_1%==%var_2% (echo var_1 and var_2 are equal) else (echo var1 and var2 are not equal )

::apply if to check if variable is already defined
if DEFINED var_1 (echo var_1 is defined)
if NOT DEFINED var_2 (echo var_2 is undefined, hence defining again && set /A var2=21) else (echo var_2 is defined)

::apply if to check if file specified exists or not
if exist 05Data.txt (echo 05Data.txt file exists) else (echo 05Data.txt file does not exist)

pause

Output

Categories
Scripts Windows Batch

Assignment Operator

07Operators_Assignment.bat

::this program will demonstrate assignment operators, following operators are supported.
::+= add & assign
::-= substract and assign
::*= multiply and assign
::/= divide and assign
::%= modulo divide and assign 

@echo off

set /A var_1=3

set /A var_1+=2
echo "add and assign var_1 :: %var_1%"

set /A var_1-=3
echo "substract and assign var_1 :: %var_1%"

set /A var_1*=5
echo "multiply and assign var_1 :: %var_1%"

set /A var_1/=6
echo "divide and assign var_1 ::%var_1%"

set /A var_1%=2
echo "module divide and assign var_1 ::%var_1%"

Output

Categories
Scripts Windows Batch

Bitwise Operator

Batch script supports logical operators for bitwise operations only. Unlike other rich programming languages, batch program does not support conditional logical operators.

06Operators_Bitwise.bat

::this program will demonstrate bitwise operators, following operators are supported
::& Bitwise AND
::| Bitwise OR
::^ Bitwise Exclusive OR 

@echo off

set /A var_1=3
set /A var_2=5

set /A bitwiseAND="%var_1%&%var_2%"
set /A bitwiseOR="%var_1%|%var_2%"
set /A bitwiseEXOR="%var_1%^%var_2%"


echo "bitwiseAND :: %bitwiseAND%"
echo "bitwiseOR :: %bitwiseOR%"
echo "bitwiseEXOR :: %bitwiseEXOR%"

Output

Categories
Scripts Windows Batch

Redirection Operators

05Operators_Redirectional.bat

::this program will demonstrate redirectional operators, following are provided operators
::>  To redirect command output to another file or stream. If file is already present it is overwritten
::> To redirect command output to another file in append mode

@echo off

::To print  list of system users to file named 05Data.txt.
net users > 05Data.txt

::To append the ping output to same file 05Data.txt
ping yahoo.co.in >> 05Data.txt


::To Print the content of file 
echo Content of file 05Data.txt
type 05Data.txt



::To provide input to sort command. Input is list of numbers which is passed from 05Num.txt
echo Content of file 05Num.txt
type 05Num.txt
echo Applying sort command on File data 05Num.txt
sort <05Num.txt





Output

05Data.txt

User accounts for \\LAPTOP-A4OSG6CL

-------------------------------------------------------------------------------
Administrator            DefaultAccount           defaultuser100001        
Guest                    Owner                    WDAGUtilityAccount       
The command completed successfully.


Pinging yahoo.co.in [74.6.136.150] with 32 bytes of data:
Reply from 74.6.136.150: bytes=32 time=292ms TTL=51
Reply from 74.6.136.150: bytes=32 time=212ms TTL=51
Reply from 74.6.136.150: bytes=32 time=681ms TTL=51
Reply from 74.6.136.150: bytes=32 time=400ms TTL=51

Ping statistics for 74.6.136.150:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 212ms, Maximum = 681ms, Average = 396ms

05Num.txt

5
4
3
2
1

Categories
Scripts Windows Batch

Relational Operators

04Operators_Relational.bat

::this program will demonstrate relational operators. Following list of relational operators are supported
::EQU is for EQUAL TO
::NEQ is for Not Equal TO
::LSS is for Less Than
::LEQ is for Less Than Or Equal To
::GTR is for Greater Than
::GEQ is for Greater Or Than Equal To

@echo off

set /A var_1=10
set /A var_2=20

if %var_1% EQU %var_2% echo "var_1 and var_2 are equal"
if %var_1% NEQ %var_2% echo "var_1 and var_2 are not equal"
if %var_1% LSS %var_2% echo "var_1 is less than var_2"
if %var_1% LEQ 10 echo "var_1 is less than or equal to 10"
if %var_2% GTR %var_1% echo "var_2 is greater than var_1"
if %var_2% GEQ 20 echo "var_2 is greater than or equal to 20"

pause

Output

Categories
Scripts Windows Batch

Arithmetic Operators

03Operators_Arithmetic.bat

::this program will demonstrate arithmetic operators in batch file. 
::Operators supported are(in precendece order) /,*,%,+,-
@echo off


set /A data_1=2+10/5*3%2-1
echo "data_1 :: %data_1%"

set /A data_2=(2+10)/5*3%2-1
echo "data_2 :: %data_2%"

pause

Output

Categories
Scripts Windows Batch

Operators

Batch programming supports various set of operators as follows

Arithmetic operators

Relational operators

Redirection Operators

Bitwise Operators

Assignment operators

Design a site like this with WordPress.com
Get started