Java Programming   Java Programming
 |Sofia Home | Content Gallery |
Home
Syllabus
Schedule
Lessons
Assignments
Resources

Lesson 1.3 Using the MS-DOS "CLI"

Today, most of us communicate with our computers by using a Graphic User Interface (GUI) such as Microsoft Windows, instead of using a Command Line Interface (CLI). As a flegeling programmer, however, you'll find that there are some things that are difficult to do from a GUI.

In this lesson, you'll get a quick introduction to DOS, the CLI that underlies MS Windows. The goal will be to teach you enough to get through the semester, not to make you a master of DOS aracana. 

Starting DOS

DOS commands are typed into a DOS window (also called the MS-DOS Prompt), which you can launch by clicking Start, clicking Programs, and then clicking MS DOS Prompt as shown here:
Selecting the MS-Dos Prompt shortcut from the Start Menu.
If you are using Windows ME, Windows 2000, or XP, the MS-DOS Prompt has been moved to the Accessories menu. In Windows 2000 and XP, the MS DOS prompt has been renamed as the Command Window.

You can change the appearance of the DOS Window in a couple of ways:

  • By pressing ALT-ENTER (holding down the ALT key and then pressing the ENTER key) you can switch between windowed and full-screen modes. I'd suggest you work in a window, rather than full-screen.
  • Clicking on the icon in the upper-left corner of the DOS window gives you access to the system menu. From the system menu, you can open the MS-DOS properties dialog or make the MS-DOS Prompt toolbar visible [in Win9X]. I usually leave the toolbar open and that's what you'll see in most of the screen shots.
  • The MS-DOS Prompt properties dialog allows you to change the number of rows visible on the screen and change the font used for the display. The Windows NT/Windows 2000 Command Window has more options available; you can change the color of the screen and provide a buffer to recall those lines that have already scrolled off the screen.
Back to Top

DOS Directories

In MS-DOS, folders are called directories or subdirectories. A directory can contain two things: 1) files, and 2) other directories (folders or subdirectories). 

If a directory contains a subdirectory, we often call the subdirectory the "child" and the directory that contains it, the "parent". MS-DOS uses a hierarchical file system which means that a child directory cannot contain its parent [either directly or indirectly].

When the DOS window first appears, it shows the name of the current, or working directory (folder). The name of the directory includes the drive letter, followed by a colon, followed by a backslash and a directory name, followed by a prompt symbol (>) like this:

An MS DOS window after it is first opened.

In this example, the current directory is the WINDOWS directory of your C: drive. If you see a longer directory name like:
 
C:\WINDOWS\SYSTEM>

it means that the current directory is the SYSTEM directory, which is a subdirectory of the WINDOWS directory stored on the C: drive. A series of directories like this, joined together using backslash characters, is known as a path. (This is just like the path used in a URL, except that URL paths follow the UNIX convention of a forward slash instead of a backslash.)

Back to Top

Changing Directories

You move from one directory to the other by using the cd command, which can also be written chdir. If your working directory is C:\WINDOWS, you can move to the root [highest-level] directory by typing cd \ like this:

Changing to the root directory using the cd command.

You can type the command in either upper or lower case. When you press ENTER, DOS will carry out the command and give you a new prompt that shows that you have successfully changed to a different working directory.

Here's another example. Suppose you want to move to the C:\WINDOWS\SYSTEM directory. You simply type the full path name of the desired destination:

Changing to the WINDOWS\SYSTEM directory using the CD command.

When you're in a subdirectory, you can move to the directory that holds it (called the parent directory) by using two dots for the directory name like this:

cd ..

For example, if you are in the C:\WINDOWS\SYSTEM directory typing this command will take you to the C:\WINDOWS directory. 

If you want to move to a subdirectory of your current directory (a child directory, in other words), you can just type the directory name, rather than the complete path. If you are in the C:\WINDOWS directory and you want to move to the C:\WINDOWS\SYSTEM directory. You can simply type 

cd SYSTEM

rather than

cd C:\WINDOWS\SYSTEM

In Windows, many folder names, such as "My Documents" and "Program Files" contain spaces, unlike the orignal version of DOS, where file and folder names were limited to eight characters and a three-character extension. To handle a pathname that contains spaces, put quotes around the name, like this:

cd "C:\My Documents\MyWebPages\"
Back to Top

Changing Drives

To change from one disk-drive to another, you don't use the cd command. Instead, you just type the letter of the drive followed by a colon like this:

Changing from the C: drive to the D: drive.

Back to Top

Directory Listings

Once you've navigated to the directory you intend to use, you can use the dir command to see the names of the files (and subdirectories) that it contains. Here is a directory listing from one of the directories on my C: drive.

A DOS directory listing.

The listing shows contents of the current directory. Each file or subdirectory is shown on a separate line. Each entry actually has two names. The first name [the left-hand column] is the short "DOS" file name. This name is created by Windows; for our purposes you can just ignore it. The far right column contains the file's "real" name. Note that the real name can contain both upper and lowercase characters, and does not have a limit to its length.

There are some other parts of the directory listing that you might find of interest:

  • The second column contains the word <DIR> if the entry is a subdirectory, and the size of the file in bytes if the entry is a regular file. 
  • The first two entries, each of which is a subdirectory, have rather peculiar names. You can ignore these entries if you like: They refer to the current directory (.) and its parent directory (..). 
  • The last two lines are a summary of the files in the directory. The first of these shows that 18 files are listed (subdirectories and the two special entries are counted as files) and the files occupy a total of 49,057 bytes. The final line shows that over 1,843 MB of free space is available on the drive.
You can list the contents of a directory other than the current directory by specifying the name of the directory as an argument. For example, the command
 
dir C:\WINDOWS

lists the contents of the C:\WINDOWS directory. If you attempt this, you'll find that there are too many lines to fit in the DOS window, and so the files will scroll off the top of the window. To prevent this, add the /p flag to the end of the dir command:

dir C:\WINDOWS /p

This causes the listing to stop each time the window fills up. You can read the listing and then press Enter to view the next page.

Back to Top

Working with Files and Directories

You can remove unwanted files by using the del command, which can also be written erase. For example, to delete the README.TXT file, just type
 
del README.TXT

When using the command-line, deleted files are not placed in the "Recycle Bin", and there is no friendly prompt asking "Are you sure?": the file is instantly and silently deleted from your drive.

WildCards

Sometimes, you'll want to delete several files at once: all of the .class or .tmp files in a directory, for instance. To do this, the delete command allows you to use the asterisk (*) or the question mark (?) as wildcard characters:
  • The * means match any number (incuding 0) of characters.
  • The ? means match any single character.
You can delete all the files in the current directory with a file extension of .class by typing
 
del *.class

In the same way, you can delete all the files in the current directory by simply typing

del *.*

Obviously, this is very powerful, so be extra-careful when you use wildcards. If you accidentally type del *.java, when you meant to type del *.class, all of your .java files in the current directory will be erased; you will not be able to get them back.

Back to Top

Copying Files

You can make a copy of an existing file by using the copy command. For example, to copy the file index.html to the C:\TEMP directory, just type
 
copy index.html C:\TEMP\

Just like with del, you can use wildcards with the copy command. To copy all the HTML files the current directory to your A: drive, you would type

copy *.html A:\

You can use the copy command to rename or move a file. To rename MyApplet.java to MyFirstApplet.java, you could type the following two commands:

copy MyApplet.java MyFirstApplet.java
del MyApplet.java

This is relatively inefficient, however, since it takes time to copy all the bytes and there must be room on the drive for both the original file and the new file. A better approach is to use the ren command, which can also be written rename. For example, you could accomplish the same thing as the previous example by typing

ren MyApplet.java MyFirstApplet.java

Because the ren command doesn't actually move the bytes of the file, both the original file and the new file must be in the same  directory.

Back to Top

Creating and Removing Directories

Another DOS skill you'll find useful is the ability to create and remove directories. To create a directory, you use the md command, which can also be written mkdir. For example, to create a subdirectory named MyJavaFiles, you type
 
md MyJavaFiles

The directory is created as a subdirectory of the current directory. If you like, you can create the directory as a subdirectory elsewhere by typing the full path name of the directory you want to create. For example, to create a directory named MyJavaFiles that is a subdirectory of C:\My Documents, you can type

md "C:\My Documents\MyJavaFiles"

Notice that we've used quotes around the directory name because the parent directory, (My Documents) has a space in it. In Windows 95 and 98, the parent directory must already exist. Windows NT and 2K will create the necessary parent directories.

To delete a directory, you use the rd command, which can also be written rmdir. The directory must be empty; that is, it must not contain any files or subdirectories. It must also not be the current working directory of any DOS window or running program. To delete a directory named C:\MUD, type

rd C:\MUD

The directory is instantly and silently deleted. If you're in a hurry, you can delete a directory and all the files and subdirectories it contains by using the /s flag. Typing

rd C:\MUD /s

will delete the C:\MUD directory and all it contains. But, DOS will politely ask you if you're sure before completing the irreversible deletion.

Back to Top

Running Programs

You can run programs from the command line by simply typing the name of the program you want to run. To run the MS-DOS Edit program, for instance, simply type edit at the command prompt, and then press ENTER like this
 
C:\My Documents\MyWebPages\> edit

When the MS-DOS Edit program starts, it "takes over" the MS-DOS Prompt window like this:

Running the DOS EDIT program from the command line.
This is true for all DOS programs. If you want to issue another DOS command while an MS-DOS program is running, you'll have to quit the MS-DOS program, or launch another MS-DOS window from the Start button.

Running Windows Programs

You can launch a Windows GUI program from a DOS window using exactly the same method. To start the Windows Notepad program, for instance, you can simply type notepad at the command prompt and then press ENTER like this:
 
C:\My Documents\MyWebPages\> notepad

When you start a GUI program from the command line, it doesn't replace the MS-DOS Prompt like console mode programs do. Instead, it starts up its own Window so that you can continue to type additional commands in the MS-DOS window like this:

Running NotePad from the MS-DOS command line.

The PATH

Suppose you have several programs named EDIT on your hard disk. How does DOS decide which one to launch? Easy, DOS searches through the directories on your hard disk and the first program matching the name you supplied is the one that runs.

To start its search, MS-DOS looks first in the current or working directory. If it finds a file of the name you supplied along with the .EXE, .COM, .BAT, or .CMD extension, then that program is launched. If it cannot find a match in your current directory, then it starts looking through the directories listed in the MS-DOS PATH .

To see the list of directories that DOS will search on your machine, simply type PATH at the DOS Prompt, followed by ENTER. Here's what you'd find on one of my machines:

Displaying the MS-DOS PATH variable.

When I type notepad at the command prompt, DOS starts looking for a matching file in the current directory. If a match is not found, it then searches the C:\WINDOWS, C:\WINDOWS\COMMAND, and C:\IBMTOOLS directories to find its file. If it cannot find a match, it will then issue an error message like this (issued when I asked it to launch the non-existant program notepax) :

Launching a non-existent file from the DOS command prompt.

The MS-DOS PATH is just one of several environmental variables that are available to programs. Most GUI programs make use of the Windows Registry to store their settings, but console-mode programs still often use the environment. You can see all of these variables by typing set (then ENTER) from the command prompt. Here's what that looks like on my machine:

Displaying DOS environmental variables.

You may be wondering why it is important to learn about the MS-DOS environment if most Windows programs now use the Registry. There are two reasons:
  1. When you deploy your Web applications you are likely to be using a UNIX or Linux server which uses a similar scheme of environmental variables.
  2. When you install the Java Development Kit, you will need to write a command script [commonly called a "batch file"] to change the MS-DOS path so it can find the JDK Java Compiler.
Back to Top

Passing Arguments

Before we move on from the command-line, there is one last wrinkle you should be comfortable with: using command-line arguments when you start a program. 

When you start programs like MS-DOS Edit or Notepad, you don't need to supply any additional information, because each of those programs has a menu system that lets you open and close the files you want to work with. When working with the tools in the Java Development Kit, however, you have to tell the compiler and interpreter which files to operate on; these are passed as command-line arguments.

To pass an argument to a program, simply type the argument after the name of the program, but before you press the ENTER key. To start the Notepad program and have it open the file index.html, you would type:

C:\My Documents\MyWebPages\> notepad index.html

When you use command-line arguments, the arguments have to be in the exact form that the program expects. Notepad, for instance, only edits one file at a time, so of you pass two arguments on the command-line, it doesn't work; Notepad treats the additional argument as part of the file name it looks for like this:

Passing two arguments to Notepad when opening a file.

Back to Top

Something to Talk About

Here's a little something you can do to make sure you've understood the material in this lesson. Feel free to discuss these questions in the discussion area.

In Lesson 1.2 you downloaded and installed the SciTE text editor and created a directory to hold your course pages [probably using Windows Explorer]. Open an MS-DOS command window and try the following exercises. What commands do you need to use?

  1. Switch to the directory where your course files are located. [I'll call this your home directory.]
  2. Create a subdirectory named junk inside your home directory.
  3. Copy only the files that start with h and end with html into the junk directory.
  4. Launch the SciTE text editor from the command-line, using the argument index.html. (If you don't have an index.html file, this should create it; if you do, it should open it.)
  5. Close SciTE and change into the junk directory.
  6. Launch SciTE from the command-line using the argument *.html . What happens? If it doesn't work, can you fix it?
  7. Close SciTE and change back to your home directory using "dot" notation.
  8. Remove the junk subdirectory. Did you have any problems doing this?

Please continue to the next section of this lesson.

 

Back to Top

 

Content Developed by Stephen Gilbert, Licensed under a Creative Commons License
Published by the Sofia Open Content Initiative
© 2004 Foothill-De Anza Community College District &The William and Flora Hewlett Foundation