Basic Batch Scripting

Wed, Jun 24, 2009

Windows

Introduction

cmd
So first of all what is batch file? Its a file with a list of commands that are executed by command prompt(CMD) to open your cmd window go to start > run > cmd

Basic Commands

  1. [batch]@echo off[/batch]
    This removes the "C:\Documents and Settings\Administrator>" part
  2. [batch]cls[/batch]
    This clears all writing off the CMD window
  3. [batch]color backgroundcolor-textcolor[/batch]
    This allows you to change the color of the text

    0 = Black       8 = Gray
    1 = Blue        9 = Light Blue
    2 = Green       A = Light Green
    3 = Aqua        B = Light Aqua
    4 = Red         C = Light Red
    5 = Purple      D = Light Purple
    6 = Yellow      E = Light Yellow
    7 = White       F = Bright White

    Example command:
    [batch]color 04[/batch]
    This example would give you red text on a black background

  4. [batch]goto TEXT[/batch]
    This will cause the script to go to a particular section of the script it will be explained more later on in this tutorial
  5. [batch]pause[/batch]
    This Pauses the command prompt and displays the message press any key to continue...

Your first batch script

For this example we will make a simple hello world script.

To get started open up notepad start > all programs > accessories > notepad
[batch]@echo off
color 64
echo hello world
pause[/batch]
Save it with a .bat extension and then double click on it to run it. It should print the text "hello world" in red font on a yellow background and then pause

The goto command

The goto command tells the script to go to a certain part of it
[batch]goto bush[/batch]
this will cause the script to go to the line that starts with
[batch]:bush[/batch]
Example:
[batch]@echo off
echo Hello, this script will repeat the message "you will see this over and over again" to cancel it press ctrl+c
pause
:bush
echo you will see this over and over again
goto bush[/batch]
The message was repeated because after it was printed the goto command made the script start again from :bush

Launching programs from a batch script

Using the start command we can start programs

Example:
[batch]@echo off
color f1
echo this script will open google.com in your default internet browser
pause
start http://www.google.com[/batch]

, , , , ,

This post was written by:

Duffy - who has written 48 posts on Tuts4Tech.

I am the owner of this website, please feel free to ask me any questions you have

Contact the author

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Leave a Reply