Common MS-DOS Commands

Command

Purpose

Switches or example usage

ATTRIB

displays or changes file attributes

o      +h hides a file (attrib +h chicken.txt)

o      -h shows a hidden file (attrib –h *.* shows all hidden files)

o      +r makes a file read only

o      +s makes a file a system file (hides it even better than hiding)

o      +a turns on the archive bit (so when a backup is done, the file will be backed up)

o      Can nest them attrib +r +s makes a file a system file that is read only

CHDIR or CD

displays the name of the current directory or changes the current directory

o      Cd.. moves up one directory

o      Cd chess moves you into chess. Note: the chess directory MUST be in the directory you are currently residing in

CHKDSK

checks the status of a disk and displays a status report; it can also fix disk errors. Use only with DOS and Windows 9x.

o      /f checks for errors and fixes them (ex: chkdsk c: /f)

o      >logfile.txt creates a log of all errors found that can be printed out (rather than just showing on the screen) (ex: chkdsk c: >log.txt)

o      /v displays all path and filename information for all files on the disk

CLS

clears the screen

 

COPY

copies one or more files to a destination you specify

C:\ copy fred.txt a:\

C: copy c:\docs\*.* a: copies all docs in that folder to the A drive.

C:\windows\copy system.ini system.bak makes a copy of system.ini and names it system.bak

DEBUG

Runs the debug program and editor to view and manipulate the components of a file system on floppy and hard drives, including the FAT, directories, and boot records. You can also use it to view contents of memory and hexadecimal memory addresses.

o      Enter the command DEBUG at the command prompt.

DEFRAG

Defragments fragmented files on a hard drive.

o      /S:N sorts the files by name on the disk

o      /S:D sorts the files by date and time on the disk

DELTREE

deletes a directory and all the files and subdirectories that are in it. Be very careful with this command!!

o      C:\deltree would delete all of C:\

DIR

displays the files and subdirectories that are in the directory you specify

o      Dir /p—list one screen at a time

o      /w use wide format

o      *.txt use a wildcard character

o      Dir myfile.txt checks to see if that file is present

DISKCOPY

copies the entire contents of one floppy disk to another floppy disk. It writes over the existing contents of the destination disk as it copies the new information on it.

o       

ECHO

displays or hides the text in batch programs when the program is running.

o       

EDIT

starts MS-DOS Editor, a text editor you can use to create and edit ASCII text files.

o      Edit a:\autoexec.bat

DEL or ERASE

deletes the files you specify

o      Del *.txt deletes all txt files

o      Del *.* deletes all files

o      Del tory.* deletes all files that start with Tory

EXIT

quits the MS-DOS command interpreter and returns to the program that started it, if one exists.

o       

FASTHELP

displays a list of all MS-DOS 6 commands and gives a short explanation of each.

o       

FDISK

Used to prepare hard drives for first use. Creates partitions and logical drives on the hard drives. Displays partition information, and also used to restore a master boot record.

o      /mbr restore master boot record

o      /status shows installed partitions

FIND

searches for a specific string of text in a file or files

o       

FORMAT

formats a disk for use with MS-DOS

o      /s Stores system files on disk after formatting (io.sys, msdos.sys, and command.com)

o      /v:volumename allows you to assign a volume name (ex. Format /v:happy)

o      /q recreates the root directory and FATs if you want to qucily format a previously formatted disk that is in good condition

o      /F:size specifies size of floppy. (ex. Format f:360 to format it as a 360k floppy)

o      /u allows an unconditional format which completely erases the drive, not just the FAT

o      /autotest does not prompt the user before and during the format

HELP

starts MS-DOS Help

o       

MKDIR or MD

creates a directory

o      Mkdir c:\chicken

MEM

displays the amount of used and free memory on your computer

o       

MORE

displays one screen of output at a time

o       

MOVE

moves one or more files to the location you specify

o       

MSBACKUP

backs up or restores one or more files from one disk onto another

o       

MSD

Provides technical information about your computer

o       

PRINT

prints a text file while you are using other MS-DOS commands

o       

QBASIC

starts MS-DOS Qbasic

o       

RD or RMDIR

deletes a directory

o       

REN

changes the name of the file or files you specify

o      Ren tory.txt mrsk.txt

RMDIR or RD

deletes a directory

o      The directory can only be deleted if there are no files in the directory

o      If the directory contains another directory, that other one must be removed first

o      Example: c:\chicken\lips

o        C:\chicken rd lips

o        Cd..

o        C:\ rd chicken

SCANDISK

starts a disk analysis and repair tool that checks a drive for errors and corrects any problems that it finds. Use with NT, 2000, and XP

o      /p displays information and fixes errors

o      /n starts and stops it automatically (you could put it into a batch file and have scandisk run on boot or shutdown using this command)

SCANREG

Scans and restores Windows 98 registry

o      /restore restore the registry from a backup

o      /fix scans and fixes common problems

o      /backup backs up registry-good if you’re going to make some registry changes and aren’t sure if they’ll work

TREE

graphically displays the structure of a directory

 

UNDELETE

restores files that were deleted previously by using the DEL command

o      Only recovers if you used the DEL command

UNFORMAT

Might be able to reverse the format command…maybe…if you’re lucky. Used if a used disk was formatted by accident.

 

VER

displays the MS-DOS version number

 

XCOPY

copies directories , their subdirectories, and files except hidden and system files. Not available on a DOS boot disk unless you add it.

o      /c continues copying even if there are errors

o      /s copies all files and subdirectories

o      /y overwrites existing files without asking you

o      /-y turns off overwriting without asking

o      /h copies hidden and system files

 

Using Batch Files

 

Suppose you have a lis of OS commands that you want to execute several times. You can create them to run certain programs, map hard drives on a network, install printers automagically…and do all sorts of evil stuff. We, however, will not do evil stuff in here…will we Frank?

 

Batch files are simply text files created in notepad or even in the edit program in DOS. The only difference is, when you save them you save them with a .bat extension. If you then double click the file, it will run your commands.

 

Let’s practice:

 

Creating a BATCH File

First open an explorer window to your c: drive, using Windows Explorer or 'my computer.' Arrange the window so you can see both your desktop and your c: drive contents.

Open the notepad application by going to 'start\all programs\accessories\notepad' or 'start\run' and type 'notepad'.

In the blank notepad window, type:

md c:\testsource
md c:\testbackup

Now go to 'file' and 'save as'.
(in case you did not read our guide to the command prompt, the 'md' command instructs the system to create a directory
using a name and location following the command.)

Save your first batch file on the desktop as 'myfirstbatch.bat'.

Close notepad and you'll see that 'myfirstbatch.bat' has appeared on the desktop. Double click the file to run it. Check your c: window. The 'testsource' and 'testbackup' directories have appeared. Your first simple batch file is a success! Delete the 'myfirstbatch.bat' file from your desktop. Beautiful!

Creating your second batch file

Now to create a batch file to backup these files into your c:\testbackup directory automatically. Open up notepad and type the following:

@echo off
xcopy c:\testsource c:\testbackup /m /e /y

The '@echo off' line tells the computer not to display anything onscreen when it runs this batch file.

The second line uses the xcopy command to copy all contents of the c:\testsource' directory to c:\testbackup the first time the batch file is run. The second time and all remaining times, it will only copy new files and files which have changed since it was last run. It will not copy unchanged files which it previously copied, even if you delete the copies it made from the c:\testbackup' directory.

Now save your batch file as 'testbackup.bat' on your desktop and double click it to run the script.

Check the contents of your c:\testbackup directory. It should now have copies of the two files you created in c:\testsource. Good stuff. Now open 'testdoc1' in your c:\testsource directory and add some text then save it.

Run your testbackup.bat batch file again, and go to the 'testdoc1' file in the c:\testbackup folder. It should have been updated with the changes you made in the other folder.

You've now created a useful backup utility with a simple two-line batch file that just takes a double click to run. Starting to see the potential usefulness of knowing your batch files yet?

Third trial batch file: getting fancy

Now that we've seen some of the extra commands that can be used in batch files, let's play with one of the most powerful of them, the FOR command. In this case, we're going to alter our simple backup batch file and make it a bit more sophisticated. It's going to differentiate between two different types of files (text/Word documents and pictures) and back each file type up to a different directory. To set up for this we need to create two more directories in c:\. Call them

C:\Text
C:\Pics

Delete the existing text and .bmp files in your c:\testsource directory and create a couple of new versions of each.

Now open notepad and enter the following:

@echo off
cd c:\testsource
for %%f in (*.doc *.txt) do xcopy c:\testsource\"%%f" c:\text /m /y
for %%f in (*.jpg *.bmp *.gif) do xcopy c:\testsource\"%%f" c:\pics /m /y

Now this is a bit more complicated than the files we did before, so let's take a close look at what this batch file is going to do.

cd c:\testsource

Tells the computer that the directory we are going to be working in is c:\testsource

for %%F in (*.doc *.txt) do xcopy c:\testsource\"%%F" c:\text /m /y

This line tells the computer that FOR any file with the .doc or .txt file extension (meaning any standard Word doc or text file), DO an xcopy command to copy that file to the c:\text directory using the same options we used in the last batch file. The confusing looking '%%F' character represents the variable that the FOR command uses to carry out this operation. For example, if your first text file in the c:\testsource directory is 'texttest1.txt', the batch file would look at it, see that it had a .txt extension and assign it as the value of '%%F'. The second part of the command

do xcopy c:\testsource\"%%F" c:\text /m /y

takes whatever %%F is (in this case your 'texttest1.txt' file) and copies it to the c:\text directory. The quotation marks around %%F are to allow the command to deal with file names containing spaces. The command then loops until it has looked at every file in the current directory before moving on to the next part of the batch file.

for %%F in (*.jpg *.bmp *.gif) do xcopy c:\testsource\"%%F" c:\pics /m /y

The only thing that is different here is that we are looking for graphics file extensions instead and copying them to the 'c:\pics' directory.

Save your third batch file on the desktop as 'trickybackup.bat' and try it out. You'll see that your newest creation neatly differentiates between text documents and pictures and splits them up accordingly.