Ms Dos Lessons

32
Amen - Amen Institute Mastering the Command Prompt With MS - DOS Information and Communications Technology - ICT 1

Transcript of Ms Dos Lessons

Page 1: Ms Dos Lessons

Amen - Amen Institute

Mastering the Command Prompt With

MS - DOS

Compiled By: Benjamin Boateng Adofo

(Software/Website Engineer)

Information and Communications Technology - ICT 1

Page 2: Ms Dos Lessons

Amen - Amen Institute

Table of Contents

Introducing MS – DOS………………………………………………………………………………………..…….. 3

Lesson 1 – The Fundamentals of MS-DOS Commands .…………………….. 4

Navigating folders and Simple Commands ………………………………………………………………5 Changing directories and Opening Files ………………………………………………………………..6Clearing the screen and creating files …………………………………………………………………….7Renaming and Editing files ……………………………………………………………………………………… 8Invoking the DOS Window Editor ………………………………………………………………………….9Invoking the MS DOS HELP, Moving files and Renaming Files ……………………………….10Copying and Deleting files ………………………………………………………………………………………..11Using the XCOPY Command ……………………………………………………………………………………..12Removing Files ……………………………………………………………………………………………………….13Creating, Renaming and Removing directories/folders …………………………………………..14

Lesson 2 – Intermediate MS-DOS Commands ………………………………………………...15

Copying directories and directory trees, Viewing IP Information ……....………………..16Simple Batch files …………………………………………………………………………………………………..17Output command to text file ………………………………………………………………………………….18Attributes ……………………………………………………………………………………………………………….19

Lesson 3 – Advanced MS-DOS Commands………………………………….…………20

Formatting ………………………………………………………………………………………………………………….21

Information and Communications Technology - ICT 2

Page 3: Ms Dos Lessons

Amen - Amen Institute

Deleting entire directory trees .……………………………………………………………………………22AUTOEXEC.BAT ………………………………………………………………………………………………………….23Complex Batch filesGraphical 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.

Information and Communications Technology - ICT 3

Page 4: Ms Dos Lessons

Amen - Amen Institute

Lesson 1 – The Fundamentals of MS-DOS

Objectives.In this lesson, you will learn to:

Use the basic MS DOS Commands. Control the Computer without the mouse. Create a file using the MS - DOS editor. Copy, Rename, and move files and folders.

Information and Communications Technology - ICT 4

Page 5: Ms Dos Lessons

Amen - Amen Institute

Navigating folders and Simple Commands

Depending on whether you are using the Prompt or "true DOS", you should see a prompt similar to this:

C:\>

or this:

C:\Documents and Settings\Yourname>

This means that the journey in DOS has started.

Seeing what is in your current directory

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

The results I got in C:\ were:

Volume in drive C has no label.

 Volume Serial Number is 8C87-FFA1

Information and Communications Technology - ICT 5

Page 6: Ms Dos Lessons

Amen - Amen Institute

 Directory of C:\

04/18/2006  12:06 AM                 0 AUTOEXEC.BAT

04/21/2006  04:42 PM    <DIR>          Batch

04/18/2006  12:06 AM                 0 CONFIG.SYS

04/18/2006  12:21 AM                5 Kofi.txt

04/21/2006  07:45 PM                 0 dir.txt

04/18/2006  12:12 AM    <DIR>          Documents and Settings

04/18/2006  12:45 AM    <DIR>          Amen

04/21/2006  04:27 PM    <DIR>          Program Files

04/20/2006  08:01 PM    <DIR>          WINDOWS

               4 File(s)             32 bytes

               5 Dir(s)  196,047,323,136 bytes free

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

ORDIR /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:\>CD Amen Amen

the prompt changes to

C:\Amen Amen>

Opening filesInformation and Communications Technology - ICT 6

Page 7: Ms Dos Lessons

Amen - Amen Institute

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:

C:\>TYPE Kofi.txt | more

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.

Clearing the screen

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.

Information and Communications Technology - ICT 7

Page 8: Ms Dos Lessons

Amen - Amen Institute

To create a file using the Copy Con method, type at the prompt:

COPY CON Newfile.txt

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:

C:\>COPY CON newfile.txt

^Z

      1 file(s) copied.

C:\>

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.

COPY CON Newfile.txt

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:

C:\>COPY CON newfile.txt

^Z

      1 file(s) copied.

C:\>

Editing files Editing files is as easy as the Edit method for creating files. For repitition's sake, simply type:

Information and Communications Technology - ICT 8

Page 9: Ms Dos Lessons

Amen - Amen Institute

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.

The EDIT window toolbars:

FILE

New - Creates new file.Open - Opens another file for editing.Save - Saves with current filename.Save As - Saves with filename you choose.Close - Closes the document but not the EDIT window.Print... - Allows you to send the file to a printer.Exit - closes the EDIT window returning you to the prompt.

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

Find... - Finds all instances of whatever keyword you enter.Repeat last find - Does exactly what it says.Replace... finds all instances of a keyword or phrase, allowing you to replace them with another phrase/keyword you enter.

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

Information and Communications Technology - ICT 9

Page 10: Ms Dos Lessons

Amen - Amen Institute

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

Commands - Lists available commands in the EDIT window.About - shows the version of the editor.

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).

destination files the same as the source.

/K - copies all attributes over.

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:

MOVE [source] [destination]

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

Information and Communications Technology - ICT 10

Page 11: Ms Dos Lessons

Amen - Amen Institute

syntax (proper order/method of issuing commands) for this example is this:

RENAME Jesus.txt Amen.txt

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:

RENAME C:\Amen Amen Amen

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:

REN file.txt file.doc

The file is renamed as a different file type.

Copying filesOK, 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:

COPY [source] [destination]

So, the syntax for copying to C:\backup would be

COPY file.txt C:\Backup

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:

/A - Means the file is a ASCII file. (COPY ascii.txt C:\Backup /A)/B - Means the file is a binary file. (COPY binary.bin C:\backup /B)/V - Makes sure the files are copied correctly. (COPY file.txt C:\Backop /V(Produces error as there is no C:\Backop))/Y - "Quiet" mode; if you overwrite a file you will not be informed. (COPY text.txt C:\Backup /Y)/-Y - Makes good and sure you know if there is an overwrite. (COPY file. text C:\Backup(Causes warning if the file exists there already))

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:

Information and Communications Technology - ICT 11

Page 12: Ms Dos Lessons

Amen - Amen Institute

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 - The Next Generation

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:

/A - Copies only files with the Archive attribute.Got a ton of files with the archive attribute? OK, maybe just one or two. Use this to pick them out of the bunch./M - Copies only files with the archive attribute, but the destination copy does not have the Archive attribute; it is removed./D:Date - Copies only the files dated after the date mentioned (replace date with the date, i.e. 010104 is Jan. 1, 2004).Need to update your backups, but only the new ones? Perfect./P - Asks permission before creating each destination file.

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.

Information and Communications Technology - ICT 12

Page 13: Ms Dos Lessons

Amen - Amen Institute

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.txtDELETE test.txtERASE test.txt

Each of these will destroy the file test.txt. Gone. Permanently*.

The possible tags are:

/P - Asks before deleting each file.For those times when you really want to be sure./F - Forces read-only files to be erased as well./S - Deletes all specified files in that folder and any subdirectories with the same name./Q - Do not ask to delete on global wildcard.Normally, if you choose to "DEL *.*" (deleting all files with all extensions), you are given a prompt that says

Are you sure?(Y/N)

Using the /Q switch disables this prompt for this particular delete./A - Deletes based on attributes.DEL *.* /AR would delete only read-only filesDEL *.* /AH deleted only hidden filesDEL *.* /AS removes only system filesDEL *.* /AA erases only archives filesDEL *.* /-H erases everything but hidden files (the "-" prefix can work with any attribute)

Creating, Renaming and Removing directories/folders

It is possible to create more directories. There are two codes that do the same thing:

MKDIR [directory name]MD [directory name]

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

Information and Communications Technology - ICT 13

Page 14: Ms Dos Lessons

Amen - Amen Institute

MD Pictures

and a directory with that name would be created.

To rename a directory, use the REM command. If you misspell Pictures, say, Pictyres, you can enter the command

REM C:\Pictyres Pictures

Very Good! Directory renamed.

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:

/S - removes all files and subdirectories/Q - Used with /s: Does not prompt when removing directories/files.

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).

Information and Communications Technology - ICT 14

Page 15: Ms Dos Lessons

Amen - Amen Institute

Lesson 2 – Intermediate MS-DOS Commands

Objectives.

In this lesson, you will learn to;

Copy directories/folders(advanced) View the Internet Protocol (IP) information of your Computer Create Simple Batch files Output command to text file Implement Attributes

Information and Communications Technology - ICT 15

Page 16: Ms Dos Lessons

Amen - Amen Institute

II. Intermediate MS-DOS.

This level assumes that you are familiar with the basic functions of DOS. If you are not, please review lesson I.

Copying directories and directory trees

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 strikes again

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.

Information and Communications Technology - ICT 16

Page 17: Ms Dos Lessons

Amen - Amen Institute

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.

Simple Batch Files

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:

ECHO - displays whatever comes after the word "echo".ECHO. - Displays a blank line.REM - A comment, not parsed.@ - Modifier that hides commands. Normally, when a command is issued from a batch file, you see the text of the command, then the results. This forces a "quiet mode".PAUSE - Pauses the batch file until a key is pressed

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.

Information and Communications Technology - ICT 17

Page 18: Ms Dos Lessons

Amen - Amen Institute

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 OFFCLSDIR /WECHO 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.

Output command to a text file

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:

DIR >> log.txt

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

Information and Communications Technology - ICT 18

Page 19: Ms Dos Lessons

Amen - Amen Institute

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:

In Windows 9x and Windows ME:

+ - adds attribute.- - Removes attributeR - Read-only (cannot be modified while attribute is set)ATTRIB +R nochange.txt (adds attribute)ATTRIB -R change.txt (removes attribute)

A - ArchiveS - System fileH - 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)

In Windows 2000 or XP:Same as Win95, 98 and ME with one addition:

/D - makes all folders this attribute as well.

In Win2000 or WinXP Recovery Console:Same as above except:1. The Archive attribute is unavailable2. The /S and /D switches are unavailable3. The Compressed attribute is available (ATTRIB +C file.txt)

Lesson 3 – Advanced MS-DOS CommandsObjectives.

Information and Communications Technology - ICT 19

Page 20: Ms Dos Lessons

Amen - Amen Institute

In this lesson, you will learn to;

Format a disk using the command prompt Delete entire directory tree Use the AUTOEXEC.BAT Work with Complex Batch files Work with Graphical interfaces for Batch files.

III. Advanced MS-DOS

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

Information and Communications Technology - ICT 20

Page 21: Ms Dos Lessons

Amen - Amen Institute

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.

WARNING! FORMATTING DESTROYS ALL DATA ON THE SELECTED DRIVE! BACK UP ALL POSSIBLE DATA BEFORE CARRYING OUT THESE PROCEDURES!

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.

With Win2000 and WinXP, the options are a little different:

/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.

/C - Same as Win9x / ME./X - Forces drive to dismount (no longer be recognized by the system) if needed. This is temporary and only in effect during the format.

In the Win2000/XP Recovery console, you only have access to /Q and /FS:filesystem.

Information and Communications Technology - ICT 21

Page 22: Ms Dos Lessons

Amen - Amen Institute

So, to format C:, and make sure it's an NTFS file system, and give it a label of "WindowsXP", the command would be:

FORMAT C: /FS:NTFS /V:Windows

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.

Deleting entire directory trees

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:

DELTREE C:\Program Files\Real

WARNING! ALL FILES IN THE SPECIFIED DIRECTORY WILL BE IRRETRIEVABLY ERASED!

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

Information and Communications Technology - ICT 22

Page 23: Ms Dos Lessons

Amen - Amen Institute

you can also add to the PATH variable without causing any trouble.

NOTE TO WINDOWS > 98 USERS:The AUTOEXEC.BAT is no longer a function after Win98. The same function is accomplished in Environment Variables.

For all other OS's: Modifying AUTOEXEC.BAT

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:

FOR {switch} %%variable in (set) do [command]

Information and Communications Technology - ICT 23

Page 24: Ms Dos Lessons

Amen - Amen Institute

Switches available:

/D - Directories only; only directories are valid answers/R - Executes command in all subdirectories. Syntax is a little different:Code:

FOR /R [drive :][path] ...The ... is the normal syntax for FOR./F - used to parse a file. The syntax for it is:Code:

FOR /F "keywords" %%variable(s) in (filename) do [command]

Replace "keywords" with as many of these as you need:eol=c - End Of Line character (one character). Lines with this at the beginning are a comment and not parsed. Replace "c" with desired character.skip=n - Replace "n" with a number. n number of lines are skipped at the beginning of the file.tokens=1,2,3-7 - Specifies which lines are parsed. At least two must be there. 1,2 would do first and second lines, 1,3 would leave out every second line, etc.. The "3-7" in the example would parse every fourth through seventh line. Place a * at the end if results are not what you expect; that may fix it.

%%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.

How /F works with tokens

In our example, we have the following:

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.

Information and Communications Technology - ICT 24

Page 25: Ms Dos Lessons

Amen - Amen Institute

So, the correct code would be:

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:

FOR {switch} %%variable in (set) do [command]

Switches available:

/D - Directories only; only directories are valid answers/R - Executes command in all subdirectories. Syntax is a little different:Code:

FOR /R [drive :][path] ...The ... is the normal syntax for FOR./F - used to parse a file. The syntax for it is:Code:

FOR /F "keywords" %%variable(s) in (filename) do [command]

Replace "keywords" with as many of these as you need:eol=c - End Of Line character (one character). Lines with this at the beginning are a comment and not parsed. Replace "c" with desired character.skip=n - Replace "n" with a number. n number of lines are skipped at the beginning of the file.tokens=1,2,3-7 - Specifies which lines are parsed. At least two must be there. 1,2 would do first and second lines, 1,3 would leave out every second line, etc.. The "3-7" in the example would parse every fourth through seventh line. Place a * at the end if results are not what you expect; that may fix it.

%%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

Information and Communications Technology - ICT 25

Page 26: Ms Dos Lessons

Amen - Amen Institute

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.

How /F works with tokens

In our example, we have the following:

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.

So, the correct code would be:

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

Graphical interfaces for batch filesSo, you want to make a cool looking box to put your batch file in? You want it to look something like this?:

Information and Communications Technology - ICT 26

Page 27: Ms Dos Lessons

Amen - Amen Institute

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.

The DATE variable

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%

As of this writing, the echo'd text is:

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:

%[variable]:~[starting character],[number of characters]

For example, to just get the date without the day, you would use

ECHO %DATE:~4%

The characters start at 0, like this:

Information and Communications Technology - ICT 27

Page 28: Ms Dos Lessons

Amen - Amen Institute

IV. Reference

Credit:

Site: www.computerHopeforums.com

Information and Communications Technology - ICT 28