Exercise 1 - Batchfiles
In the following you will rehearse DOS commands and DOS batchfiles. If you are familiar with
DOS commands you can skip that part but read the exercises anyway to refresh your memory.
You can do all the exercises by using the Command Prompt or MS-DOS Prompt of Windows 2000,
Windows NT or Windows 95. Doing the exercises may differ a bit between the different operating systems
but fundamentally it is the same.
You can and you have to use the command reference of Windows as help for the exercises.
It includes the syntaxes of the commands and some examples. It's wise to learn how to use the command reference!
- Seleting Start | Help and writing the search term Command Reference
to the Index tab shows the command reference in Windows 2000
- In Windows NT you'll find the command reference by writing ntcmds.hlp
(the name of the help file) to the Start | Run window.
- In Windowsin 95 and 98 you have to use the Command Prompt
for help. Help on the commands is shown when you write the question mark as an attribute for the command in question
(doscommand /?).
Do all the following exercises using Command Prompt (Start | Command Prompt).
Rehearsing DOS commands
This is to rehearse briefly the DOS commands reviewed on the Preliminary Course on Information Technology.
You can use leture 3 to refresh your memory.
- Make a directory called systems on the U: drive and go to the directory
(thus set it as a default directory). (Hint:
MKDIR and CD)
- Use EDIT text editor to create a file that contains your name and address. Save it as ME.TXT.
- Create a subfolder BATS.
- Copy the ME.TXT file to
BATS by using relative reference. Go to the BATS directory
and make sure you have the file copied there (Hint: COPY).
- Rename the file ME.TXT in the folder SYSTEMS as
ME2.TXT (Hint: RENAME).
- Move the file ME2.TXT in SYSTEMS to the BATS folder using
absolute reference and make sure the file moved (Hint: MOVE.)
- Delete the files in the BATS folder and move on to the next exercises (Hint: DEL).
Pipes, redirection and some new DOS commands
The exercises below are to introduce some new DOS commands and to learn redirection of the commands and pipes.
- First see what the program MORE is for. You will find the help for the program the same way as for
DOS commands.
- Pipe the output from the command DIR to the MORE program.
- The contens of the directory should be divided to pages so use DIR command for a directory whose
contents are too long to for the screen.
- What attribute of DIR does the same thing?
- What is the difference between the commands?
- Redirect the output from the command DIR to file DIR.TXT.
Check if you can find the output in the DIR.TXT file. Use the EDIT program to view
the file.
- See what the program SORT does.
- Pipe the output from DIR through the SORT
program and redirect it to file DIR.TXT.
- Use SORT to add an output from DIR command to the end
of the DIR.TXT file in descending order.
- See what the command XCOPY is for. You will need the command in the exercises later on.
- What is the command DELTREE (Windows95/98) or RMDIR /S (Windows NT/2000) for?
This command is a bit dangerous but efficient in the right hands.
Elementary batchfiles
Some hints for writing batchfiles:
- A batchfile is a file containing DOS commands.
- For writing batchfiles you an use EDIT editor or NOTEPAD.
- A batchfile is saved with the postfix .BAT.
Another possible postfix is .CMD but is doesn't work on Windows 95 and Windows 98.
- You can run a batchfile by writing the name of the file to Command Prompt when you are in the same directory
as the file. Absolute or relative reference to the file is used from other directories.
- Save the exercise batchfiles you write and name them for example according to the names of the exercises.
- You can use the complete batchfiles as a base when you write new ones.
In the exercises above you used the DOS commands on the command prompt but below you do the
same kind of things by using a batchfile. A very simple example is used as a base. The batchfile you write
is not very useful as such but the point to be taken is how to write batchfiles.
- Go to the folder BATS.
- Write a batchfile that prints "Have a nice day" on the screen (Hint: ECHO).
- Save it and test it.
- Also the command lines are now printed. Change the batchfile so that only "Have a nice day" is printed.
- Then change the command string so that one can give a name to be printed with the greeting as a parameter
(for example "Have a nice day, Pete!"). Thus you call the batchfile, for instance,
like this: hello.bat Pete
(Hint: %1)
- Write a new batchfile that creates a new subfolder TEMP.
- The idea is to use the same command for creatig the folder as in the rehearsal of DOS commands above.
- Save the file and test it.
- Refine the command string to print the contents of the new folder after creating it.
- Then refine it to create the folder, copy a file (you decide which one) from BATS to the new folder and then print the
contents of the new folder.
The batchfile you wrote is not very useful as it only creates folders called TEMP.
So refine the batchfile for more general purpose.
- Change the batchfile so that the user can give the name of the folder to be created as a parameter.
- Also refine the batchfile so that the user can give the name of the file to be copied to the new folder
as a second parameter. The file to be copied is in the default folder.
- Now make the batchfile more user friendly. Thus add information for the user and hide all unnecessary
output.
- The batchfile works incorrectly if the folder given as a parameter already exists. Refine it to create
the folder only if it does not already exist. The batchfile should also inform the user whether the folder
was created or not (Hint: IF and GOTO).
- Further refine the batchfile so that it stops before creating the file and gives the user an opportunity
to cancel the task by pressing CTRL-C (Hint: PAUSE).
- Then one last trick: Make the batchfile to check whether the user has given anything as a first and second parameter.
If there is no second parameter, all the files will be copied to the new folder. If there os no first parameter,
running the batchfile stops.
Additional exercises
- Refine the batchfile you created above to copy the whole directory hierarchy given by the user to the new folder.
You an use the command XCOPY for that.
- Then change the batchfile so that the folder where to copy is given first and then an indiscriminate
number (thus also more than nine) of files to copy as parameters (Hint: SET, SHIFT, IF and GOTO).