Batch - Beginners Guide Part 1
In this guide, I will be going through getting started with Batch and some basic commands you can do. Just like my other programming guides, I will split this guide into 3 parts to keep it nice and simple.
Please note that I am running a Windows 7 operating system, however Batch can be used through all Windows operating systems I believe as long as you have Command Prompt or a downloaded alternative.
Please note that I am running a Windows 7 operating system, however Batch can be used through all Windows operating systems I believe as long as you have Command Prompt or a downloaded alternative.
The Contents...
- Part 1 - Getting started with Batch - What do you need?
- Part 2 - Basic commands (Echo, Pause etc.)
- Part 3 - Opening applications and webpages using Batch.
Part 1 - Getting started with Batch - What do you need?
So in English, what is a Batch file?
With Batch you can write scripts to perform repetitive tasks. You can write Batch in any simple word-processor just like other languages, and I choose to write it in Notepad, Windows' built-in word-processor. I find Batch files can be useful as for simple tasks in the Console window (Command Prompt)
So how are Batch files created?
Batch files are created by of course writing your script, in a word-processor and then saving the script as a .bat file. This will then create an executable file which will open in Command Prompt by default.
Beginner's Tips & Tricks;
With Batch you can write scripts to perform repetitive tasks. You can write Batch in any simple word-processor just like other languages, and I choose to write it in Notepad, Windows' built-in word-processor. I find Batch files can be useful as for simple tasks in the Console window (Command Prompt)
So how are Batch files created?
Batch files are created by of course writing your script, in a word-processor and then saving the script as a .bat file. This will then create an executable file which will open in Command Prompt by default.
Beginner's Tips & Tricks;
- How do you edit Batch files? - This is easily done by simply right-clicking on the Batch file, selecting 'Open with' and select Notepad or your preferred text-editor.
- Running Batch files? - Running a Batch file couldn't be easier, just simply double-click on the file (treating it like any other shortcut) and the script will run in Command Prompt.
Part 2 - Basic commands (Echo, Pause etc.)
Now the fun part, the coding. Lets start of with @ECHO OFF command, which is important in the Batch world. Also please note that none of these commands are case sensitive, and do not require semi-colons or any other special characters.
@ECHO OFF basically hides all of the unnecessary information shown in the Command Prompt window. So make this the top line of most of your scripts... Let's take a look at a very basic script.
@ECHO OFF basically hides all of the unnecessary information shown in the Command Prompt window. So make this the top line of most of your scripts... Let's take a look at a very basic script.
@ECHO OFF
Echo Hello World
Pause
So you can see we have added the @ECHO OFF command, and have also added a second line. The ECHO command basically displays any information you have written after that command. In this case it will display "Hello World".
We then added a final line, 'Pause' which let's the user actually absorb the information you have written. If this command was not present, the script would take place, however exits the window before you can even see what's happening. The Pause command is replaced with "Press any key to continue..." in the Command Prompt window, and doing so will exit the window.
Lets take a look what this looks like in Command Prompt;
Command Prompt Preview;
If we were to take away the @ECHO OFF command, you will see additional information (location of the file) is displayed which in my opinion is unnecessary and makes the script look cluttered.
Command Prompt Preview;
As you can see the additional information is present. This is why the @ECHO OFF command is very important to Batch developers.
So there's your first basic script. You have simply displayed some information and indicated the script to hide extra information. Lets move on to opening applications and webpages.
Part 3 - Opening applications and webpages using Batch.
A very useful feature of using Batch is that you can launch any webpage through your web browser and open any application. This can be useful if you want to create a quick executable shortcut or bookmark. Lets take a look at some scripts...
start http://google.com/
This will open Google in your default web browser.
start Notepad.exe
This will open Notepad.
Now you will notice that we have taken out the ECHO commands, as they are not needed for these scripts. This is because we do not need to see or view any of the information in the Command Prompt window, however want it to run the script. In which it will do this quickly and launch whichever webpage or application you want.
Remember to save the files as - Anything.bat
Now you will notice that we have taken out the ECHO commands, as they are not needed for these scripts. This is because we do not need to see or view any of the information in the Command Prompt window, however want it to run the script. In which it will do this quickly and launch whichever webpage or application you want.
Remember to save the files as - Anything.bat
So there's the first basic guide on the Batch language. In future guides I will go over more commands and creating a more advanced script. Please stay tuned and remember to share Berrytech with your friends! Thanks.
Berrytech