Make your own free website on Tripod.com

MSM1 Lecture #5 - Working with Files in DOS

Materials:
Working complete PC
Blank Diskette
Student Diskette, "New Boot A Ver 2.0+"
Objectives:
The student should become familiar with the DOS file system including:
File attributes,
Creating, Editing files,
CONFIG.SYS and AUTOEXEC.BAT
Competency:
The student will become familiar with file attributes including how to view hidden files as well as be able to change the attributes of a file. The student will be able to create new files and be able to edit existing files including the CONFIG.SYS and AUTOEXEC.BAT.
  1. Boot the PC and view the files in the root directory. The DOS operating system has two user definable boot configuration text files that control how the OS will be configured to function during the current session. The session can be defined as "this boot up" from the time the DOS prompt appears until the PC is rebooted. These two files are the CONFIG.SYS file and the AUTOEXEC.BAT file.

  2. The CONFIG.SYS file is the only file on the drive that has the *.SYS extension that is not a machine language driver for some peripheral component but is instead a text file that the user can modify. This file does however manage all of the other *.SYS files and the way DOS will work during the session. The CONFIG.SYS file is parsed by IO.SYS shortly after this file is loaded into RAM by the OS boot strap loader program that is embedded in the DOS Boot Record sector of the partition. The term parse means a program, in this case IO.SYS, will read the text file, interpret the file, and decide what it will do with the instructions. IO.SYS'es decisions are very limited. This program can reject syntax and references to files that do not exist with errors and it can change the order in which it executes each instruction. This out-of-order execution of the CONFIG.SYS instructions is the reason it is said that IO.SYS parses the CONFIG.SYS file.

  3. Enter this command to display the contents of the CONFIG.SYS file on screen and see that it is in fact a text file:

    C:\>TYPE CONFIG.SYS
    
  4. The TYPE command will display the contents of a plain ASCII encoded text file straight to the screen. If the file is too large, it will scroll off of the screen and TYPE does not unfortunately have a /P switch like DIR does. Switch to the DOS directory and type:

    C:\>TYPE NETWORKS.TXT
    
  5. Here is a file that is much too large to view using the TYPE command. Now hit the [F3] key to repeat the previous command at the command line and then add the following to the end of it:

    C:\>TYPE NETWORKS.TXT | MORE
    
  6. The "|" is the "pipe" symbol and can be typed by pressing the [shift]+[\] keys. This time the file will be passed or piped through the MORE command who will display it one screen at a time and then pause until you hit a key to continue with the next screen.

  7. Unfortunately, all of the instruction syntax of the CONFIG.SYS file is unique and only occurs within this file. It has a language of its own, which is another task that the user must learn. The most common commands and what they do can be found in the DOS Home Page under CONFIG.SYS.

  8. The DEVICE= command is used to load a device driver. Most of these have the *.SYS extension and cannot be executed from the command line. This is because of the way that they must negotiate a cooperative behavior with IO.SYS and essentially “plug in” to IO.SYS and become a part of the DOS kernel. This can only be done during the boot up process because DOS is not a dynamic OS that can reconfigure on the fly. Windows on the other hand has this capability and does dynamically load components which are called *.DLLs or Dynamic Linked Libraries. The full path to the driver file must be specified because the PATH command of the AUTOEXEC.BAT has not been activated yet.

  9. The DOS=HIGH command can be used to move a portion of the DOS kernel into the HMA which is the roughly 64KB block of memory above the 1MB memory addressing limit. This can only be done if HIMEM.SYS has been loaded. IO.SYS will wait if this command precedes the DEVICE= command to load HIMEM.SYS. If HIMEM.SYS never loads DOS will display an error informing the user that HMA not available and DOS will be loaded LOW.

  10. The DOS=UMB command also depends on a memory manager, in this case it is the file EMM386.EXE which can loaded with a DEVICE= command. This allows remapping of memory blocks that reside in the Upper Memory Area (UMBs) above the 640KB DOS addressing limit. This is a design limitation for DOS because this area is populated by the Video RAM as well as BIOS device drivers, so DOS was designed to stay out of the UMA. Under the supervision of HIMEM.SYS, DOS can safely allocate these areas to programs and drivers.

  11. LASTDRIVE= must be used by a DOS system that will need a drive letter beyond the letter F: by a “late bnding” device driver that will establish a new drive letter for a device after IO.SYS has relinquished control to COMMAND.COM. One such driver is MSCDEX.EXE. This driver will detect the existence of a block device in memory established by the CD-ROM low level device driver. Then it will negotiate a drive letter and make the CD-ROM accessible from the command line like another logical drive. DOS only reserves the room in memory for six drive letters A: to F: and B: is almost never used. LASTDRIVE=Z tells DOS to reserve room for all possible drive letters. The Win9x IO.SYS files automatically reserve room for all possible drive letters without being told to do so with this command.

  12. The FILES= command tells the DOS kernel to reserve the memory support to hold as many files open simultaneously for use by programs as the line specifies. This one can be very memory and processor hungry and should be capped at 30 unless a program specifically needs more.

  13. The BUFFERS= command allocates general purpose data transfer areas in memory. These are not tied to any particular file or drive but facilitate data transfers from files and drives to other structures and active programs and drivers.

  14. The STACKS= command sets up DOS stacks which can be best viewed as activity scratch pads or “to do” lists for DOS to remember what processes it is currently involved in such as what program is currently running? What is the current directory? What files are currently open? and so forth. It is reported that the ideal value for this command is: STACKS=9,256 which instructs IO.SYS to create 9 stacks each one being 256 bytes in size. The Win9x IO.SYS does this automatically.

  15. The SWITCHES= command was included to facilitate four basically unrelated settings that can control specific problems on the system. Some early enhanced keyboards did not send the correct signals to the keyboard controller. To fix this the SWITCHES=/K command will cause the low level driver to ignore all extended characters and act as if the keyboard is the old standard 83-key keyboard. The other switches can be looked up of they are needed.

  16. The SHELL= command can be used to point to the command interpreter that IO.SYS should load and pass control to. Under normal circumstances this is always the COMMAND.COM that comes with the version of DOS that is running. Very few alternative or third party COMMAND.COMs were ever developed. But the command can be used in a situation in which COMMAND.COM has been moved out of the root directory.

  17. Once all of the CONFIG.SYS commands have been executed by IO.SYS, the command interpreter, COMMAND.COM, is found and loaded into memory and then control is passed to it. COMMAND.COM will search for the file AUTOEXEC.BAT and if it exists it will execute the instructions inside. COMMAND.COM will not parse a batch file. Instead all instructions will be executed line by line from the top down. Batch files are very close to normal command line syntax in nature except for a few extra commands that only work in a batch file. Here are the BATCH file commands:

    CALL, CHOICE, ECHO, FOR, GOTO, IF, PAUSE, REM, SHIFT
    
  18. A batch file can be used to “call” another one into execution. This can be done two different ways. One way is to simply put the name of the batch file which acts like any executable program on a line of the first batch file by itself. When this is done, COMMAND.COM will locate the other batch file, and execute its lines, but it will not return to the next line of the first batch file and continue execution of it. If this is desired (you want control to return to the first batch file when the other one is done) then use the CALL command. This will cause COMMAND.COM to locate the second batch file and keep the “calling” batch file open and complete the “called” batch file and then return to the “caller” and continue execution of it.

  19. CHOICE is an external command, which means that CHOICE.COM must be present on a diskette if you wish to use it in a batch file on the diskette. CHOICE allows the presentation of a question and the acceptance of a single key response and is used along with ECHO to display a menu and then take the user’s choice then act on that choice.

  20. ECHO has two separate functions. The first is to activate or deactivate batch line display to the screen during execution of the batch file. COMMAND.COM defaults to displaying each line of a batch file as it executes it which can result in ugly displays to the screen. @ECHO OFF is commonly seen at the top of batch files and is the syntax to turn displaying of the command to the screen off. The @ symbol at the beginning of any batch file command causes COMMAND.COM to suppress the display of that line only. Placing it in front of this line causes even this line to not be displayed as it is executed.

  21. The other function of ECHO is to display the text that follows it to the screen. In this way the batch file can create custom output for commands and processes that it is undertaking. In this usage the first word that follows it cannot be either ON or OFF since that will be seen as the instruction on controlling command display to the screen.

  22. The FOR command creates a loop that can be quite powerful. The full syntax is: FOR %%variable IN (set) DO command /switches. Here is an example to see how this structure works:

    C:\DOS>FOR %%FILE IN (*.TXT) DO TYPE %FILE | MORE
    
  23. What happens first is that COMMAND.COM recognizes the wildcard filename syntax and locates all files with this extension in the current directory. It then substitutes each file one at a time into the variable named %FILE and then executes the command TYPE %FILE | MORE. The result is that each text file one at a time will be displayed on screen until all of them have been displayed. This is a useful batch file because the TYPE command does not take wildcards so this is the only way to accomplish this. Here is another example that shows the power of FOR. Move to the root, create a new directory called TEXT and change into it and run this command:

    C:\TEXT>FOR %%CMD IN (COPY DEL) DO %CMD C:\DOS\*.TXT
    
  24. In this case, the text string COPY will be substituted into the %CMD command. Then the DO part reads: COPY C:\DOS\*.TXT. As long as the current directory is not C:\DOS, then all of these files will be copied to the current directory. Then the loop substitutes the second string, DEL, into the variable. Now the DO part reads: DEL C:\DOS\*.TXT which will delete all of these files from the DOS directory.

  25. The IF command is used to test a condition and then control execution of the batch file based on the result of the test. There are three possible tests that the IF can perform. The first is a string comparison. Here is an example:

    IF %1z==z THEN GOTO end
    
  26. At this point passed parameters should be explained. Batch files are fully executable as if they were *.EXEs or *.COMs. And they can be passed command line parameters just like other DOS commands and software programs. Let’s write a small batch file:

    REM This batch file will illustrate passed
    REM parameters
    @ECHO OFF
    IF %1z==z GOTO error1
    IF %2z==z GOTO error1
    IF %1==%2 GOTO equal
    ECHO %1 is not equal to %2.
    GOTO end
    :error1
    ECHO Correct Syntax is:
    ECHO.
    ECHO COMPARE STRING1 STRING2
    GOTO end
    :equal
    ECHO %1 is equal to %2.
    :end
    
  27. To create the above file type EDIT COMPARE.BAT then hit [enter]. The DOS full screen text file editing utility will open. When EDIT opens it offers you some help, hit the [Esc] key. Type in the entire batch file above, hit [enter] at the end of each line of text. When done do the following: Press and hold the [Alt] key, then hit the [F] once. This opens the File menu. Now release the [Alt] key and press the [S] key. This saves the text that has been typed onto the screen into the COMPARE.BAT file. With the file now saved, exit EDIT by opening the File menu , [Alt]+[F], then hit [X].

  28. If you are not already in the root move to there and type COMPARE at the prompt and hit [enter]. You should see this on screen:

    C:\TEXT>compare
    Correct Syntax is:
    COMPARE STRING1 STRING2
    C:\>_
    
  29. The first two if statements test the passed parameters to see if they are empty. If so then the %1 is substituted with nothing and the IF reads: IF z==z goto error1. Since z does equal z then the batch file will jumpt to the error1 label and display the help syntax then another GOTO skips the rest of the batch file and sends control to the :end label where the batch file ends.

  30. By the way another special syntax of the ECHO command is ECHO. which prints a blank line. Now type this at the command line:

    C:\TEXT>compare this that
    this is not equal to that.
    C:\>_
    
  31. So you can pass parameters to your batch file and it can use them internally. The name of the batch file itself becomes the variable %0 within the batch file and the string of characters that follows the first space up to the next space will be %1 inside the batch file. The next group of letters between spaces will become %2, and so forth up to %9. So when you type two words separated by spaces after the batch file’s name it will compare them for equality. If they are equal it will announce it and display them within the proclamation. If they are unequal it will display them within that proclamation. While COMPARE.BAT does not accomplish much it can be seen that the batch file can function like a simple program and branch according to outside input and situations.

  32. The AUTOEXEC.BAT file is a special batch file. COMMAND.COM will search the root directory of the boot drive immediatey upon gaining control of the system from IO.SYS at boot up for a file by this name. If COMMAND.COM finds this file it will immediately execute it. Hence the name meaning AUTOmatically EXECuted BATch file. This file can also be used to adjust the configuration of DOS for the current session. Here are some common commands that are used in the AUTOEXEC.BAT:

    LH, PATH, PROMPT, MSCDEX, SMARTDRV
    
  33. It should be noted that only one program can occupy the HMA during the session and the HMA is 64KB in size. If a program is too large then it will not go into the HMA and you will not see any message on screen. LH will load programs and drivers into UMBs when they have been created, but the only way to check if this is actually working is with the MEM command or even better the MSD command.

  34. The PATH command is covered in module 3.

  35. The prompt command can be used to change the nature of the command prompt itself. The standard prompt has the appearance: C:\> but this is achieved with the prompt command PROMPT $P$G The prompt can easily be changed. Try the following:

    C:\>prompt C:\$g
    C:\>_
    
  36. But the prompt has indeed changed, you just don’t know it yet. Change to the DOS directory:

    C:\>cd dos
    C:\>_
    
  37. But wait! The prompt should now be "C:\DOS>" why didn't you move into the directory? Oh, you did move there, the DOS prompt is no longer set to show you the current path with the $P variable, it is now set to show you C:\> no matter where you go. Insert a floppy disk in drive A: and change to the floppy:

    C:\>A:
    C:\>_
    
  38. And so the prompt will remain until you fix it:

    C:\>prompt $p$g
    A:\>_
    
  39. MSCDEX is commonly run from the AUTOEXEC.BAT file to establish a drive letter and therefore access to a CD-ROM drive. Since optical drives entered into the scene years after DOS kernel was well established it was decided never to actually bloat the DOS kernel with additional support and size by including this program code. MSCDEX works just fine and communicates with any ATAPI capable driver (which they all are) and communicates well with IO.SYS to build the new drive letter for the CD-ROM drive. MSCDEX will not work if a hardware level device driver for the optical drive has not already been loaded by the CONFIG.SYS file. This operation will be covered in a later module.

  40. SMARTDRV can be executed from the AUTOEXEC.BAT in order to launch drive read/write caching. Since the drives including the hard drive are all very slow devices compared to the rest of the system, it is possible if HIMEM.SYS has been loaded to launch a program that will read ahead of the user or a program that is making hard drive reads and store the read ahead in RAM. Whe nthe user or the program actually needs the information that was read ahead it can be delivered straight from high speed RAM. Similarly if the user or a program is making numerous small writes to the disks the program can collect them in RAM and then write them all out when it gets a chance while the system is not busy. The program that does this is SMARTDRV.

  41. 36. There are two more ways to create a text file besides EDIT. Remember that EDIT is an external command and may not always be around. But as long as there is a command prompt, COMMAND.COM is around and COMMAND.COM has two internal commands that can be used to create small text files. The first one is the COPY command. DOS refers to the keyboard and the monitor by the file name CON and you can copy the CON file if you remember one important safety tip:

    C:\>COPY CON NEWFILE.TXT
    _
    
  42. That when you execute the command the machine did not crash, it is awaiting the CON file, which is the keyboard:

    C:\>COPY CON NEWFILE.TXT
    This is a few
    lines of
    text for the new file.
    _
    
  43. Splendid, but how do you get it to stop? You must manually feed the COPY command the End-Of-File marker. Press the [F6] function key and this ^Z will appear, then hit [enter]:

    C:\>COPY CON NEWFILE.TXT
    This is a few
    lines of
    text for the new file.
    ^Z
        1 file(s) copied
    C:\>_
    
  44. You cannot type in the characters ^ and Z and hope to get out of COPY CON hell. You have been warned. It is a dark and lonely place. No prompt, no "Bad Command or File Name" nagging either, just the black screen, blinking cursor, and you...

  45. The other internal command way of getting COMMAND.COM to create a small text file is with the ECHO command seen earlier in use sending output to the screen from within batch files. Poor ECHO was never intended to be so abused as will be done in this command:

    C:\>ECHO This is a new file too! > newfile2.txt
    C:\>_
    
  46. Notice that nothing appeared on screen. The greater than symbol in the command is very significant and powerful and is called a redirector. The redirectors will be covered in depth in a later module. In the meantime remember that al text between the ECHO command itself and the “>” will be placed into a new file with the name specified behind the “>” symbol. This can only create a one line text file since you can only type one line at the prompt.

  47. Also note that the CONFIG.SYS and AUTOEXEC.BAT files must be located in the root of the boot drive and cannot be located anywhere else. If they are moved out of the root they will no longer be read and their commands will be ignored. This is a way to bypass either one if they are causing problems, but it is better to rename the file like: REN CONFIG.SYS CONFIG.OLD, and then reboot.

  48. Name the CONFIG.SYS command that loads device drivers at startup. Give an example that will load the memory manager HIMEM.SYS, if it is located in the DOS directory and not the root:


  49. Name two CONFIG.SYS commands that control the amount of memory blocks that will be devoted to data transfers:


  50. Name the command that will allow all possible drive letters to be allocated by device drivers:


  51. Give the command that will allocate 10 memory storage locations of 512 bytes each that DOS will use to keep track of the system:


  52. Write a batch file named HELLO.BAT that will suppress output of the lines themselves to the screen and display "Hello World!":




  53. Write another batch file named BETTER.BAT that will display "I am better than Hello World", then on the next line it will run HELLO.BAT, then on the third line it will display "See I told you.":






  54. Modify the line that runs HELLO.BAT so that when HELLO.BAT is complete, control does NOT return to BETTER.BAT:






  55. Write an AUTOEXEC.BAT that will set the PATH to the root of the floppy and the directory DOS on the floppy. It will set the PROMPT equal to the current Time followed by a greater than symbol and launch SMARTDRV. Format a floppy, copy smartdrv.exe onto it and test the floppy.

  56. Fix the error by creating a CONFIG.SYS that will load the memory manager and copy the memory manager file to the DOS directory of the floppy. Create this CONFIG.SYS using the COPY CON command.

  57. Use the ECHO command to create the three text files named AUTOEXEC.0, AUTOEXEC.1 and AUTOEXEC.2. Create each one using ECHO and containing one line of your AUTOEXEC.BAT. When completed, use the COPY command to stitch them together into one file named AUTOEXEC.NEW.

  58. Rename the AUTOEXEC.BAT on the floppy to AUTOEXEC.OLD and copy the AUTOEXEC.NEW onto the floppy and name it AUTOEXEC.BAT. Test the floppy by booting to it, does it work?

  59. Create two batch files named 1.BAT and 2.BAT. Be sure that both suppress displaying lines to the screen. 1.BAT should display “I am ONE, hit [2] to see TWO” be sure to allow the [1] key to quit the batch file and return to the prompt. 2.BAT should be executed by 1.BAT and display the message “I am TWO, hit [1] to see ONE” This one should use [2] as the “escape” key to return to the prompt.

  60. Can you make several such batch files and allow the user to select from a menu which one they want to see next?

    Write the batch file command that will test for the existence of the file COMMAND.COM:


    Write the batch file command that will test if the first command line parameter is equal to "C" and if so jump execution to the label :cwarn






Copyright©2000-2004 Brian Robinson ALL RIGHTS RESERVED