| Appendix C: The Command Prompt | ||
| Previous Appendix B: Using Archives |
Home Appendices |
|
Back in the days before Windows, everything was typed in to the computer. Only certain commands are accepted, while just the slightest typo would cause the whole command to work incorrectly. Regardless of these shortcomings, learning how to use a command-prompt system is a powerful skill. The power of the prompt is addicting.
The most basic kind of operating system is a command-line system. A command line system requires only two pieces of hardware to be attached to the computer core: a keyboard and a monitor will get you by. This duo is called a console. In a command line system, you type special commands into the prompt, which sends them to software called the shell for interpretation. Then you get your desired results - if the command is correct.
The command prompt was used exclusively before Windows. Microsoft's command-line system is called MS-DOS, often simply called DOS. Microsoft relized that many new users didn't have the time or patience to learn all of those commands, so they wrote Windows. As you learned in Section 1-2, Windows has a graphical user interface. By comparison, DOS has no GUI - all commands are text-based.
Calling up a command prompt is simple. On Windows 9x systems, go to Start > Programs > MS-DOS Prompt. In Windows 2000 and XP, you will go to Start > All Programs > Accessories > Command Prompt. 2000/XP users can also type cmd into the Run dialog (Start > Run...).
Windows was designed to be an extension of DOS. It was pretty much a simple frontend to DOS. Windows wasn't really an operating system: you were really using DOS, which was being controlled by you through Windows. Windows 2000 and XP are different though: they are true operating systems, not simply an extension of DOS.
So what's this mean? On 9x systems you can step out of Windows for a while and experience a "true" command prompt, not just an emulator like that in 2k/XP. To do this, you go to Start > Shut Down... and select the "Restart in MS-DOS mode" option. To return to Windows, just type exit and Windows will reappear.

Figure C.1: The shutdown dialog in Windows 9x
When you start a command prompt, you will get something which looks like this:
C:\>
This is called the prompt and is where you type commands. The first part of the prompt, (C:\ in this example) lists the working directory, which is where you "are" in the system at the time. This is the directory which your commands will apply to - in this case it is the root folder of C:.
You can change the working directory by use of the cd command. (cd stands for "change directory". You follow it with a space and the name of the directory you want to switch to. (The extra information after the command is called a parameter.) For example, if your working directory was C:\ and you wanted to go to C:\WINDOWS you could type cd windows (case is not important). Pressing ENTER will execute the command.
Again, you can change directories. Say you wanted to go to C:\WINDOWS\system32. You could do that, by first doing cd windows and then cd system32. You could also do it in one command: cd windows\system32.
There are three special "directories" that you can cd to, to help you save time. The first one is ., which refers to the current working directory. One is .. (two periods), which refers to the parent of the current working directory. For instance, if you are in C:\WINDOWS\SYSTEM32, then cd .. would put you in the C:\WINDOWS directory. The other special directory you can refer to is \ (backslash), which refers to the root directory of the drive that you're on.
You do not use cd to change drives. You simply type the name of the drive (e.g. A:) and that will be enough to change to the root directory of whatever drive you wish.
You can now go to any directory on any drive of the computer. But that's not a lot of good if you don't know what files are in your working directory. That's what the dir command is for. Just type it, and you'll get a directory listing looking something like this:
Volume in drive C has no label Volume Serial Number is 1472-11E5 Directory of C:\Browser . <DIR> 02-09-00 10:06p . .. <DIR> 02-09-00 10:06p . INSTALL LOG 3,819 03-27-01 9:05p INSTALL.LOG UNWISE EXE 82,864 07-03-97 9:44a UNWISE.EXE
The first two entries are the . and .. directories described above. Below this are the files which are in this folder. These are INSTALL.LOG and UNWISE.EXE.
For very full directories, the listing will go on and on and push the top of the list off the top of the screen. You can modify how the dir command acts, however, by adding a forward slash and a letter. This combo is called a switch. In this case, you can do dir/w, which turns on the "w" (wide) switch, producing this output:
Volume in drive C has no label Volume Serial Number is 1472-11E5 Directory of C:\Browser install.log unwise.exe
Multiple switches cannot be ganged together without using more forward slashes. To use both the /a and /b switches on the command example, for instance, you would use example/a/b, not example/ab.
To copy a file from one location to another, use the copy command. The command takes two parameters. The first is the source file, where you're copying from. The second parameter is the destination file, or where you're copying to. For instance, say you were copying file1.txt to file2.txt. The command would be copy file1.txt file2.txt. In this case file1.txt and file2.txt are both in the working directory - you can also specify a full file path for one of the parameters, e.g. copy file1.txt C:\Folder2\file2.txt.
NOTE
If the file names you're inputting contain spaces, you must enclose them in quotes. This prevents the command prompt as interpreting the file name as two different parameters.
copy "file name.txt" file2.txt
Files can be moved similar to copying them by using the move command. Again, it takes two parameters, the source file and the destination.
Files can be renamed with the ren command. Once again, two parameters, source and destination. Example: ren file1.txt file2.txt
NOTE
You can also use the move command to rename files.
move file1.txt ./file2.txt
Files can be quickly deleted with the del command. It only takes one parameter, the name of the file(s) to be deleted. Example: del file1.txt
CAUTION
Files deleted from the command prompt are not moved to the Recycle Bin, but permanently deleted! Also, the command prompt will not ask for confirmation before deleting the file!
Text files can be edited from the command prompt with two commands. The first, edlin, is derived from the UNIX editor ed. It is very minimalistic and has its own set of commands to memorize. The other, more user-friendly editor is MS-DOS Editor, invoked with the edit command.
NOTE
Optionally, you can give edit the name of a file (as a parameter) and it will start with the file already opened.
edit provides a fake GUI that you should be immediately comfortable with, shown in Figure B.1. Depending on your Windows version and the prompt you run it from, mouse support may be enabled. If not, you may access the menus by pressing Alt and using the arrow keys.

Figure C.2: MS-DOS Editor
MS-DOS Editor is similar to Notepad and suited for the same types of tasks, editing plain-text files. (It is not a word processor like Microsoft Word.) It should be simple for you to learn how use it, especially if you are familiar with Notepad.
Directories can be created and destroyed from the command line as well. To create a new folder, use the command mkdir, which takes one parameter, the name of the directory to be created.
To delete an existing folder, use the rmdir (ReMove DIRectory) command. It takes one parameter as well, that of the directory to be deleted. Example: rmdir Folder3 To protect your data, rmdir will not allow you to delete a non-empty directory. You must first cd into the directory and delete everything using del.
Directories are copied, moved, and renamed using copy, move and ren as if they were files.
After executing many commands (especially dir commands) the screen can become extremely messy, filled up with the output from these older commands. Often this history can become distracting. To clear the screen, execute the cls command. This will remove everything from the command prompt and leave you with a fresh black screen with a brand new prompt at the top of the screen.
Programs can be executed simply changing to their directory and typing the name of the program. (The *.exe extension is not required.) The program name can optionally be preceded by call (e.g. call pomona), but this is typically only used in batch files (see below).
Programs in the C:\WINDOWS directory can be run from anywhere without needing to specify the file path. Try typing sol in as a command and see what happens!
The command prompt allows a limited scripting language in the form of batch files, which feed a sequence (batch) of commands into DOS and execute them one after another. A simple batch file can be created in a text editor (edit is often used) and saved with a *.bat extension. A batch file is simply a list of commands to be executed, separated with one command on each line. Consider this batch file:
C: cd WINDOWS cd SYSTEM32 dir/w pause cd\ cls
This does precisely the same thing as if you had typed the commands into the prompt yourself. You should recognize all of the commands except pause, a special command used only in batch scripting to make the script wait for the user to continue it. Try copying and pasting this into an editor, saving it with a *.bat extension, and running it.
Because the command prompt can do things like delete and move files, be careful writing batch files and extremely cautious running those given to you by others. Always open up a batch file and look over it before running it.
A fuller treatment of batch scripting is beyond the scope of this book; however, if you're interested in batch files, Computer Hope has a more in-depth reference for batch scripting. You can also Google for batch files and find a wealth of useful information. Batch scripting is another great way to get started programming, if you're curious.
A small, biased side-note: Batch scripting is nice and useful, but in my opinion the Windows command prompt is still very weak. If you like the idea of batch files but would like a more powerful shell to script, try getting a copy of the Linux operating system or download the Cygwin UNIX emulation software. These include a copy of the Linux/UNIX command prompt, BASH, which is much more powerful than the Windows command prompt.
With the advent of the GUI, many users will not need to ever step into the dark and type their commands to the console ever again. However, knowing your way around the command prompt is an extremely valuable skill. For instance, should your Windows installation ever become damaged and unable to boot, many recovery tools simply start up and give you a shell with no GUI. Knowing how to copy your files back and forth from disks can allow you the opportunity to recover months of hard work, or you may be able to repair your system by editing configuration files with edit.
Of course, this doesn't happen often. But being able to run things from the prompt - and automate them with batch files - is a convenience that you shouldn't deny yourself. You'll only get the hang of the command prompt if you use it often. Keep at it!
| Previous Appendix B: Using Archives |
Home Appendices |
|