Mastering The Command Prompt With: Ms - Dos
Mastering The Command Prompt With: Ms - Dos
Mastering The Command Prompt With: Ms - Dos
MS - DOS
Compiled By:
Benjamin Boateng Adofo
(Software/Website Engineer)
Table of Contents
Introducing MS – DOS………………………………………………………………………………………..…….. 3
Formatting ………………………………………………………………………………………………………………….21
Deleting entire directory trees .……………………………………………………………………………22
AUTOEXEC.BAT ………………………………………………………………………………………………………….23
Complex Batch files
Graphical interfaces for Batch files.
DATE variable …………………………………………………………………………………………………………….27
Reference ……………………………………………………………………………………………..28
Introducing MS – DOS
MS-DOS stands for "Microsoft Disk Operating System". The question most IT
Professionals ask is that DOS is a command-line method of executing basically the same
functions available in Windows. The main difference is the lack of wizards in DOS as in
windows, the need to be precise, and the fact that DOS came first before the standard
windows.
However, DOS has much more power than the standard Windows, and if you know the
right commands, you can get information much more quickly.
In DOS, [nameofdrive]:\>, means the root directory eg C:\> is the root drive.
Objectives.
In this lesson, you will learn to:
Depending on whether you are using the Prompt or "true DOS", you should see a
prompt similar to this:
C:\>
or this:
Obviously, you have to be able to see what you're doing or else you are "shooting in the
dark" with no idea what to do. Luckily, there is a command to show what is in your
current directory. Type DIR and press [Enter] to activate this function. It should look like
this:
C:\>DIR
Directory of C:\
The items with <DIR> are directories, or folders. Anything else is a file. The column on
the left is the date the files were last changed, the next column shows the time the files
were modified. The number to the left of the file name is the size of the file in bytes.
If there are too many directories to list on one screen, type one of the following
commands:
DIR /w
OR
DIR /p
The /w option makes the DIR appear in a wider format, while /p displays the DIR one
screen full at a time.
Changing directories
One may be required to change the current working directory. To change directories,
type CHDIR [directory name] or CD [directory name], replacing [directoryname] with
the name of the directory you wish to go to. So, on my pc, if I want to change to
C:\Amen Amen, I type
C:\Amen Amen>
Opening files
In the Command Prompt, these files will run outside of cmd.exe. In true DOS, this is
obviously not the case, but only DOS-friendly files may be run. To open an EXE file,
simply type the name of the EXE file, and it will run. So, If I want to call the windows
calculator from the command prompt, I would type
C:\>calc.exe
and the program would run. To access a text file, you would use the TYPE command:
C:\>TYPE Kofi.txt
where Kofi is a name of a text file on drive C.
and the information would display on my command screen. Note, however, that some
text files may be too long for the screen. For files like this, type the following command:
Replacing Kofi.txt with the name of the file you want to display. The | is called the pipe
key and is found above the [Enter] key ([Shift] and \). When the file reaches the size of
the page, it will pause, asking you to press [Enter]. Pressing [Enter] will move the page
down one line. If you change your mind partway through reading the long file, Press
[Ctrl] and C. You will go back to the prompt.
The TYPE command works with virtually any kind of file, but note that you will not be
able to read all of them. Compiled EXE files will show strange characters, as will .dat and
other files. Do not worry, no harm is done.
If your screen is filled with text, press CLS at any time. This command clears the screen
of everything but the latest prompt.
Creating files
So far, you have only taken a look at how the files are stored and where. Here, you will
learn two ways of adding to your system in a way that cannot harm your system at all:
Creating files.
This is a part of MS-DOS that is more powerful than Windows. In Windows, you can only
create the extensions listed on the "New" menu. In MS-DOS, you can create any file
extension you want, including extensions not recognized by Windows. They can still be
read, however, from Windows if you open it with a program like Notepad.
To create a file using the Copy Con method, type at the prompt:
Replace Newfile.txt with any file name and extension you choose and press [Enter]. At
first, nothing will seem to happen. You will need to press [Control] and Z. The symbol
^Z will appear. Press [Enter] to confirm. What you see should resemble this when you
are done:
1 file(s) copied.
C:\>
EDIT newfile.txt
You will be taken to a blue window, MS-DOS's first attempt at a GUI. Type whatever you
want, then go to File and click Save. Then go back into File and click Exit. The file is
created.
A note about the EDIT method: If the file already exists, you will see that file's contents
instead of a blank blue screen. It is not recommended that you change what's there
unless you know what you are doing.
Replace Newfile.txt with any file name and extension you choose and press [Enter]. At
first, nothing will seem to happen. You will need to press [Control] and Z. The symbol
^Z will appear. Press [Enter] to confirm. What you see should resemble this when you
are done:
Editing files
Editing files is as easy as the Edit method for creating files. For repitition's sake, simply
type:
EDIT file.txt
You will be taken to a blue window, MS-DOS's first attempt at a GUI. Type whatever you
want, then go to File and click Save. Then go back into File and click Exit.
You, of course, can edit and change the file's contents just like the Windows Notepad.
The only thing that must be said about the EDIT command is that it is the only way to
edit files in true DOS, however, in the Prompt it is almost useless; you can open any file
you want in Notepad, Word or some other program.
FILE
EDIT
Cut - Removes selected text but keeps in memory (known as the clipboard). Only one
string of text may be kept in memory at a time.
Copy - Same as Cut, but leaves the original intact.
Paste - Whatever is in the clipboard is placed at the cursor.
Clear - Removes selected text without committing it to memory.
SEARCH
OPTIONS
Settings - Allows you to specify how many tab stops on the page and what port the
printer is on.
Colors - Allows you to change what color text, the background, etc. is.
Another method is the Edit method. Simply type:
EDIT newfile.txt
You will be taken to a blue window, MS-DOS's first attempt at a GUI. Type whatever you
want, then go to File and click Save. Then go back into File and click Exit. The file is
created.
A note about the EDIT method: If the file already exists, you will see that file's contents
instead of a blank blue screen. It is not recommended that you change what's there
unless you know what you are doing.
HELP
NOTE: In older versions of "True DOS", the REN* command is not available. You will
have to use the MOVE command, if this is the case (covered later).
Normally, the attributes like "read-only" are taken off during XCOPYing. With this option,
all attributes are preserved.
You can see why XCOPY is considered so powerful; it truly is! Usually, however, a
normal COPY will be suitable for your purposes.
Moving files
Let's say you have a file in C:\ but it needs to be in C:\Pictures. You could copy it, and
then go back and delete the original. But there is an easier solution.
NOTE: I cannot verify this, but I believe that some really old versions of DOS do not
support a MOVE command. The MOVE command is exactly like Copy, except that the
original, the source, is deleted. The syntax is:
The only options are the /Y and /-Y, which serve the same function as the /Y and /-Y in
the COPY command. As with COPY, you can move multiple files elsewhere.
Renaming a file
Oh no! You wanted to create a file called Amen.txt but you accidentally created
Jesus.txt! How do you fix it? Better get rid of it and start over, right? No! Windows has a
handy Rename feature, and so does DOS.
To use the Rename feature, type RENAME, the file's current name, and the new one. The
syntax (proper order/method of issuing commands) for this example is this:
The file will be named the way you want it to. This command also works with directories.
Say I have the directory C:\Amen but I want it to be C:\Amen Amen. It is done like this:
A shorter method of renaming files is REN. It does the same thing as RENAME and works
the same way; it's just shorter. Use what works for you.
It is also possible to change the extension of your file with the REN* command. Simply
specify it like this:
Copying files
OK, so you got a file on C:\ but you really want it on C:\Backups. BUT... you don't want
to lose the original. Here's where the COPY feature comes in handy. The syntax is:
There are options for this command which alter the copy somewhat. An option is any
modifier to the command; they are expressed usually after the entire command, as a /
and a letter or number. In this case, the options are:
It is also possible to copy multiple files at the same time. Let's say you wanted to copy
all the files in C:\Backup to e:\ (a pendrive). To do this:
COPY C:\Backup\*.* A:
To combine specific files, do this:
COPY file1.txt+file2.txt
This will add all of file2.txt to the end of file1.txt
XCOPY is a powerful version of COPY which has more versatility, options, and is
therefore a must-have. For simple commands, COPY is sufficient. However, XCOPY
works exactly the same, but with more features. The XCOPY options related to files
are:
To make sure you're copying what you want to copy, This option is for you.
/W - Asks you to press a key before copying.
Good for batch files (covered later). If you need to make sure the user is OK with a
copy, this will work out nicely.
/F displays source and destination names while copying.
Good for if you need to see what all is being copied.
/L - Displays files that would be copied. Does not actually copy.
Good for a "preview" function.
/H - Copies hidden and system files as well as normal files.
This will help make sure you have everything in that folder.
/U - Updates the files instead of overwriting them - makes the destination files the
same as the source.
Great for backups.
/K - copies all attributes over.
Normally, the attributes like "read-only" are taken off during XCOPYing. With this
switch, all attributes are preserved.
You can see why XCOPY is considered so powerful; it truly is! Usually, however, a
normal COPY will be suitable for your purposes.
Removing files
That dumb test.txt file is taking up valuable space on your hard drive! What's worse is,
you don't need it anymore! Great, just great. How does one remove it? Well, it can be
done with one of not one, not two, but three commands. The following three are all in
correct syntax and do the exact same thing:
DEL test.txt
DELETE test.txt
ERASE test.txt
Using the /Q switch disables this prompt for this particular delete.
/A - Deletes based on attributes.
DEL *.* /AR would delete only read-only files
DEL *.* /AH deleted only hidden files
DEL *.* /AS removes only system files
DEL *.* /AA erases only archives files
DEL *.* /-H erases everything but hidden files (the "-" prefix can work with any attribute)
It is possible to create more directories. There are two codes that do the same thing:
Either way, a new directory is created for you to store files in. So, if you need a directory
for all of your pictures. So, you could type
MD Pictures
To rename a directory, use the REM command. If you misspell Pictures, say, Pictyres,
you can enter the command
If you want to remove a directory, you can use the RMDIR or RD command:
RD [directory name]
Normally, only empty directories, with no files or sub-directories in it, can be removed.
The options for RMDIR/RD are:
NOTE: The above options only work with Windows 2000/XP. To remove entire
directories, including files, in earlier versions of Windows or DOS, see the DELTREE
command (covered much later).
Objectives.
Copy directories/folders(advanced)
View the Internet Protocol (IP) information of your Computer
Create Simple Batch files
Output command to text file
Implement Attributes
This level assumes that you are familiar with the basic functions of DOS. If you
are not, please review lesson I.
now, you have a whole load of files in a bunch of directories, and they all stem from
C:\Documents. You need the entire thing, from C:\Documents\ down to the last
subdirectory and file, copied to a pen drive or another drive. Sounds difficult, but is
really easy.
XCOPY has a lot of power concerning the copying of files, but XCOPY, unlike COPY, is
capable of copying directories as well. The options for XCOPY regarding directories are:
/T - Copies directory tree, but no files. /T by itself does not include empty source
directories.
/S - Copies all directories except for empty ones, along with the files they contain.
/E - Forces XCOPY to copy empty directories as well. Can be used with /T or /S.
So, if you wanted to copy every cotton-picking thing in C:\Documents, you would do
something like this:
XCOPY C:\Documents\*.* e: /S /E
A note to Windows Users: Sometimes XCOPY has a problem with copying Internet
Explorer favorites. This is because IE tends to violate the 8.3 format by using long file
names with extensions of more than three digits. If this does not work and regular COPY
does not work, then there is no way of copying that favorite. However, if you are in the
Recovery Console attempting to save your computer's files before you format the disk,
it's a small loss.
IP information
As you may know, your IP address is your unique computer signature on the net. Even if
your PC does not have access to the Internet, you still have an IP address for "just in
case". Sometimes, it is necessary to retrieve or modify this information. To see all the
information you will need, use the command
ipconfig /all
This will display, among other things, your IP address, Subnet Mask, Default Gateway,
etc.. You should also see info about any Ethernet Adapters or Wireless Adapters you
have. You may or may not have more than one adapter; most PC's just have the one.
So, if you need to renew your IP address in order to troubleshoot or some such thing,
type
ipconfig /release
If you have more than one adapter, specify the number of the adapter like this:
ipconfig /release 0
replacing 0 with the number of your adapter. This command gets rid of your IP address.
Now, type
ipconfig /renew
if you specified a number when you released, then you should specify that same number
again.
To say that batch files are simple is like saying the ocean is water: It can be true, but
there's usually more to it than that. Some batch files are easy and simple, requiring little
actual coding. Others seem to be the code version of essays.
Before you can make a batch file, you need to know a bit about them. Batch files are
really nothing more than a series of DOS commands strung together. When the batch
file is executed, all of the commands are run. There are a series of commands that may
work in the prompt, but are more suited to batch files. They are:
There are others as well, but for simple batch files this should be enough.
To use a batch file, create a file with a .bat extension. Review Creating Files if you are
unsure of how to do this. If you are using a Prompt, you can either use the EDIT
command or use Notepad. True DOS users are stuck with the EDIT function.
The first line of a batch file is usually the line "@ECHO OFF". This line makes in
unnecessary to use a "@" for the rest of the batch. For simplicity's sake, as an example
we will write a batch file which clears the screen, displays the directory's contents in a
wide format and displays a custom message. Each command must be on it's own line.
Code:
@ECHO OFF
CLS
DIR /W
ECHO Wide format on clear screen courtesy of Amen Amen
Replace Amen Amen with your name in the above example, and you'll have a perfectly
good batch file with a specific function. Play around with different DOS command
variations; be creative. See what utilities you can create. A few ideas:
Change directories to a specific folder, give the contents, and pause, clearing the
screen on resume.
Write a short essay on how to do something in DOS and use the ECHO command
to display it.
Make a file that creates a file, allows you to edit it, and then make a backup using
the COPY command.
OK, so you want to make a log of what a batch file does. Or, you want to make an
example of what a command does because you're making a MS-DOS FAQ and you want
the format to look just right so the readers won't be confused. For whatever reason, you
want to get the output - the results of the command - into a text file.
Doing this is deceptively easy. Type the command, then add >> and the name of the
text file. Example:
The DIR results will not display on the screen, but instead will place DIR info in log.txt.
NOTE: This may not work for you. If it doesn't, use only one ">" instead of two.
Classically, one > is used to create the file and >> is used to append to it. Depending on
your DOS/Prompt version you may be able to get away with only using >>'s. Your
mileage may vary.
Attributes
For whatever reason, you may want to make it so that nobody can modify a file. Or,
maybe hide it from view. This is where the ATTRIB command comes in handy.
The use of this command varies from Operating System to Operating System, but they
are similar:
+ - adds attribute.
- - Removes attribute
R - Read-only (cannot be modified while attribute is set)
ATTRIB +R nochange.txt (adds attribute)
ATTRIB -R change.txt (removes attribute)
A - Archive
S - System file
H - Hidden file
/S - Sets attribute to all files in all folders in specified path.
ATTRIB +H file.txt /S (sets all files in current directory and below as hidden)
This tutorial set assumes that you have been reading the other tutorials and/or have a
decent understanding of how MS-DOS works and that you are very comfortable with
using MS-DOS. If this is not the case, it is recommended that you review any topics you
are unfamiliar with.
Formatting
You want to use a pendrive but you're getting a "unrecognized file system" or similar
error. Or, you're in the recovery console and you need to wipe your drive clean
(presumably after backing up data). You can do this in the prompt by using the FORMAT
command.
For safety's sake, we will assume you are not going through the turmoil of the recovery
console. Either way, it's almost the same.
FORMAT e:
The above command would format your pen drive drive. If you buy a new disk, even if it
says it's formatted, you should use this command before using the pen drive for the first
time. If you just want to erase data, use the following:
FORMAT e: /Q
This forces a "quick" format. Other options are Operating System specific. On Windows
9x/ME, you generally should choose your preference between the following:
/V:label - Replace label with any name, and the disk will be labeled that name.
/B - Makes the disk a "System" disk.
/S - Copies system files to the disk.
/C - Files placed on the disk are compressed by default.
/FS:filesystem - Replace filesystem with FAT, FAT32, or NTFS; the file system specified
will be used in the format. Only use this if you are sure of what file system you want.
/Q - Same as Win9x / ME.
In the Win2000/XP Recovery console, you only have access to /Q and /FS:filesystem.
So, to format C:, and make sure it's an NTFS file system, and give it a label of
"WindowsXP", the command would be:
It is STRONGLY recommended that you NOT use the above command unless
you are sure you want to do this. You will lose everything on your boot drive,
all files, etc. Make a backup of important files before you begin.
If you wish to practice this command, you should do so on a (preferably blank) diskette.
This way, you cannot harm your system nor will you lose important files.
The Windows GUI uninstall feature hardly removed anything. Or, you're in the Recovery
Console and you're trying to free up space on your hard drive. Well, this command
works perfectly.
NOTE: If you are using Windows 2000 or XP, the command below is not for you. Use the
RD command with the /S option for everything from DOS 5.0 to Windows ME:
DELTREE [path]
So, if you no longer need C:\Program Files\Real and everything in it, do this:
There is only one switch: /Y. Using this forces the DELTREE to run without prompting for
you to give the OK.
AUTOEXEC.BAT
Some installation instruction requires you to edit the dreaded AUTOEXEC.BAT. Oh no!
What are you going to do now? Well, it's really quite simple. AUTOEXEC.BAT contains
nothing more than variables with set values. You can see the list of variables by typing
"SET" bar the quotes and pressing [Enter]. It will be a long list. Most of these values
should not be modified at all. However, you can add variables without causing harm, and
you can also add to the PATH variable without causing any trouble.
Let's say you want to keep batch files in a directory, and have them work no matter
where in your computer you are. Simple, right? Of course it is. Let's say you have your
batch files in the same place I do - C:\Batch. To "install" this directory, go into
AUTOEXEC.BAT, find the path variable, and go to the last directory. If there is not a
semicolon (;) at the end, put one there. Then type the full path of the directory (no
spaces in the whole variable). Save and exit. You should be able to use your batch files
anywhere you choose. If not, type AUTOEXEC and press enter; you may need to have
the PC cycle through the PATH variable again.
To add a variable of your own, simply go to the end of AUTOEXEC.BAT, add a new line,
and use this syntax:
[variable]=[value]
Example:
color=blue
You can also set variables with the prompt, but they only exist for that particular
session. If you reboot (or exit, if you're using the Prompt) the variable will be gone. use
the SET command:
SET color=red
You can set the same variable an infinite number of times, limited only by your boredom
with endlessly setting a variable; however, there may be a limit to the number of
variables you can set. If you run into this, consider reusing a variable or exiting the
session/rebooting (for true MS-DOS).
As you will learn in the next lesson, variables are a great asset to the MS-DOS
experience.
As you will learn in the next lesson, variables are a great asset to the MS-DOS
experience.
FOR - The FOR loop has a wide range of uses. It's main purpose is to execute a
command for all files/directories that meet a certain standard. The syntax:
Switches available:
%%variable is the first variable to be used. The number of tokens you use is how many
variables are added by the FOR command. So, if you started with %%a and used tokens
1 and 2, you would have %%a, %%b and %%c.
(set) is whatever you want. It can be a text file (for use with /F), it can be a directory,
or even a text string. It's up to you.
Code:
FOR /F "tokens=1,2*" %%i in (file.txt)
%%i is set to the first line of file.txt, %%j is assigned to the next line, etc.. This is
repeated until the file is parsed.
Code:
for /F "tokens=1,2*" %%i in (file.txt) do echo %%i %%j %%k
Assuming @ECHO OFF is at the top of the batch, the output would be to echo the
entirety of the file.
Another example: Let's say you wanted to save all the file names in a directory into a
text file, but don't want the clutter of a DIR. You could do this:
Code:
FOR %%A in (*.*) do ECHO %%A >> for.txt
FOR - The FOR loop has a wide range of uses. It's main purpose is to execute a
command for all files/directories that meet a certain standard. The syntax:
Switches available:
%%variable is the first variable to be used. The number of tokens you use is how many
variables are added by the FOR command. So, if you started with %%a and used tokens
1 and 2, you would have %%a, %%b and %%c.
(set) is whatever you want. It can be a text file (for use with /F), it can be a directory,
or even a text string. It's up to you.
Code:
FOR /F "tokens=1,2*" %%i in (file.txt)
%%i is set to the first line of file.txt, %%j is assigned to the next line, etc.. This is
repeated until the file is parsed.
Code:
for /F "tokens=1,2*" %%i in (file.txt) do echo %%i %%j %%k
Assuming @ECHO OFF is at the top of the batch, the output would be to echo the
entirety of the file.
Another example: Let's say you wanted to save all the file names in a directory into a
text file, but don't want the clutter of a DIR. You could do this:
Code:
FOR %%A in (*.*) do ECHO %%A >> for.txt
Well, you need to use some extended characters in the ASCII Dos-Extended Character
Set.
No matter how much you hate the EDIT window, you must use it to make a graphical
interface. No exceptions.
It is advised that you hit the [Insert] key during graphical design so you don't end up
with a vertical line moved a long way away from the table you are making.
If you need the date for anything at all, use the variable %DATE%. To find the date's
format, type at any prompt:
ECHO %DATE%
Sun 05/14/2006
But what if you only want part of this? You can use the ~ modifier to change what you
get. The syntax is:
For example, to just get the date without the day, you would use
ECHO %DATE:~4%
IV. Reference
Credit:
Site: www.computerHopeforums.com