Programming

Simple Programming - Part 3

So far we've looked at simple batch files that do simple jobs.  Now we are going to look at something with a little more intelligence (at least, code that appears to have intelligence!).

Let's create two batch files and look at how they interact with each other.

First, test.bat:

echo This is test.bat
call test2.bat
echo Back to test.bat

And test2.bat:

echo This is test2.bat

There's a few new things here for you to digest.  First, echo.  This is just a way of telling the system to print the test onto the screen (a bit like cout for those that might have some experience of C++).

Secondly, call.  This is a way of running another batch file from within another one.

OK, save both and run test.bat.  You should see the following:

OK, it works because you can see the echoed lies from both batch files displayed in the right order.  The test.bat batch file runs first and displays the line "This is test.bat" is displayed.  A call is then made to test2.bat which displays the line "This is test2.bat" before handing back control to test.bat again for the final say.

It works but it's ugggg-ly.  We can get rig of those "c:\files>" prompts by adding a simple line to the beginning of test.bat:

echo off
echo This is test.bat
call test2.bat
echo Back to test.bat

Now the prompt isn't shown but it now says "echo off".  If you don't want that to show simply add "@" at the beginning of test.bat:

@echo off
echo This is test.bat
call test2.bat
echo Back to test.bat

Before we end this third installment of simple programming with batch files, I want to show you how to give your batch files the ability to check if something is true of not and act on whether it is or not.

Take a look at the following code for test.bat:

@echo off
echo This is test.bat
if not exist test3.bat (
echo test3.bat does not exist
pause
) ELSE (
call test3.bat
)
echo Back to test.bat

What this code does is check for the presences of a file called test3.bat.  This file shouldn't exist yet (we've not created it yet) so what should happen is as follows:

  1. The first two lines are run:

@echo off
echo This is test.bat

  1. Now it gets to the "IF" statement.  "if not exist test3.bat" is batch file way of saying "if test3.bat doesn't exist then do something "

if not exist test3.bat (

  1. What that "something" is is the code that is contained in the brackets (parentheses).  This gives you a on-screen prompt that the file is missing and then pauses to wait for you to press any key.

echo test3.bat does not exist
pause

  1. If the file did exist then the ELSE part of the code would run it.  This is only run if the fist statement is false (in this case, the file exists).  Remember to close all brackets.

) ELSE (
call test3.bat
)

  1. Finally, back to test.bat for the final line.

echo Back to test.bat

Here is the batch file in action:

More to follow next time!

Go to Next Part >>

Adrian Kingsley-Hughes
Last updated: May 11th 2006
Print This Page   |   Email me when this page changes    |  Search This Site



Crucial.com System Scanner does the work for you!



links.inc"); ?>

Contact Us