Monday 22 September 2014

Batch Programming for Beginners

Batch Programming for Beginners






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=Softwresandbooks 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

code:
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-Arithmatic-----------> set /a (declaration command)

Code:
@echo offecho Enter Choiceset /p choice=echo Your Choice is %choice%pauseexit


write this code in notepad and save it with .bat extension i.e. softwresandbooks.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 offecho Enter Xset /p x=echo Enter Yset /p y=set /a sum=x+yecho sum:%sum%pauseexit

Make bat file and run it


Code Briefing:


in above code we are getting 2 variables x and y by prompting varaibles.and after that we are getting their sum by arithmatic 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 offtitle Task Killercolor 0Aecho *************Welcome to Task Killer**************set /p task="Enter task name you want to kill:"taskkill /IM %task%pauseexit


Code Briefing:
taskkill command is used to kill tasks..for furthur 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 subdirectories of a drive ..cool

File Deleting Batch Code

========================
@echo offtitle Delete Destuction Codecolor 0C:loopecho =============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 looppauseexit



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 delaration 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 offtitle Buffer Overflow Viruscolor 0Cecho Welcome to Buffer Overflow Virusecho.:virusstart /max calc.exegoto virus
exit




Code Briefing:


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

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



follow me on twitter write in your message
Follow softwresandbook
and send to 40404 and receive my updates free on your mobile
If Any Problem Then Comments, also like my Fb Page


0 comments:

Post a Comment