Saturday, May 16, 2015

BAT Tutorial begining











This is a copy paste of a tutorial that posted by someone else i can't remember thanx to him.

Very Basic Batch Tutorial


NOTE!: Whenever something is in quotation marks ("example"), do not include the quotation marks (if you see that you must just type: example)
 

Step 1: So, what is Batch
For those who don't know, a Batch file is a text file containing a series of commands to be executed by Command Prompt (CMD). When a batch file is run, CMD reads the file and executes its commands.

To open your CMD, click START > RUN > type "CMD" > OK
(some of you may need to type "CMD.EXE")
(or you can click START > ALL PROGRAMS > ACCESSORIES > COMMAND PROMPT).
Step 2: Now what?
These are some VERY basic commands you need to know:
(please try out some of these commands in your CMD).

@echo off - This gets rid of that "c:\documents...etc"

echo - This displays a message, (e.g "echo hello" = "hello") because if you type "hello" into CMD it will interpret "hello" as the command

cls - Clears the CMD of all text.

color - Changes the color (type "help color" for a list of colors).

goto - Goes to a particular word in your text (you will learn more later)

pause - Pauses the command prompt and displays the message: "Press any key to continue..."

and there are a few more we will learn about later.




Step 3: But what if I want to run a lot of commands.


That is where BATCH comes in.

Try copying this into your notepad (NOT Microsoft Word) and save as BatchFile.bat
Notepad is in accessories
NB: You must save it as .BAT or it won't work.
P.S (copy BETWEEN the stars, don't include them)
***********************************************************
echo If you type echo before a sentence it displays it as a message!.
echo Wow, thats pretty cool!
***********************************************************
copy that into notepad and save as .bat
Double-click on the .bat file...cool hey!
NOPE. It went too fast...try adding a pause at the end, like this:
***********************************************************
echo If you type echo before a sentence it displays it as a message!.
echo Wow, thats pretty cool!
PAUSE
**************************************************************
There it worked, but its quite hard to read with all those file paths in the way...
thats why we add an "@echo off" to the top;
like this:
***************************************************************
@echo off
echo If you type echo before a sentence it displays it as a message!.
echo Wow, thats pretty cool!
PAUSE
*******************************************************
There! that worked perfectly, you have now written your first batch file!









Step 4: The GOTO command


Now we will learn how to use the GOTO command.

The GOTO command jumps to a certain part of your text.
Example:
if you type "goto fish"
then it will goto the place that you have typed in ":fish"
NB: you must always have a colon (:), before a word that is going to be GOTO'ed

Example:
you know the drill, copy into notepad and save as a .bat file.
(do not copy the stars)
**************************************************
@echo off
:fish
echo This is only one message being repeated over and over; Press Ctrl+C to stop
goto fish
**************************************************
*Note: Ctrl+C is the universal way of stopping a batch file

What happened was that every time it typed "Press Ctrl...etc"
the GOTO command made it go to the top.
Pretty nifty eh?


Step 5: The START command
The START command starts something.
Not much to explain here.

For example:
open your CMD and type:
"start www.instructables.com"
This opens your default explorer and goes to the Instructables site.
OR
"start itunes"
OR
You can also open files or folders:
"start desktop\foldername

**NOTE: To access a file\folder which has spaces in the name, put quotation marks (" ") around it.
e.g start "C:\desktop\folder name"


Not So Basic Batch Tutorial

In the previous tutorial we learned the core essentials of writing batch files.

If you have no clue what batch is, but want to learn it, refer to the "
Very Basic Batch Tutorial".

In this tutorial you will learn more advanced commands and how to use them to create an application.
 
Step 1: Variables Step (1/3)
Variables are things that are subject change.
When we create a variable, we are creating something that we want the computer to remember for us, something that we can use later on in a program;
we give the computer the value that we want to store and we give it a label to store it under.

we can create integers and strings using variables.

To create a variable you need to learn the SET command.

The SET command is what creates variables;

SET name=value

Type the following into your CMD:
SET name=hello

'name' is the name of the variable, and 'hello' is what the variable is storing

so now every time you type "echo name" it should say "hello" yes?
NO

if you want to display a variable you must put percentage (%) signs around it.

So therefore if you type "echo %test%" and it should say "hello" yes?
YES
Step 2: Variables Step (2/3)


So now surely we can do maths?

we type
set num=1
This creates a variable called "num" with a value of 1 attached to it.

then
set num=%num%+1
(this means that we take "num" (aka 1) and make it num+1 (aka 1+1))

then
echo %num%
it should give us 2, right?

let's try it:
type the following in notepad and save as MathAttempt.bat
(do not include the stars (*))


@echo off
set v=1
set v=%v%+1
echo %v%
pause


it should say 2, yes?
NOPE.
it says 1+1

because the computer interprets your command as:


you: "so num=1, right?"
pc: "right"
you: "so what is num plus one?"
pc: num+1 = 1+1


so the computer interprets your command literally.









Step 3: Variables Step (3/3)


So how do we get the computer to think mathematically?
Simple, we add an /a before the variable name

For example:

we type
"set /a num=1"

then
"set /a num=%num%+1"

then
"echo %num%"

then we should get 2, right?
let's try it
Type this into notepad....blah blah blah, you know the drill.
***************************************************
@echo off
set /a num=1
set /a num=%num%+1
echo %num%
pause
****************************************************
there! it added 1+1!
this is how the computer sees it:
_
you: so num=1, right?
pc: right
you: so what is num plus one?
pc: num+1 = 1+1 = 2


Voila!

So now lets make a counting program!
we will use the goto command that we learned about in the Very Basic Batch Tutorial.

*********************************************************
@echo off
set /a num=1
:top
set /a num=%num%+1
echo %num%
goto top
**********************************************************
The computer is adding 1, then going to the top and adding 1 again etc.





Step 4: Parameters Step (1/2)

So now that we can use variables what if we have a choice of options, like:
press 1 to say Hello.
press 2 to say Goodbye.

We use the "IF" command, for example:

Type this in your CMD:
if 1==1 echo See it works!
(==) means "is equal to", you could also type "EQU")
We got a message saying "See it works!"

Now type this:
if 1==2 echo It Works!
We didn't see anything because 1 does not equal 2

If we want to wait for the user to put something in we add a /p and leave the part after the variable empty.

Like this:
set /p variablename=

That means that the computer will wait for you to put in something.

so we type:
**************************************
@echo off
set v1=hi!!
set v2=bye!!
echo Press 1 to say HI!
echo Press 2 to say BYE!
set /p you=
if %you%==1 echo %v1%
if %you%==2 echo %v2%
pause
**************************************
This is telling the computer that if we type 1, it must echo HI!, and if we say 2 it must echo BYE!!


Step 5: Parameters Step (2/2)


So now we know that if we want to choose a variable we type:
set /p variablename=

and if we want to set a variable, we type:
set /a variablename=value

So now why not make a little program that counts to and from 2000?
We will use SET, IF and GOTO in this program (and obviously echo)
*************************************
@echo off
set /a num=0

:top
set /a num=%num%+1
echo %num%
if %num%==2000 goto goback
goto top

:goback
set /a num=%num%-1
echo %num%
if %num%==0 goto top
goto goback
**************************************
So now, whenever it reaches 2000, the IF command makes it GOTO the second part which makes it count down, then when it reaches 0, it will GOTO the first part which makes it count up...etc etc etc




Some Cool Batch Applications

This is my first instructable so no complaining!
I'd like to thank neodudeman for his instructables, they helped a lot.

In this instructable i will show you some pretty nifty applications with batch commands.

If you have any problems or changes to request just send me an e-mail at jbell@live.co.za
Leave any feedback in the comment section below.
 

Step 1: Batch Calculator


This is...well... a calculator.
just copy and paste into NOTEPAD and save as calculator.bat
the filename is not important but the extension MUST be saved as " .bat "

@echo off
title Batch Calculator by seJma
color 1f
:top
echo --------------------------------------------------------------
echo Welcome to Batch Calculator by seJma
echo --------------------------------------------------------------
echo.
set /p sum=
set /a ans=%sum%
echo.
echo = %ans%
echo --------------------------------------------------------------
pause
cls
echo Previous Answer: %ans%
goto top
pause
exit

Step 2: Folder Protector


This is pretty cool as it requests a password for a folder.
Once you copy it into notepad you must insert your own password and designated folder directory into the designated areas!
NB: THERE ARE TWO PLACES IN WHICH YOU MUST ENTER INFO!!!

@echo off
title Folder Password v1.5
color 0a
set /a tries=3
set password=***ENTER YOUR PASSWORD HERE***
:top
echo.
echo ----------------------------------------------
echo.
echo Folder Password
echo.
echo ----------------------------------------------
echo You have %tries% attempts left.
echo Enter password
echo ----------------------------------------------
set /p pass=
if %pass%==%password% (
goto correct
)
set /a tries=%tries -1
if %tries%==0 (
goto penalty
)
cls
goto top
:penalty
echo Sorry, too many incorrect passwords, initiating shutdown.
start shutdown -s -f -t 35 -c "SHUTDOWN INITIATED"
pause
exit
:correct
cls
echo ----------------------------------------------
echo Password Accepted!
echo.
echo Opening Folder...
echo ----------------------------------------------
explorer ***ENTER FOLDER PATH HERE***
pause

*NOTE: This is not an advanced piece of software, it does not hide or block the destination folder or anything like that so don't get your hopes up.
**Yes, it can now open files with spaces, just use a backslash (\) when typing your filepath.










Step 3: Guessing Game


This is a game in which the computer generates a random number, and you must try to guess it.

@echo off
color 0e
title Guessing Game by seJma
set /a guessnum=0
set /a answer=%RANDOM%
set variable1=surf33
echo -------------------------------------------------
echo Welcome to the Guessing Game!
echo.
echo Try and Guess my Number!
echo -------------------------------------------------
echo.
:top
echo.
set /p guess=
echo.
if %guess% GTR %answer% ECHO Lower!
if %guess% LSS %answer% ECHO Higher!
if %guess%==%answer% GOTO EQUAL
set /a guessnum=%guessnum% +1
if %guess%==%variable1% ECHO Found the backdoor hey?, the answer is: %answer%
goto top
:equal
echo Congratulations, You guessed right!!!
echo.
echo It took you %guessnum% guesses.
echo.
pause


*Note: For the backdoor, type "surf33"


Step 4: Site Selector


This is an application that lets you select from a list of sites.
Feel free to add some of your own sites.

@echo off
color 2f
title Site Selector by seJma
:top
echo ***************************************************************
echo.
echo Site Selector
echo.
echo ***************************************************************
echo.
echo Key: [1] Google - Search Engine
echo [2] Hotmail - Mail Server
echo [3] Yahoo - Search Engine/Mail Server
echo [4] Facebook - Social Networking
echo [5] Myspace - Social Networking
echo [6] CNN - News
echo [7] Weather - Weather
echo [8] WikiHow - A How-To Website
echo [9] Instructables - A How-To Website
echo [10] YouTube - Online Videos
echo [11] Answers - Online Encyclopedia
echo [12] Wikipedia - Online Encyclopedia
echo.
echo [e] Exit
echo.
echo ***************************************************************
echo Enter the number of the website which you would like to go to:
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==1 start www.google.com
if %udefine%==2 start www.hotmail.com
if %udefine%==3 start www.yahoo.com
if %udefine%==4 start www.facebook.com
if %udefine%==5 start www.myspace.com
if %udefine%==6 start www.cnn.com
if %udefine%==7 start www.weather.com
if %udefine%==7 start www.wikihow.com
if %udefine%==9 start www.instructables.com
if %udefine%==10 start www.youtube.com
if %udefine%==11 start www.answers.com
if %udefine%==12 start www.wikipedia.com
if %udefine%==e goto exit

cls
echo ***************************************************************
echo.
echo Thank You for using Site Selector by seJma
echo.
echo ***************************************************************
echo Type [e] to exit or [b] to go back and select another site.
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==b goto top
if %udefine%==e goto exit
:exit
cls
echo ***************************************************************
echo.
echo Thank You for using Site Selector by SEjMA
echo.
echo ***************************************************************
pause
exit


Step 5:


This is a fake virus that I designed, it looks pretty convincing if you put it in someone's startup folder.
NB: this batch starts a "60 second till shutdown" script when its done. to abort shutdown click START>RUN:shutdown -a
NB: this does not do anything harmful or destructive to your computer.

***NEW: I've added an extra command that disables themes to make it look more realistic.


@echo off
color 47
net stop themes >nul
title DEEP VIRAL INFECTION!
echo VIRAL INFECTION!!!
echo VIRAL INFECTION!!!
echo VIRAL INFECTION!!!
echo ERROR!!!
echo -
echo virus - TROJAN_DEMOLISHER code #45643676
echo -
echo FIREWALL - FAILED
echo -
echo ANTI-VIRUS - FAILED
echo -
echo IP ADDRESS BREACHED!
echo -
echo VIRUS ATTAINING: ****-****-****-8894
echo -

pause
cls
echo -
echo SCANNING INFECTED AREAS...
echo -

pause

set /a num=0
:repeat1
set /a num=%num% +1
echo %num%
if %num%==100 goto end
goto repeat1
:end
cls
echo -
echo 86.5 PERCENT OF MEMORY INFECTED
echo -
echo INFECTION FATAL!
echo -
echo DELETION OF ENTIRE CONTENTS OF LOCAL DISK C: REQUIRED
echo -

pause
cls
echo -
echo DELETING HARD-DRIVE C:
echo -
dir /s

pause
cls
echo -
echo CONTENTS OF HARD-DRIVE C: ERASED
echo -

pause
cls
echo -
echo SCANNING...
echo -

set /a num1=0
:repeat2
set /a num1=%num1% +1
echo %num1%
if %num1%==100 goto end1
goto repeat2
:end1
cls
echo -
echo 0.00 PERCENT OF HARD-DRIVE INFECTED
echo -
pause

echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR

pause
cls
title SYSTEM FAILURE
color 17
echo ERROR!
echo -
echo VISUAL MEMORY LOST!
echo -
echo RAM LOST!
echo -
echo CORE PROCESSOR FAILING...
echo -
echo TOTAL SYSTEM CRASH IMMINENT!
echo -
echo -

pause
cls
echo -
echo -
echo -
echo SHUTDOWN COMPUTER NOW TO AVOID RISK OF FIRE!
echo -
echo -
echo -

pause
cls
echo -
echo -
echo -
echo SEEK PROFESSIONAL HELP IMMEDIATLY TO PREVENT FURTHER DAMAGE!
echo -
echo -
echo -
pause
start shutdown -s -t 60 -c "SYSTEM FAILURE<SHUTTING DOWN TO AVOID FURTHER DAMAGE!"


**NB: Type "shutdown -a" in your Run box to abort a system shutdown
***Type "net start themes" to in your Run box to get the themes service working






No comments:

Post a Comment