Thursday, 17 January 2008

MS-DOS Tutorial - Mastering The Command Prompt

WWW.BEST-OF-COMPUTING.COM - Modified By Easy2Learn

You might argue that there is no longer a need to know anything about the DOS Command Prompt, but armed with this knowledge you should be able to understand your system as a whole much better and be able to explore more possibilities in your computing endeavors. If you follow through, understand and practice all the tutorials that I plan to list on this site, you will soon become an expert at computing. You never know when this knowledge might come in handy and you will also be able to help your friends and colleagues.

Ok, enough of why you need to have some DOS knowledge, lets get started. I will give you all the important commands that will be all you basically need to help you in any DOS "situation" that you may encounter.

Start a DOS session by clicking on Start, Point to All Programs, Accessories and then click on Command Prompt. In the "Ultra-Fast Program Access Tutorial" you can learn how to set up your system to instantly activate the Command Prompt, just press "Ctrl" and "Alt" and while still holding them down press "D".

A DOS Window should be now open on your desktop. Well now for the first trick, to make the DOS Window display in full-screen hold down "Alt" and press "Enter" on the keyboard. To get it back to its previous state just repeat the operation again by holding down "Alt" and pressing "Enter" again. To switch back and forth between this tutorial and DOS it is best to keep the "Command Prompt" DOS session in a Window state instead of full-screen.

If you are working with Windows XP the Command Prompt should look something like the following where "UserName" will be your User Name:

C:\Documents and Settings\UserName>_

I should just mention that in the old days they used the word "Directories" for what we now call "Folders". For the purpose of this tutorial I will use the old naming "Directories" instead of "Folders".

Just to make sure we are all on the same page, when I talk about "Path" it mean the path to the current directory. In the case of

C:\Documents and Settings\UserName>
the path indicates the drive which is "C:" (C Drive), the Directory is "Documents and Settings" and the Subdirectory is "UserName". On your system substitute "UserName" for what ever it may be on your system.

Another use for "PATH" is the "PATH" environment variable on your machine. This can be set using the "PATH" command or "PATH" can be used to display the current settings for "PATH" on your system. But for now when I talk about "Path" it is just the "route" to a specific directory or subdirectory I'm talking about and not the "PATH" environment variable or command. When I actually get to the "PATH" environment variable or command I will make sure to inform you about it.

Moving Between Directories

To maneuver your way around Directories use the CD (think of Change Directory to easily remember it) command. The CD.. command is used to back your way down a certain path. Now click on "Command Prompt" on the Windows Taskbar to display the Command Prompt which should currently display

C:\Documents and Settings\UserName>
Type CD.. and press "Enter". Now the displayed path should be
C:\Documents and Settings>
Type CD.. again which should take you right to the root of the drive and it should display like follow:
C:\>
To work your way up the path again to the same position you started of, use the CD command as follow: CD Documents and Settings which will land you in
C:\Documents and Settings>
Then type CD UserName (Remember to substitute UserName for what it is on your system) which will land you in
C:\Documents and Settings\UserName>

To go right down to the root of the drive from the current subdirectory use the CD command as follow :
CD\
If you used above CD\ command you should now be in the root of C: (C Drive). Just one last thing about the CD command, when you work your way up a path using the CD command you do not have to take it one directory at a time. I may be done as follow :
CD Documents and Settings\UserName
to land you right in the subdirectory.

Lets summarize:


CD..   (Work your way down a path)

CD\ (Take you right to the root of the drive)

CD NameOfDirectory\NameOfSubDirectory (Work your way up a path)

CD (When used on its own it just displays the current path)
Switching Between Drives

This is very easy as all you have to do at the Command Prompt is type the following and press "Enter"

D:   (After typing D: press the "Enter" key to switch to D Drive)

E: (After typing E: press the "Enter" key to switch to E Drive)

F: (After typing F: press the "Enter" key to switch to F Drive)

C: (After typing C: press the "Enter" key to switch back to C Drive)
Clearing The Screen

You probably now have a DOS screen full of typing and displayed information. To clear it all just type CLS at the Command Prompt and press "Enter".

Listing Directories (Folders) And Files

Type Dir at the Command Prompt and press "Enter" to display a listing of the Directories and Files contained in the current Directory. There is various options to use with the Dir command :

Dir /p (Pause the display after every screen - Press Enter to view next screen)
Dir /w (Displays in Wide Format. Date and time file created not displayed)
Dir /aa (Displays only files ready for archiving)
Dir /ah (Displays only hidden files)
Dir /ar (Displays only read-only files)
Dir /as (Displays only system files)
Dir /a-a (Displays all files except files ready for archiving)
Dir /a-h (Displays all files except hidden files)
Dir /a-r (Displays all files except read-only files)
Dir /a-s (Displays all files except system files)


Creating Directories (Folders)

If you are not currently in C: (C Drive) type C: and press the "Enter" key on the keyboard to take you to C Drive. Also if not currently in the root of C Drive which will be displayed like this : "C:\", type CD\ and press "Enter" to take you to the root of the drive. Now type MD MainDir and press "Enter" to create the in the root of C:\ the MainDir directory. The MD command is used to create directories. To remember it easier think of it as Make Directory.

Type the Dir /p command and press "Enter" to see if you can notice the MainDir directory. Note that directories will always be indicated by <DIR> right after the date and time fields. Then type cd MainDir to change current directory to

C:>\MainDir>
Note that you can use cd or CD as the case in which you type the commands does not matter.


Once inside the MainDir directory type md SubDir1 and press "Enter" to create a subdirectory called SubDir1 inside the MainDir parent directory. Follow it up by typing md SubDir2 to create a 2nd subdirectory inside the MainDir parent directory.

Use the dir command to display a listing of your 2 subdirectories. I will get a little bit later to the issue of deleting directories as we are first going to use these subdirectories that have just been created in some exercises.

Editing Text Files

At the command prompt type Edit and press "Enter". The MS-DOS Editor will now display in the Command Prompt Window. Type some text inside the editor window. Now click on "File" and then on "Save". Before we go any further first note the path indication just below the "File Name" label. It is indicating the current directory and path to it which should now be "C:\MainDir". In the "Directories" box, double-click on "SubDir1". Note that the path should now be "C:\MainDir\SubDir1". Click on the first dot in the field next to the "File Name" label or use the Tab key until the cursor flickers in this field. Now type File1.txt in this field and click on OK.

When ready click on "File" and then click on "New". Enter some text in the editor window and click on "File" and then on "Save". Notice the path just beneath the "File Name" label which should now be "C:\MainDir\SubDir1". However, we want to save our current information in the editor window into a new file and also in the "SubDir2" subdirectory instead of the "SubDir1" directory. In the box below "Directories" there should be 2 dots right at the top. Double click on ".." and notice that the path changed to "C:\MainDir". Doesn’t this look somehow familiar? Yes remember the CD.. command that is used to work your way backward down a path to the parent directory. It is fulfilling the same function in this "Directories" box. Next double-click on "SubDir2" and note that the path should now be "C:\MainDir\SubDir2". Use the Tab key on the keyboard until the cursor flickers in the field next to "File Name". Type File2.txt in this field and click on OK.

For the purpose of demonstrating some other commands we need to create another file. So here we go again. In the MS-DOS Editor click on "File" and then on "New". Type some text in this editor window and save it as "File3.dat" (Notice the different extension we use this time - ".dat" instead of ".txt") in "C:\MainDir\SubDir2". If you get stuck just look back at the steps in the previous paragraph. Also make double sure that it is saved in the "SubDir2" directory as we need it in that location for some of the exercises that will follow.

Now click on "File" and the on "Exit" to exit the MS-DOS Editor. You should be now back at the MS-DOS Command Prompt. Should you need to edit any of the text in the files you've just created use the Edit command. When the MS-DOS Editor displays, click on "File" and then on "Open". Use the "Directories" box to work your way up or down paths or to change drives. All files that is listed in the current directory will be listed in the "Files" box. To open a file double-click on it.

Just one last thing I would like to bring under your attention while we are still busy with the Edit command. If still in the MS-DOS Editor click on "File" and then on "Exit" to exit the MS-DOS Editor. Make use of the CD command to set the Command Prompt to "C:\MainDir\SubDir2>" if it is not the current directory. Type Edit at the Command Prompt and press the "Enter" key. Click on "File" and then on "Open". In the file name notice the following characters: *.* which is called "wild cards". What it basically means is the any file name with any file extension will be listed in the "Files" box that is in the current directory. Change the *.* to *.dat and press "Enter". Notice that only files with the .dat extension will be listed. Alternatively change it to *.txt and only files with the .txt extension will be listed. We will get back to this "wild cards" at a later stage when deleting or copying files.

Using The ATTRIB Command

The ATTRIB command is used to set file attributes like making a file read-only or make it a hidden file. All the different file attributes is listed below:

R      Read-only file attribute.
A Archive file attribute.
S System file attribute.
H Hidden file attribute.
If you are still in the MS-DOS Editor click on "File" and then on "Exit". If you followed along the Command Prompt should currently be "C:\MainDir\SubDir2>". If it is not the case use the CD or CD.. command to change it to "C:\MainDir\SubDir2>". Type Dir at the Command Prompt and make sure that the files "File2.txt" and "File2.dat" are present in this subdirectory. If it is not the case exactly repeat the steps explained in the "Editing Text Files" section. At the Command Prompt type Attrib +r File2.txt and press the "Enter" key. File2.txt should now be a read-only file. You can test this by typing Dir at the Command Prompt. Both "File2.txt" and "File3.dat" should now be listed. Now type Dir /ar at the Command Prompt and press "Enter". With this command entered like this only read-only files will be listed and you should now only see "File2.txt" listed.

Type Edit at the Command Prompt and click on "File" and then on "Open" in the MS-DOS Editor. Notice that the path beneath the "File Name" label should now be "C:\MainDir\SubDir2". If it is not the case set it by using the Directories box. Double-click on "File2.txt" to open it. Try to make some changes to the text in this file, but as you will see you will be notified that the file is read-only. If you should open this file in Notepad in Windows, you will see that you can make changes to it but you will not be allowed to save it under the same name. Exit the MS-DOS Editor now. At the Command Prompt type Attrib -r File2.txt and press the "Enter" key. The read-only attribute should now be removed from the file. If you open the file now with the MS-DOS Editor you will see that changes can be made and it can be saved. Exit the MS-DOS Editor by clicking on "File" and then on "Exit".

Under normal circumstances you should never have to use the "System File Attribute" as this is reserved for system files. Therefore the only other attribute that is worth mentioning is the "Hidden File Attribute". At the Command Prompt type Attrib +h File2.txt and press the "Enter" key. Type the Dir command and notice that "File2.txt" will not be listed. If you however type Dir /ah it will be possible to see this file as Dir /ah displays only hidden files. Likewise in Windows you will not be able to find the file if "Folder Options" is set to "Do not show hidden files and folders". If the "Folder Options" is set to "Show hidden files and folders" then you will be able to see the file even if its hidden attribute is set. At the Command Prompt type Attrib -h File2.txt to remove the hidden attribute.

The TYPE Command

The Type command is use to display the contents of text files on screen. At the moment the Command Prompt should still be at "C:\MainDir\SubDir2>". If not please change it accordingly. Type Type File2.txt at the Command Prompt and press "Enter". The contents of "File2.txt" should now be displayed. Well that is all there is to the Type command.

The COLOR Command

Lets spice up the console display with some color. Use the COLOR command to set the default console foreground and background colors. Below is a list of hex digits that represents certain colors.

0 = Black
1 = Blue
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Light Blue
A = Light Green
B = Light Aqua
C = Light Red
D = Light Purple
E = Light Yellow
F = Bright White
Type COLOR 3f at the command prompt and press the "Enter" key to set the console background color to aqua and the foreground color to bright white. To reset the console foreground and background colors to its default state just type COLOR without any argument at the command prompt and press the "Enter" key.

Moving Files

In the few examples that is going to follow we will make use of "wild cards" again when working with files to give you more insight into their use. Make sure the Command Prompt is currently "C:\MainDir\SubDir2>". If not use the CD or CD.. command to change it to "C:\MainDir\SubDir2>". At the Command Prompt type Move *.* C:\MainDir\SubDir1 to move the files to "SubDir1". Note how you had to specify the absolute path to Subdir1. Also note the use of wild cards. The *.* actually means All File Names with All Extensions should be moved to SubDir1. Type Dir at the Command Prompt and you will see that "File2.txt" and "File3.dat" is no longer present in "SubDir2".

To continue with the exercise we should change over to SubDir1. At the Command Prompt type CD.. which should move you back to "C:\MainDir>" and then type CD SubDir1 which should move you to the "C:\MainDir\SubDir1>" subdirectory. Type Dir at the Command Prompt and press "Enter". "File1.txt", "File2.txt" and "File3.dat" should now be present in "SubDir1".

At the Command Prompt type Move *.txt C:\MainDir\SubDir2 to move only the files with an .txt extension over to "SubDir2". When you do a directory listing with the Dir command you will notice that only "File3.dat" remains in the "SubDir1" subdirectory. Just a quick reminder, you do not have to use capital letters in your commands or in the path. I'm just doing it to make it more readable.

Copying Files

For the purpose of this exercise we have to be back in "SubDir2" so use the CD.. command and type "Enter" to get back to the "C:\MainDir>" parent directory. Now type CD SubDir2 to move to the "C:\MainDir\SubDir2>" subdirectory.

Use the Dir command and notice that "File1.txt" and "File2.txt" that you previously moved here with the Move command will be present. At the Command Prompt type Copy *.* C:\MainDir\SubDir1 to copy all the files in "SubDir2" over to "SubDir1". You also could have used Copy *.txt C:\MainDir\SubDir1 that would have had the same effect as these were the only files present in this subdirectory.

Type Dir at the Command Prompt and notice that because you used Copy instead of Move, both files will still be present in the "SubDir2" subdirectory.

We must now change over to "SubDir1". At the Command Prompt type CD.. which should move you back to "C:\MainDir>" and then type CD SubDir1 which should move you to the "C:\MainDir\SubDir1>" subdirectory. Type Dir at the Command Prompt and press "Enter". "File1.txt", "File2.txt" and "File3.dat" should now be present in "SubDir1" as "File1.txt" and "File2.txt" has been copied here with the Copy command.

Deleting Files With The DEL Command

Just a friendly reminder, take EXTREME care with the Del command as files deleted with this command is not going to the Recycle Bin and it can NOT BE RECOVERED.

Currently the Command Prompt should be at "C:\MainDir\SubDir1>". Type Del *.txt and use the Dir command to see the listing. Notice that all files with the .txt extension should have been deleted by the previous command. To delete the remaining file type Del File3.dat and do a directory listing again with the Dir command. There should be no remaining files.

Now we should change over to "SubDir2". Use the CD.. command to back down to the "C:\MainDir>" parent directory. Then use CD SubDir2 to move to the "C:\MainDir\SubDir2>" subdirectory. Do a Dir and notice that there is 2 files present in the subdirectory. Type Del *.* at the Command Prompt and do a Dir again. Notice when using the *.* wildcards together with the Del command that All File Names with All Extensions will be deleted which will be all files present in the current directory or subdirectory.

When using the *.* wildcards with the Del command the system might prompt you if you are sure. Just type a y or n which ever may be the case and press "Enter". If you previously forgot to remove the read-only attribute from "File2.txt" access will be denied if you try to delete this file. To fix that just type Attrib -r File2.txt at the Command Prompt and press "Enter". The Del command should now work without denying you access.

Deleting Directories (Folders) With The RD Command

As in the case when deleting files with the Del command, the same goes for when deleting Directories (Folders) with the RD command, please take EXTREME care with the RD command as Directories (Folders) deleted with this command is not going to the Recycle Bin and it can NOT BE RECOVERED.

If you currently are in the "C:\MainDir\SubDir2>" subdirectory use the CD.. command to back down to the "C:\MainDir>" parent directory.

Type RD SubDir1 and press "Enter" to delete the "SubDir1" subdirectory. To easily remember this command just think of RD as Remove Directory. Doing a Dir will reveal that "SubDir1' is deleted and only "SubDir2" remain. Proceed by typing RD SubDir2 to delete the "SubDir2" subdirectory.

Now type CD.. or CD\ to back down to the root of the drive. Type RD MainDir to delete the parent directory. Doing a Dir /p to see for yourself that it has been deleted. Just a quick reminder - the Dir /p is used to pause after every screen just in case the listing spans more than one screen.

Displaying or Setting The Date And Time

Type Date at the Command Prompt and press "Enter". The current date will be displayed and you are offered the option to change the system date. If you want to change the date be sure to follow the format as indicated. If you do not want to make changes to the system date just press the "Enter" key without entering a new date.

To view or set the system time type Time at the Command Prompt. Type the new system time in the indicated format and press "Enter". If you do not want to make changes to the system time just press "Enter" without entering new time information.

Other Uses For The COPY Command

If working with complex print jobs see if your software supports the "Print To File" option. This option is normally supplied in the print dialog box of certain applications like Microsoft® Word, Microsoft® Excel or CorelDraw® and lots of other software products. What is nice about this trick is that if the file is very complex and printing takes a long time to process, it can now only be processed once and from then on copied to the printer port.

Another use for this "Print To File" feature is if you want the document printed on a printer that you do not have connected to your PC for instance a printer with better print quality.

Ok, lets do it by way of an exercise. Start up Microsoft® Word (On the keyboard press "Ctrl" and "Alt" simultaneously and then press "W" if you followed along with the Ultra-Fast Program Access tutorial). Do some typing and insert some colorful graphics or photos. Now one problem, your own printer's quality is maybe not exactly what you had in mind, but you can always pop over to your neighbor or a family member who got the latest and greatest of printing technology connected to their PC. Now you might be faced with another problem, your neighbor might not have the same software application that you used to produce your masterpiece in. What now? Now for the solution ...

Get hold of the Driver for the printer you plan to print to. (It might already be available in Windows XP if you go to "Control Panel", "Printers and Other Hardware" and click on "Add Printer" or download the driver from the Internet). In your newly created masterpiece in MS Word or whatever application that is supporting the "Print To File" option click on "File" and then on "Print". Select the printer that you plan to print this job for in the field next to "Name" of the Printer and click on "Print to file" to put a check mark in that selection box. Now click on OK. A "Print to file" dialog box will appear. In the left pane click on "Desktop" and in the field next to "File name" type the name of the file. For the purpose of this exercise just call it "myfile" without the quotes. This file should be now on the "Desktop". Also note that the file will have a .prn extension.

Now copy "myfile.prn" to your favorite external media storage and you are ready to visit your neighbour. Copy "myfile.prn" to your neighbor’s PC's Desktop. Next start the MS-DOS Command Prompt. In Windows XP it can be done by clicking on "Start", point to "All Programs", point to "Accessories" and click on "Command Prompt". The Command Prompt should be something like: "C:\Documents and Settings\UserName>" where "UserName" will be the name of the current user. Type the CD Desktop to move the Command Prompt to the Desktop directory (folder). Following is the final command to print the document on the desired printer. (For the command to work the printer must be connected to the LPT1 port).

Type copy /b myfile.prn lpt1 and press the "Enter" key. The results of this operation is not always as desired so please check if your document turns out as expected.

(Please note that I have not actually tested this command on a machine running Windows XP, but in theory it should work if Windows XP allows the copy operation to go ahead. I will however post an update on this tutorial, once I connect a printer to the LPT1 port of my system again).

Batch Files

Batch files are a quick and efficient way to automate MS-DOS tasks or commands. Make sure the Command Prompt is currently in the root of C: (C Drive). It should display as follow: C:\> (If not Type CD\ at the Command Prompt and press "Enter")

Type Edit at the Command Prompt and press "Enter". In the MS-DOS Editor enter the following lines of text exactly as shown below with each command on a separate line. (Press the "Enter" key when at the end of a line).

MD MainDir

CD MainDir
MD SubDir1
MD SubDir2
In the MS-DOS Editor click on "File" then click on "Save". Next to the "File Name" label type "mybatch.bat" without the quotes in the "File Name" field and click on OK. Exit the MS-DOS Editor by clicking on "File" and then on "Exit".



At the Command Prompt type mybatch and press the "Enter" key. The batch file will execute and firstly create the "MainDir" directory in the root of the current drive. Then it will do a change directory to "C:\MainDir>". After that it will create "SubDir1" and "SubDir2" in the parent directory "MainDir" by using the MD (Make Directory) command. If you do a Dir now you will notice that you are already in the "MainDir" parent directory and the 2 created subdirectories is present. (Note that the names "MainDir", "SubDir1" and "SubDir2" I'm using is just to make explaining the exercises easier. You may use any name you desire.



The PATH Command.
As I said earlier I will devote some time to this command. The PATH command is used to set or display the search path for executable files. If the Command Prompt were currently at "C:\MainDir>" and you typed Edit to launch the MS-DOS Editor, the Command Interpreter (command.com in earlier OS versions or lately cmd.exe) would not have been able to find the MS-DOS Editor program if not for the PATH statement. Type PATH at the Command Prompt and press the "Enter" key. All the different search paths will be listed. So when you type Edit at the Command Prompt the Command Interpreter will search through all these paths to find the Edit Program.



I recommend that you read through the 3 paragraphs below, but DO NOT ACTUALLY TYPE OUT AND EXECUTE THE COMMANDS if not really required. (At a later stage I will show you in one of the more advanced Windows tutorials how to safely and permanently make changes to your system's path and under what circumstances you really need to do it).



To reset the path type PATH; at the Command Prompt and press "Enter". (DO NOT ACTUALLY DO THIS AS YOUR MACHINE MAY NOT OPERATE CORRECTLY AFTER THE PATH HAS BEEN CLEARED)



To create a new search path type for instance PATH C:\MAINDIR at the Command Prompt and press "Enter". (NOTE HOWEVER THAT THIS WILL CLEAR ALL OTHER SEARCH PATHS AND CAUSE YOUR MACHINE TO OPERATE INCORRECTLY)



The most logical way to correctly use the path statement is to append the old PATH to your newly created path. Your machine should then still function properly. Do it as follow by typing PATH C:\MAINDIR;%PATH% at the Command Prompt and pressing the "Enter" key. (Note that no spaces is to be used in the statement/parameters section immediately after the PATH command. There should only be one space and that is immediately after the PATH command right in front).



These settings to the systems path is only temporary. Should you mesh it up, shut down the computer and restart it to reset the PATH to its original state.



Other Import MS-DOS Commands.

I'm not going to talk about the FDISK and FORMAT command in this tutorial. These command used to be very important in preparing your hard disk before loading Windows 98 or previous versions. With the widespread use and easy installation procedures of Windows XP these commands have dropped in importance as Windows XP basically leads you through the process.



To give you more insight into these 2 commands and your system as a whole, I will however in the future write a separate tutorial for them that will be intended to be practiced on an old machine and not a machine that is in everyday use.



Getting Help From DOS

I have covered by way of exercises the commands that will see you through with MS-DOS. Just in case you need more or quickly need info about one of the commands I've covered above, help is at hand directly from MS-DOS.



To get a listing of all MS-DOS commands type Help at the Command Prompt and press "Enter". Make sure the Command Prompt is not in full-screen mode otherwise you will not be able to see the complete listing. When not in full-screen mode you can use the scroll-bars at the right of the window to scroll up or down.



To get help for a specific command type CommandName help or type CommandName /? and press the "Enter" key. Below is some examples:

dir help or copy /?


Finally remember that you do not have to use capital letters to type out the commands.



The EXIT Command


To exit the Command Prompt session type Exit and press "Enter".



Final Word

It was quite a mouthful and I certainly hoped you've learned a lot. This was the knowledge I started of with and to learn it really helped me to appreciate how everything fit together. A lot of the knowledge you gained here will even help you to better understand Windows when you use it. In the Windows tutorials that will follow you will see how to accomplish the same tasks and knowing the MS-DOS way of doing it will really give you a better understanding of everything.



6 comments:

shaliyamk said...

I realy thank you for giving such a knowlage.Your work is very good.Thank you.

Nomi Jutt Sahab said...

thanx for the help

chethu lakya said...

Thanx man its good its helped me a alot

yani ^_^ said...

This is awesome... in just a little time i already learned a lot. Thank You!

rinku patnaik said...

you beauty....thnx mn...

Syed Qasim Sabzwari said...

which comonds are use to for (file hiding and unhide) plz ans