Tag Archives: ECHO

Replaceable parameters in batch file

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).
Previous article: Stopping a DOS batch file.

Photo: freeimages.com

With the information covered so far we are able to use batch files to make our lives easier, and help our day to day computing operations. However, we could make our batch scripts more sophisticated by avoiding the big limitation that we have been faced with so far. That is – our files are hard-coded (written in stone, so to speak) and if we want to manipulate a file, we have to change the batch script again and again.

What if we wanted to perform a particular operation on a different file each time? We can use replaceable parameters to help us with this task. The two important factors in using replaceable parameters are – how to code them in the batch file, and second – how to give the values for the replaceable parameters to the batch file. The latter is rather quite easy. Replaceable parameters are to be entered after the batch file name and before pressing the Enter key. For example:

C:\>batch-params.bat Par1 Par2 Par3 Par4 Par5

Either a space or a comma character separates each parameter. Both are treated the same. Now, how many replaceable parameters does the above line have? The obvious answer would be five. This would be wrong. DOS counts the name of the batch file itself as a parameter, so there are 6 replaceable parameters on this line. Much like other programming languages DOS names them %0, %1, %2, %3, %4, %5 rather than %1 through %5.

DOS uses replaceable parameters as if they do not exist – for example, everywhere you see a replaceable parameter, the DOS sees the value of that parameter. When a batch script executes with ECHO ON, the DOS precedes each command in the batch file prompt. DOS treats the commands in the batch file exactly like it would if you entered them from the DOS prompt.

:: batch-params.bat
:: Example with Replaceable Parameters.

The first 2 lines are remark statements that show the name of the file and a brief description of the program. The third line echo’s the first replaceable parameter entered on the command line. If replaceable parameter was not entered, the command will display the Echo is on message (just like with simple ECHO command). The fourth line shows the 2nd parameter. The fifth line shifts the argument. The line 6 acts like line 4, but because of SHIFT, it and echo’s third argument. SHIFT command allows to use more than 10 parameters / arguments in the batch file:

SHIFT changes the values of the batch parameters %0 through %9 by copying each parameter into the previous one. In other words, the value of %1 is copied to %0, the value of %2 is copied to %1, and so on. More in MS Help site.

The SHIFT command discards the first parameter %0, moves the remaining parameters down one value, and brings in a new value if one exists. Therefore after the SHIFT command gets read, the value in %1 is moved into %0, and the value in %2 moves into %1 and so on and so forth.

The SHIFT command really has two major purposes. First, by moving the parameters down into a lower replaceable parameter, a single replaceable parameter may be used for all coding by forcing the batch script to loop through the code. Second, it allows the batch file to handle more than nine replaceable parameters. The total number of replaceable parameters is constrained by the 127-character command line limitation (for old OSes), and starting Windows XP or later you can use longer command line – up to 8191 characters. A space, comma or semicolon is also a requirement to separate each replaceable parameter.

If you start our example batch file with the parameters: text1, text2, text3 and text4, you will see the following output:
C:\>batch-params.bat text1 text2 text3 text4
C:\>ECHO text1
C:\>ECHO text2
C:\>ECHO text3

And, note: we do not access fourth parameter (%4), so it is not shown in the console. Also, see the screenshot from Windows 8.1 CMD.exe:

Showing output example from the batch file with parameters
Showing output example from the batch file with parameters

Next article: Loops in the Batch files with examples.

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

Update Feb 21, 2015. Added link to the next article.

First cmd / DOS batch script example

Photo: freeimages.com

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

Previous article: What is a batch script?
Next article: Stopping a DOS batch file

Now we are familiar with the command prompt. Batch scripts are one or more DOS commands that are executed sequentially, and the script is written using a text editor. This does not mean you can use Microsoft Word or Google Docs or any other of those fancy programs you might use to type up a report. Those types of programs are called word processors, not text editors. Besides the obvious that these programs allow you to change colors and import graphics, these programs add extra formatting that you don’t see.

A great example of a text editor is Notepad. Notepad comes bundled up with Microsoft operating systems (or better choice is open source editor – Notepad++ which supports syntax highlighting). To access Notepad simply click the Start button followed by the Programs option. Next, click on Accessories which is usually at the top of the column. The next pop-up column should contain the Notepad program option. Or on newer Windows, hit Start, and type Notepad.

Search - Notepad on Windows 8.1
Search – Notepad on Windows 8.1

A helpful hint is to make a shortcut to the Notepad in your desktop or Taskbar by right-clicking on the Notepad option (or Pin to Taskbar on the newer Windows). Subsequently select Send To followed by selecting Desktop (Create a Shortcut). This allows quick access in the future by placing an icon on your desktop. A shortcut can also be made using batch.

At this point I highly recommend to download the Notepad++, or change Folder options in the Windows, to see file extensions, so you can easily change .txt file extension to the .bat for the batch files.

Notepad++ editing .bet file under Windows 8.1
Notepad++ editing .bet file under Windows 8.1

Now that we have the Notepad open we can begin scripting. Let’s begin by printing the words Hello World to the screen. Type the following into Notepad screen. Then save our new text file as helloworld.bat or my-first-batch-script.bat, or anything you like.

@Echo off
Rem helloworld.bat

:: Next line prints Hello World to the screen
Echo Hello World


Now let’s explain what each line does.

The computer has a habit of taking everything you type and storing it verbatim. This can be annoying and troublesome at first however you will soon realize that predictability is nice. Anyway, this means that all commands will be seen by the end-user. This makes for a very ugly and bothersome execution.

To circumvent this we will type @ECHO OFF at the first line. ECHO OFF makes sure that command echoing is turned off. This fixes our previous problem. However, it creates a new one. ECHO OFF is a command thus it will also be seen. That is where the use the @ (at) sign comes in. This stops the echo of the ECHO OFF command. If you were to leave the @ sign out you would see ECHO OFF above your Hello World message as part of your output.

The majority of the batch scripts have this snippet of code at the beginning. You can also turn echoing back on if you like by using the command ECHO ON. If you type ECHO by itself the status of ECHO will be displayed. The ECHO OFF command only turns off command echoing. The output from the DOS commands will still be seen.

When you copy a file in a batch file, you will still see the 1 File(s) copied message. If you use a batch file to start a GUI program, that program will indeed appear on the screen (For example, Calculator, Notepad, MS Word, MS Excel). ECHO OFF turns off batch file commands seen in the console while that specific batch file is running.

Here is a screenshot of your first batch file’s output:

Hello World.bat output
Hello World.bat output

The ECHO command is great for communicating with the user. Messages and questions can be easily delivered to the end-user. This is great for giving the user a list of choices and wanting feedback before a particular action is performed. Try experimenting with different formats for delivering messages to the user. For example, centering the text using spaces (this works because most consoles uses fixed-width fonts by default). Or adding hyphens and exclamation marks for borders:

@echo off

echo ---------------------------------
echo !     Your hello message...     !
echo ---------------------------------

Using fixed width fonts in the console
Using fixed width fonts in the console

You can create a line break, a blank line, by typing ECHO. but notice the period mark after the echo command. The ECHO and period combination is what creates the line break. Example:

REM helloworld.bat
:: Prints 'Hello World' to the screen...
:: with a line break in between the words.
ECHO Hello
ECHO World

Lines 2, 3 and 4 in this batch file example are comments or remark statements. When writing batch scripts you will want to place two comments at the top of the file. Coding standard style should be in place, which improves the quality of software which results in fewer bugs which is easier to maintain and use. The first line should contain the name of the file and the second remark statement should include a brief statement about the following program. This helps yourself when you start writing many batch files and it also helps someone else (for example, your colleagues) read your script that might not be as familiar with it as you. This could perhaps be the case of having a friend debug your file.

The first remark statement has the REM signifying a remark. Such remarks are used in every programming language to add comments to your code. Since your first batch scripts will be smaller programs, we won’t need very many remark statements, though it is a good idea to get in the habit of documenting your scripts well. Remark statements are ignored by DOS. Notice the :: (two colons) in the next remark statement. This is another way of commenting your batch scripts. The double colon approach is a better way of commenting your code because the command prompt will entirely ignore this line making your program run faster.

Yes, REM is ignored however the command prompt takes a more detailed approach at reading the comment and it takes a longer amount of time to execute (not relevant on modern PC). The double colon makes for a quicker program. This gives you advantages when you start building more complicated programs and you start adding more of them. For the preceding reasons we will not use the REM command rather we will use the double colons when commenting our code.

Another characteristic of batch scripting is that line breaks (spaces between lines) in your batch file are ignored. Here they are used merely for readability. Notice the two above / previous examples each have a different line break scheme. The first implements a double space layout for better readability. The second is more compact.

ECHO is similar to the PRINT command in other programming languages. ECHO is used for displaying a string of text to the screen.

Finally, we have the PAUSE command. Your program will run just fine without this. However, you will not be able to see your output; the text Hello World in this example. If you include the PAUSE command you will see the text string Press any key to continue… If you do not include this command the command prompt may appear, then flash away quickly (depends on how you run batch scripts). Run the program without this and note the difference (first run by double clicking mouse, second – from the CMD.exe console).

Note that batch commands are usually not case sensitive. All caps are shown in this article to help you identify the batch code. When an instance arises where case-sensitivity will matter it will be explained.

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

Edit Oct 9, 2014: Updated formatting and added link to the next article.

MS-DOS (cmd.exe) command prompt: CD, MD, copy

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

Another very common command is cd – Change Directory (also works in Linux). This changes the directory you are currently in.

C:\>cd \users\default

CD command in Windows 8.1
CD command in Windows 8.1

This example might not work for you if you do not have drive C, and in the drive C there are no users\default folder, however the scheme is still the same. You have to type CD followed by the root, the first \, followed by the first directory under the root, users, followed by the next directory, default. You would keep doing this until you got to your last directory.

Another useful command is being able to create directories. Directories are given names just like files are. They can contain numbers and letters but be careful of some of the special characters like * ? / ” ‘, etc. Avoid these. Also, when you make a directory try to avoid adding an extension. Directories with extension is a little bit confusing but fully valid. To make a directory, use the MD command (Make Directory). Use the following syntax:

C:\users\default>md MyDirectory

MD command in Windows 8.1
MD command in Windows 8.1

Have you ever copied and pasted a file from one folder to another? Maybe you just copied and pasted a snippet of text or a picture to another file. Suppose you wanted to copy a file and make a duplicate of that file with a different name. In the next example, we have 1 file called SourceDocument1.doc. The contents of 2nd file will be the same as we are creating a Document2.doc using our existing SourceDocument1.doc file.

C:\>copy SourceDocument1.doc Document2.doc

All that you have to do for the copy command to work is place the original file’s name first. Press the space bar. Then put what you wish to name the new, copied, document. This is a good simple way to make a backup file. There are typically two messages that will come back – ‘1 file(s) copied’ or ‘File not found’. You can always use the DIR command to make sure you have the correct filename (see previous article with DIR command examples). You can experiment with using different drive letters and directories using the same principles we used to create and change directories. To copy a group of files you can use what is called the wildcard. Here is an example:

C:\>copy *.doc D:\

In this example the *.doc matches all the files ending in .doc in current directory, and files are copied to the drive D (flash drive in my case). Be careful with the wildcard when deleting files as it is very easy to delete more than what you want to delete.

You can press the CTRL+C (Control key and the C key), looks like ^C on most documentation, to stop the copy progress or batch file from executing. In case of batch files, you will then be asked if you wish to terminate the batch file. Press a Y for yes or a N (stands for no) to continue on processing the batch script or let the current process to finish. You can also use the CTRL+Break key combination. This combination produces the same output as the CTRL+C combination (except for the Ping command). Exceptions to rules are common, but you will learn then as we go with more details.

Something that you might find interesting later on when you learn about the ECHO command is that when you enter ECHO OFF at the command prompt you will in essence “lose” your prompt. You need to type ECHO ON in order to get back to where you were. Not very useful in command prompt, but very useful when you are writing a batch file (.bat).

Next article tomorrow: What is a batch script?
Previous article: MS-DOS (cmd.exe) prompt basic commands

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

Edit Sep 26, 2014: added link to the next article.