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!

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.

  1. 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)
  2. Use EDIT text editor to create a file that contains your name and address. Save it as ME.TXT.
  3. Create a subfolder BATS.
  4. 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).
  5. Rename the file ME.TXT in the folder SYSTEMS as ME2.TXT (Hint: RENAME).
  6. Move the file ME2.TXT in SYSTEMS to the BATS folder using absolute reference and make sure the file moved (Hint: MOVE.)
  7. 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.

  1. First see what the program MORE is for. You will find the help for the program the same way as for DOS commands.
  2. Pipe the output from the command DIR to the MORE program.
  3. 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.
  4. See what the program SORT does.
  5. Pipe the output from DIR through the SORT program and redirect it to file DIR.TXT.
  6. Use SORT to add an output from DIR command to the end of the DIR.TXT file in descending order.
  7. See what the command XCOPY is for. You will need the command in the exercises later on.
  8. 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:

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.

  1. Go to the folder BATS.
  2. Write a batchfile that prints "Have a nice day" on the screen (Hint: ECHO).
  3. 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)
  4. Write a new batchfile that creates a new subfolder TEMP.
  5. Refine the command string to print the contents of the new folder after creating it.
  6. 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.

  7. Change the batchfile so that the user can give the name of the folder to be created as a parameter.
  8. 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.
  9. Now make the batchfile more user friendly. Thus add information for the user and hide all unnecessary output.
  10. 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).
  11. 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).
  12. 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

  1. 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.
  2. 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).
http:// appro.mit.jyu.fi /2001/syksy/laitteistot/demot/demo1/index.html
© Tommi Lahtonen ()<URL: http://www.iki.fi/hazor/>
29.10.2001 14:34:09