A BATCH of ECHO, AKA CALL of the *.BAT

· Comment
Authors

No, this is not a James Bond thriller in the lines of Quantum of Solance, nor a cheap little story. It is our introduction to some Batch file programming, more correctly, in modern terminology, for the NT series of Microsoft, Windows shell scripting – among other lusterless names. Most of the information we have been looking for on line has been written for those who have, at least supposedly, advanced knowledge of the matter, although with some of the disagreements found on the forums, it seems that anyone can be an expert.

Our idea is to have as much of this supposedly elementary command in one place.  Several exercises are given to sharpen the reader’s skills.

Since this writer pretends to have a touch of history in his articles, the reader might ask, what the connection is to the past. Well, it goes back to the days of DOS at least. The writer used to implement a version of QBASIC which ran as soon as the computer started up, in order to record some daily activities before they could be forgotten. More advanced operating systems no longer allowed this option, and there was no time available to explore the alternatives until the present. While the batch programs are less elegant, properly written, they can fulfill some requirements for those who would like to record events, or test their knowledge. We will show uses made by computer administrators.

We might divide batch files into 2 categories: those which run by themselves, and those which are run by an external batch command. We prefer the former, but will end with one of the latter. The complete beginner, especially if young, and with lots of available free time, might want to run each command separately, in order to stamp the results into human memory, in a more forcible way. This will be more or less how we start off. Commands are not to be forgotten!

So, this page, for those with some knowledge, will be next to useless. Something more concrete will soon appear, length of author’s health permitting.

Personally, we are using Windows XP. At an Internet Cafe, (depending on where we go), we usually use Windows 7 Ultimate, but since we cannot do everything we would like in such a place, explorations are incomplete.

OK, suppose you are an XP user, or that your machine allows the equivalent. From the Start Button, look for the Command Prompt, represented by a mostly-black icon, with a blue band on the top, and in white letters, C:\_ . It is mostly found by moving along in the menu to “All Programs”, then “Accesories”. (On Windows Ultimate, it was the same.) Click on that, and you will see that a window opens, which will look like those used under pre-Windows 95, e.g. Windows 3.1. Boring stuff, for those who like lots of visual impact, but for those who like to be in control, this is the beginning of one type of mastery! It has helped this writer identify hackers sitting on machines next to him, who were running his machines in parallel. By shutting down the computer being used, we saw, out of the corner of our eye, an abrupt glance – probably of annoyance, which confirmed our suspicion. [It was doubly confirmed the next day, when the owners of the Internet Café informed us that they had been told the same by others.] On the two occasions we reacted thusly, the different suspects left the premises soon after our defensive action. Well, they were victims of hubris, by thinking that their actions could not be detected. This writer does not have the confidence to believe that he could prevent any and all such manipulations on “his” machine, but the risks he takes, are calculated ones.

Well, let’s start with something absolutely useless, but important to know none-the-less.
Inside the command prompt window, where there will be seen a flashing, write CLS, and press ENTER.

The CLS command was an old one, meaning CLear Screen. You will know have eliminated the information about Microsoft.

Tired already? Our next command is EXIT. Type EXIT, and press ENTER. The window disappears. Under some cirumstances, this command will not be necessary.

Let’s start with another command prompt window. Type, “I want to exit” (without the quotation marks”), and press ENTER. You will get a message that this is not recognizable. But if you type “Exit is what I want to do.”, after keying “Enter”, the screen disappears. Different grammar for the same message, but for the computer, a different message completely. You cannot start typing something with a word which is a command, if that command is not to be carried out.

Now we get to the crux of this article. ECHO. Let’s open our command prompt window again:
write ECHO, and then execute by pressing ENTER. (From now on, “run”, “enter”, or “execute” will always refer to this procedure). You will get the message, “ECHO in on”. Boring stuff, eh!

Try it again. Same result. This is getting tiresome. Let’s try a small variation. Type ECHO ECHO. Run. On the line immediately following, depending on whether you chose capital or small letters, you will see the word “echo” printed as you defined it.

Let’s make sure the machine is not acting quirkily. Try this message:

ECHO There is an echo here.

There must be!

But, what happens if we try: ECHO Exit. We seem to be in luck. Our screen is still there. How about:

ECHO CLS.

Things are still cool. Just remember, you can’t type these things in reverse, CLS ECHO is useless, that’s just CLS for the computer.

OK, now for en eye-sight test. This writer knows his eyes are bad, which could account for some spelling mistakes – which in any event, are not as serious as the incorrectly-translated instructions for QBASIC he had in Spanish, which caused the program not to run, until he finally discovered the error. Anyway, just to make thinks clear, run ECHO again. Look carefully at the screen. Now, type this: ECHO. [with a period at the end]. Can you describe exactly what happened?

A blank line appeared on the screen, in place of the previous text.

Now, before we can show some other variations with ECHO, we need to try out some other command. Do you have any files in “Documents and Settings”? Find out, by typing “Dir” (without quotation marks) in the commnand prompt, and execute. You will get a listing, if there is one. Should there be nothing there, click on “My Computer”, choose “Documents and Setting”, then choose “Administrator”. (If you can’t get here, you do not have the necessary privileges.  Sorry!) Here, right click, choose the new text file option. Click on that file, write something inside, a few words at the most, and save it with a simple name, like “test.txt”. To avoid clutter, it may be best to rename the file externally, but it doesn’t matter. Personnally, we have some files with 3 letter names preceding the dot and txt file type description. Short and sweet! One letter, like b.txt would be good enough.

Now, back in the command propmpt window, type TYPE test.txt, or whatever. The contents of the file will be printed out. This will give a pure DOS look, although it has nothing of that operating system.

Now, for that old Windows 3.1 loook, type EDIT test.txt, or whatever your file is called. A window of the old style will open, offering an opportunity to edit. There are options to change the colors, if you do not like them, or even the screen presentation (none of which this author likes). Leave this editor by clicking “File” at the left, and choose “Exit” or its equivalent in the menu which opens.

And if you ever forgot to do something with that file, you can go back in the command prompt, by pressing the “up” arrow. In fact, you can go back several commands, and sometimes save time.

Just about every lesson of programming gives you “Hello, World”. So, let’s do a stupid ECHO “Hello, World!”. Not too impressive, a screen full of junk.

A small improvement, CLS, followed by ECHO “Hello, World!”. Much less screen clutter now, but not yet ideal.

Let’s change our ECHO command to @ECHO. We write that, run, and get the old “ECHO is on” message. So, what’s new? If you see something interesting, that’s it.  Otherwise, it’s an error.  Live and learn.

Try this, now: ECHO “Hello, World!”. You see nothing different. No need for panic, it will be useful.  This is just to clutter up the screen a bit before our next experiment.

Now, moving along, let’s kill the command: write ECHO OFF (no period). Hmmm, the cursor is now in at the extreme left our our screen. Put ECHO “Hello, World”, run, or just choose one word, even the quotation marks are not necessary. We see that things are becoming more compact on the screen.

Try ECHO OFF, followed by, on a new line, just ECHO, and then, on another line, ECHO. (with a period).

You will see that the ECHO command, used alone, reports on whether it is on or off, or dishes out an empty line.

Try the ECHO “Hello, World!” routine again, but first, CLS! We will be down to 5 words on the screen. Can we do any better than that?

Yes, we write @ECHO OFF. Try ECHO “Hello, World!” again. No difference! What was the point?

OK, to show the usefullness of the symbol, let’s first learn one new command, without which, we may have the screen flash in front of our eyes, without giving us time to read it (depending on the commands).

PAUSE

We write that, and the computer tells us to press any key to continue. This is useful under two circumstances, the first, just described, to give us a chance to see what is going on, and second, to give the computer a chance to do whatever it is that we instruct it to do.

We will analyze the following, which should be written into EDIT, not WordPad or Word, and saved, for example, as TEST.cmd, in the “Documents and Settings\Administrator” folder. Be sure not to save as a text file, read carefully before saving.

ECHO
PAUSE
CLS
PAUSE
ECHO CLS
PAUSE
ECHO.
PAUSE
TYPE TEST.TXT
PAUSE
ECHO. BE SURE TO EXIT TEST.TXT AFTER VIEWING.
PAUSE
ECHO If we had not put this pause, the preceding message would not have been seen.
EDIT TEST.TXT
PAUSE
CLS
ECHO OFF
ECHO.
ECHO HELLO, YOU HAVE ECHO OFF
ECHO.
PAUSE
@ECHO OFF
ECHO.
ECHO HELLO, YOU HAVE @ECHO OFF
ECHO.
PAUSE
@ECHO ON
ECHO.
ECHO HELLO, YOU HAVE @ECHO ON
ECHO.
PAUSE

EXIT

Now, open up a separate command prompt, and from there, we will have our last new command, CALL.

Type, into the window: CALL test.cmd, and press ENTER. Pay special attention to what happens with the various forms of ECHO HELLO, YOU HAVE *ECHO O*.

Pictorally, we now show a program which uses less commands, but which may be even easier to analyze than the one above.  We do not print it out in text format, because the repeated use of one word, (found in our title), will have us accused of key-word stuffing. Perhaps the reader can use OCR software.  Anyway, it should not take too long to copy out the text.  If you have just one file in the directory, just use the command “dir” or “DIR”.  If you have several, it is suggested that you select the only file beginning with a single letter, such as we have, here, with dir t*.*.  If further refinement is needed, add a second letter, such as in our illustration further below, where we use sh*.*.  DO NOT USE TEMP1.TXT if you already have that in your folder, change it to something you do not have. Click on the image to see it full-size.

pic of text

Batch to test various forms of ECHO, according to prefix, suffix, or other word.

Now, the use of the “DIR” command really clutters up the screen!  For the purposes of testing our program, we do not need this, that’s all right if you are going to crate a record of your drive or folder, but we just want some basic information.  For that, we will use the format DIR /B, which gives us the plain facts.  Compare the information in the following conjoined screenshots.

DIR, 2 versions.

Comparison of information shown with the command “DIR” and the command “DIR /B” (with the selection of files beginning with the letter “s”).

 

We will now, using the above knowledge, use a combination of DIR and ECHO. Note that the differences can be quite subtle. Click on the image to see it full-size.

Scr Output

Screenshot, showing commnands to be entered on the right, and output on the left. Should @ECHO OFF be used instead of the last ECHO OFF, the instruction will not be printed, and the output will be four lines of “Sherwood.bat” (in the author’s personal example) instead of the separation into two blocks of two.

We suggest the symbol @ be restricted to when the user wants clean output, not requiring any on-screen instructions. To avoid errors, always start off with pauses before and after commands, save as a CMD file, but keep the editor open, to save time in modifying the text.

SUMMARY: You have learned how to use ECHO in various formats. Why use ECHO ON when ECHO OFF gives a cleaner screen? For the same reason we use PAUSE – we might want to see at exactly which point the program is operating, or what it was that we were trying to do. This is useful in finding errors. CLS is our eraser, EXIT is the drain down which it all goes in the end. TYPE is a simple way to see a text file printed on screen, EDIT is a more powerful version, not to be confused with Notepad.exe. In fact, that option apparently exists – you might (at your own risk) try your test.txt as follows, on the command line:

test.txt > notepad

Our own result seems to work, but in truth, it creates an empty file in our administrator folder, called “notepad”.

Incidentally, while we have appended some comments to our ECHO, the standard instruction for this is REM, that is a reminder, or something to remember. A good place to put such a reminder, is at the beginning of a batch file which will be used more than once, or sometime again in the future, for review purposes.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: