Making Batch Files
Introduction:
First off when making a batch file the first line should always be:
This is important.Code:@ECHO OFF
To save a file as a batch file open notepad or any other text editor, write the first line @echo off then go Save As.., All Files, then save it as NAME.bat
Now it is a batch file.
Your First Batch File:
Now that you have saved your file as a .bat and written your @ECHO OFF, try to run it. You should see that it flashes a quick blank Command Prompt then it disappears. This is Because there is no content. Lets add a simple Hello World!
Add this to the second line of your batch file.
Now try to run it again. You should see the same quickly disappearing box but it should say "Hello World!" in it. Now your probably wondering how to make it stay. Well to make it stay you have to add a pause.Code:echo Hello World!
Add this to the third line:
Now your batch file should write thisCode:PAUSE
Congratulations you have now written your first batch file!Code:Hello World! Press any key to continue...
A Second, More Complicated Batch File
Now we will write a batch file with a menu.
First write the @echo off line the write
IF you ran this you would see the menu but when you chose something it would close to fix this we need to add some goto's and code for the options.Code::MENU cls echo Welcome to my menu! echo Please select an an option echo 1 = OPTION1 echo 2 = OPTION2 set choose = 1 set /p choose =Please type your choice:
First lets add the goto's under the last line of the previous code add this:
Now let's add some code for each option here is a simple code for each option:Code:if %choose% == 1 GOTO OPTION1 if %choose% == 2 GOTO OPTION2
Now you have a simple menu.Code::OPTION1 cls echo This is Option 1 pause GOTO MENU :OPTION2 cls echo This is Option 2 pause GOTO MENU
Commands:
COMMANDS USED:
CLS | This clears the screen of any previously written text
ECHO | This writes the text after it
SET and SET /p | These are used to make menus. SET sets the value of a variable and SET /p lets you make it say CHOOSE AN OPTION:_ and so forth.
GOTO | This lets you go to a specific chunk of code.
PAUSE | Pauses the flow of code until any key is pressed.
IF | It is an if command okay do i really need to explain it?
:SOMENAME | This is used to name a chunk of code and should always have a cls as the first line. (IMHO at least)
Notes:
Batch file commands are the same as in a command prompt.
Batch files are very fun for pranks or fake viruses. I may add the fake shutdown virus on here later.
Batch files can be a very useful tool.
Enjoy!