Lab 5 - Batch Files
Lab 5 - Batch Files
Ref: www.chebucto.ns.ca/~ak621/DOS/BatBasic.html
Batch files are files that contain a list of commands. It executes the commands within the file
when you execute the batch file. The commands execute one by one. A command within a
batch file can be used to start/run another application, for example you could start MS Word.
A batch file could be used to run frequently run commands, deleting a series of files, moving
files, copying files etc. A simple batch file does not require any special programming skills
and can be done by users who only know DOS commands.
An example of a well known batch file is the autoexec.bat, which is a simple batch file
loaded each time the computer is loaded on MS-DOS and early Windows computers. This
batch file contained all the necessary commands and programs used to run MS-DOS and
Windows each time the computer booted.
Use your VMware accounts to do this lab sheet because you will need to save your work.
Important:
You should practice the DOS commands at the command line to ensure
you know how they work before putting them into the batch file.
1
Exercise 1 – Create your first batch file
1. Open an MS-DOS command window.
2. Change to the root directory of the C drive and create a directory called BatchFiles.
You can use this directory to save your batch files.
3. Change into Batchfiles folder. Create a new file called Ex1.bat using notepad.
4. To do this type notepad ex1.bat at the command line.
5. Type in the following lines into the file and Save. Each of the lines below is a
command.
cls
ver
pause
dir c:\windows
6. Before continuing, can you remember what the effect of each of the 4 commands that
you have typed into the file? If not – look them up in the help.
7. Make sure you are in the Batchfiles folder.
8. Key in DIR and MAKE SURE you see a file called EX1.BAT
9. At the prompt, type in the name of the batch file i.e. Ex1 and press Enter.
10. Understand the outcome.
10. Put the command @ECHO OFF as the first line into Ex1.bat.
Run the batch file again. What is effect of this statement?
...................................................................................................................................
..................................................................................................................................
Important:
C:\
ACCOUNTS COLLEGE
Check either in Explorer or at the prompt that the directory structure shown above has been
created.
2
Add comments to all your batch files using Rem command at start of the line. The comments
should indicate the following:
- Program purpose
- Author
- Date written
Run the batch file EX2.BAT. Are you now in a different folder to the one you were in before
you ran the batch file. You should not move directory as a result of running a batch file
unless that is part of the batch file.
a) Files with an extension of COM and have 4 letters in the filename. e.g., chcp.com
mode.com etc.
Make sure the output does not scroll up the screen too quickly.
Put a pause command in between parts a) and b)
3
Batch commands
Just like all MS-DOS commands, all batch file commands are not case sensitive. However, in
the below listing we have listed all commands in all caps to help you identify what is a
command and what is not.
@ Does not echo back the text after the @ symbol. This is most commonly used as @ECHO
OFF to prevent any of the commands in the batch file from being displayed, just the
information needed.
%1 The percent followed by a numeric value, beginning with one, allows users to add
variables within a batch file. Example of what can be used in a batch file.
echo Hello %1
When the above one-line batch file is created, add your name after the batch file. For
example, typing myname (being the name of the bat file) and then your name:
myname bob
Note if you have not typed @ECHO OFF at the beginning of the file this will also print
"ECHO Hello World" and
"Hello World".
4
SHIFT Changes the position of replaceable parameters in a batch program. .
Write a batch program called EX7.BAT. It should carry out the following tasks:
Copy all files with an extension of ps1 from Drive C to Drive E. (Use either the copy
command or the xcopy command. The xcopy command is the better one to use here.) For
example files like profile.ps1 or types .ps1
Before the files are copied across use the echo command to tell the user you are doing so.
Modifications
Add the pause command to your batch file. This line should be inserted before the copy is
carried out. What effect does this have on the program?
ACCOUNTS COLLEGE
Sample.txt
report.doc
5
Check either in Explorer or at the prompt that the directory structure shown above has been
created.
Create the three files shown in the diagram above using the …………………. command.
b. There are two files Example.txt and Sample.txt in the directory Accounts. Write the DOS
command(s) to copy the two files to the directory called Java.
c. Delete the file Example.txt from the Accounts folder. Prompt the user to confirm before
deleting the file.
d. Write the DOS command to move the file Report.doc to the folder Apps and call it a
different name ReportOld.doc.
Ensure the screen is cleared and that there is a blank line under each line of output.
6
Exercise 12 – Using the call command
Write a batch program called EX12a.BAT that contains the following statements:
cd..
dir
Save this program.
What will be the effect of these commands?.........................................................................
What directory are you in now ? ................... How did you get there?.......................
.....................................................................................................................................
Write a batch file called Ex13.Bat which accepts one parameter; the parameter is a persons name. The
program will then display the word “Hello” followed by the person’s name.
For example you should be able to run the program as follows from the DOS prompt:
EX13 David
Exercise 14a Add error checking. Check whether the user has supplied the parameter. It
should print an error message, if it has not been supplied.
7
@echo off
If "%1" == "" GOTO No-Directory
Echo.
Echo Directory Specified
Echo.
GOTO End
:No-Directory
Echo.
Echo No Directory Specified
Echo.
:End
Use this with your code from Exercise 14. If parameter is specified, it will create a directory
with that name.
For example you should be able to run the program as follows from the DOS prompt:
EX15 Reports IBM
For example you should be able to run the program as follows from the DOS prompt:
EX16 IBM report.txt
8
PATH
What error checks will be included? in MS-DOS
.........................................................................
Specifies directories where executable programs are located. Path specifies the search path.
Use the PATH command to tell MS-DOS which directories to look into when an executable
file is not in the current directory. When you enter a line of text at the MS-DOS prompt that is
neither a recognized command nor an executable filename in the current directory, MS-DOS
will search through the directories in your PATH for a filename that matches the text you
entered. Many packages, such as Microsoft Word, automatically add or modify the PATH line
during the installation process. Each path cannot exceed 127 characters, and may be further
limited by your MS-DOS environment space.
BE CAREFUL when you attempt to change the path that you do it correctly.
%PATH% represents the existing path. This example would add C:\BLASTER to the path.
In the System Properties, Advanced tab; click the Environment Variables button.
Highlight the Path variable in the Systems Variable section(lower section) and click Edit.
Each different directory is separated with a semicolon as shown below. You can add or
modify the path lines with the paths you wish the computer to access.
C:\Program Files;C:\Windows;C:\Windows\System32
WARNING:
Be careful when changing the paths on the computer system
9
Exercise 17 – Paths
Add the directory to the path to allow your batch files to be run from any directory at
the command prompt.
Change to a different directory to the one which contains your batch files.
Now try to run the batch file.
Were you able to run the batch file from a different directory to the one the batch file
is stored in? .........................................
d. The batchfile should also have a comment line which contains your name on
the top of the program.
Q2. Write a batch file called Q2.bat that reads in a directory name and creates that
directory on the C:\ drive. If no directory name is specified an error message is
displayed to the user indicating so.
Q3. Write a batch program called Q3.bat that accepts one parameter which
contains the name of a directory.
a. then create a directory in the letters folder which has the same name as
the parameter supplied.
b. Write the DOS command to copy the file setupact.log from the
c:\accounts directory to this new directory and give the file a new name
called setupact.tmp
……………………………………………………………………………………………………….
b. Write the DOS command to add the path to your batch files to the existing path.
……………………………………………………………………………………………………….
10