To know more about set command just type 'set /?' in command prompt or look at the windows command reference help file. Now here is an example program of set command.
2. For Loop : Syntax of For loop is 'for %%{variable} in (set) do (command)'
@echo off
FOR %%i IN (Windows, Linux, Mac_OS) DO (echo %%i)
Pause
exit /b
For loop on numeric mode
Syntax : 'for /L %%{variable} in (starting point, step, end point) do (command)
@echo off
FOR /L %%i IN (1, 1, 20) DO (echo Hello world!!!)
Pause
exit /b
the above script prints 'Hello world!!!' 20 times, In For loop (1, 1, 20), where first '1' is starting point and second '1' is step means first '1' is increased by 1 at every loop and 20 is end point, when first 1's value is 20 then for loop is end. We also write this loop as : 'for /L %%i in (20, -1, -1) do (echo hello world)'
Conditional Statement in Batch Script:
1. if-else :
@echo off
:Again
set /p num="Enter A Number{0 to continue}: "
if %num%==0 (goto Again) else (
echo.
echo You have entered %num%)
pause > nul
exit /b
2. if not-else :
@echo off
echo.
set password=12345
:pass
set /p user_input="Enter The Password : "
if not %password% == %user_input% (
echo Wrong Password ?
echo.
goto pass ) else (
echo.
echo Congratulations..!! You have SuccessFully Log in
pause > nul
exit /b)
3. if exist :
@echo off
echo.
if exist "C:\Windows\SysWOW64" (echo Your Operating System is 64 bit) else (
echo Your Operating System is 32 bit)
echo.
Pause
exit /b
Arguments:
We also use arguments in batch scripts, to access arguments use %1, %2 .... where %1 denotes first argument and %2 denotes second. Here is an example script which shows how to use Arguments :
@echo off
set /a ans=%1+%2+%3
echo.
echo The Sum of %1, %2, %3 is %ans%.
pause > nul
exit /b
Above program takes three arguments, then print their sum. Make sure run this script via command line.
Coloring in Batch Script:
To create a color-full Batch script use color command, with this command we can change the text color as well as background color. The color code of Batch script is
0 = Black 8 = Gray
1 = Blue 9 = Light blue
2 = Green A = Light green
3= Aqua B = Light aqua
4 = Red C = Light red
5 = Purple D = Light purple
6 = Yellow E = Light yellow
7 = White F = Bright white
For more info about color command type 'color /?' in command prompt. To change the text color only type
Syntax : color [color code]
Example : color a
For changing text and background color use :
Syntax : color [background color][text color]
Example : color 1a
try out this command with different combinations of colors.
Multicolored Batch Script:
Now here is a Batch Script which shows multiple colors in a single script :
@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
rem Prepare a file "X" with only one dot
<nul > X set /p ".=."
echo.
call :color 0a " Hello Guys This IS Test.... "
echo.
call :color 0b " This IS The Second Colour........ "
echo.
call :color 0c " This IS The Third Colour........ "
echo.
echo.
pause >nul
del X
exit /b
:color
set "param=^%~2" !
set "param=!param:"=\"!"
findstr /p /A:%1 "." "!param!\..\X" nul
<nul set /p ".=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
exit /b
Script source page :
http://stackoverflow.com/questions/4339649/how-to-have-multiple-colors-in-a-batch-file/5344911%235344911
Functions in Batch Script:
We can also create functions in Batch programming. Functions are simply a reusable piece of code, in Batch programming a function is creates using a Label and goto :EOF command. Here is an Example of a simple function
:print
Echo Hello this is my first function.
Echo Which Prints this Message.
Goto :EOF
A function can be called by call command which followed by function's label.
Example : call :print
So the above script look like this
@echo off
call :print
pause > nul
exit/b
:print
Echo Hello this is my first function.
Echo Which Prints this Message.
Goto :EOF
for more information and detailed tutorial about functions check out below link
http://commandline.co.uk/lib/treeview/index.php
http://www.dostips.com/DtTutoFunctions.php
Now here is function written by me which returns the IP address of given website url
@echo off & setlocal ENABLEEXTENSIONS
echo.
set /p url="Enter The WEBSITE URL : "
call :SiteIp %url% ip
echo.
echo The IP Address of %url% is : %ip%
pause > nul
goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:SiteIp %address% ip
::
:: Func: Return the IP address of given Website URl.
:: If function fails, it shows "Problem in Connection.??"
::
:: Args: %1 var for WebSite URl & %2 to receive IP Address.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS & set "ans=Problem in Connection.??"
for /f "tokens=3" %%a in ('ping -n 1 %1^|findstr /B Pinging') do ( set d=%%a)
if exist %d%=nul ( endlocal & set "%2=%ans%" & goto :EOF)
for /f "delims=[]" %%b in ('echo %d%') do ( set data=%%b )
endlocal & set "%2=%data%"
goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
In this below link has a huge collection of ready made functions
http://www.dostips.com/DtCodeCmdLib.php
Here is the list of links for Batch Scripting References :
http://www.dostips.com/
http://ss64.com/nt/
http://en.wikibooks.org/wiki/Windows_Batch_Scripting