Dos Commands





ABOUT DOS  (Disk Operating System)


        Before going to know about DOS we should know about operating system. An operating system is a software program that acts as an interface between user and a computer. 

        MS DOS is an example of a single user operating system that has a Command Line Interface .MS DOS was developed by Microsoft and was considered the de-facto single user operating system for a long time. Even today, most of the operating systems provide emulators for MS DOS. DOS is called a command line interface operating system because it is textual in nature.

        A Graphical User Interface (GUI) displays text as well as icons and images. GUI based software is slower than CLI software but is more user friendly. Ex: windows

--------------------------------------------------------------------------------------------

DOS Commands List



1. CREATING A FILE:


C:\> COPY CON FILENAME.EXTENSION

Extension is not compulsory for files but it improves readability by specifying whether a file is a text file, c programming file, batch file etc.,

2. VIEWING A FILE:


Contents of a file can be viewed by the command TYPE as follows

C :\> TYPE FILENAME

Or

C :\> TYPE DRIVE:\DIR\FILENAME      //if the file is in a different drive

Output can be redirected to file using type filename filename (Input file) (Output file) It has the same effect as copying one file to another.

3. DELETING A FILE:



C :\> DEL FILENAME

We can also delete group of files by using wild cards (* and?) as follows

C :\> DEL *.*            // deletes all the files in the current directory

C:\> DEL *.TXT             //  deletes all the text files in the current directory.

4. RENAMING A FILE:


C:\> REN OLDFILE NEWFILE.

5. MODIFYING TEXT IN A FILE:


Data can be appended at the end of the file using ECHO command as follows

C :\> ECHOES STRING >> FILENAME  string is the text we want to insert at the end of the file.

Using echo inserting large text is difficult .To insert more no. of lines at a time but only at the end of file dos provides another command

C :\> TYPE CON >> FILENAME

NOTE: “> >” is called concatenation symbol

However using echo, type commands we cannot insert text at our required positions in the file. Also we cannot perform any operations like removing text, selecting, copying text etc., This can be achieved in dos using the EDIT command as follows

C :\> EDITS FILENAME

We must save the file to keep the changes permanent.

6. ATTRIB:


This command is used to set attributes for a file(R for Read Only, H for hiding/unhiding etc.)

C :\> ATTRIB +R FILENAME

This attribute sets the file to be read only .It implies that we cannot add, modify text, delete text into it.

C :\> ATTRIB -R FILENAME

Using this command we can remove the read only property of the file therefore perform all the operations like appending text, deleting text etc.,

C :\> ATTRIB +H FILENAME

Using this command we can hide the file. It will not be displayed even if we use dir

C :\> ATTRIB -H FILENAME

Using this command we can unhide the file. It will be displayed now if we use dir.


7. CREATING A DIRECTORY:


Directory is nothing but group of files related to a particular user or task. Directory can be created using the commands MD or MKDIR as follows 

C:\>MD DIRECTORYNAME or md path or MKDIR DIRECTORYNAME

Ex: 1 md nd   // creates a directory named nd

Ex: 2 md c:\nd\p1 creates a subdirectory directly from the c prompt

8. MOVING INTO A PARTICULAR DIRECTORY:


This can be done using the commands CD or CHDIR as follows

C:\>CD DIRECTORYNAME

Or

CHDIR DIRECTORYNAME

We can move to a subdirectory directly instead of using CD command twice or thrice etc., by using the following command

C :\> CD DIRECTORY\SUBDIR1\SUBDIR2\……

We can move to the previous directory from current directory using the command

C :\> CD...

We can move to the C drive directly without using cd... Twice or thrice by using the command

C :\> CD\

9. DELETING A DIRECTORY:


C :\> RD DIRECTORYNAME

A directory can be deleted only if it is empty and can be deleted from it’s root or parent directory which contains our directory. so we must first delete the files in the directory by moving into that directory And then move back to it’s parent directory and delete the directory there. This is explained with the below example

Ex: C :\> md nd

C :\> cd nd

C:\nd> copy con s1.txt (create and save the file)

C:\nd> Del s1.txt

C:\nd> cd...

C :\> rd nd

10. DELTREE COMMAND:


Deleting a directory is very tedious and time consuming particularly if The directory contains subdirectories and many files because in order to Delete the root directory we have to delete first all the files and Subdirectories and then delete the root directory. To make this process Easier dos includes a command called DELTREE which is very much Useful and can directly delete the directory and it’s subdirectories at a time. The command is used as follows

C :\> DELTREE ROOTDIRECTORYNAME.

11. COPY COMMANDS:


A. CREATING A FILE


In dos a file created indirectly using copy command by copying data from the console keyboard using the following syntax.

C :\> COPY CON FILENAME.EXTENSION

Note that the extension is not compulsory. We can create the file without any extension. But we must use extensions to easily identify what type the file is.

Ex:    .txt implies a text file
         .C implies a c file
         . Exe implies an executable file

B. TO COPY A FILE INTO ANOTHER FILE COMMAND IS


C:\>COPY FILE1 FILE2

C. TO COPY A FILE FROM PRESENT DIRECTORY TO ANOTHER DIRECTORY


C :\> COPY FILE1 C:\DIR1\FILE2

D. TO COPY A FILE FROM PRESENT DRIVE TO ANOTHER DRIVE


C :\> COPY FILE1 D:\DIR1\FILE2 (since file1 is in currently in c drive no need to specify c drive before file1)

OR

C :\> COPY D:\DIR1\FILE1 E:\DIR2\FILE2

E. TO COPY A FILE INTO A FLOPPY (A: specifies floppy drive)


COPY FILE1 A:\FILE1

F. TO COPY FILE FROM FLOPPY


COPY A:\FILE1.TXT C:\FILE2.TXT

OR

COPY A:\ C:\

G. COPYING M FILES INTO A SINGLE FILE


COPY FILE1+FILE2+…FILEM FILEN (Here file n contains the data of all the m files)

H. COPYING USING WILDCARDS


COPY C:\*. * D:\ND\

Copies all the files in c:\ in to the nd directory in d drive

I. USING XCOPY


XCOPY command is used to copy group of files at a time.

XCOPY C:\DIR1 D:\DIR2

This command is similar to using copy c:\dir1\*.* d:\dir2 the difference is clear when we use xcopy with /s option .This is amore powerful command because it copies entire sub directories also , which is not possible with ordinary copy command 

The syntax is

XCOPY C:\DIR1 D:\DIR2/S

This command copies not only the files in root directory (dir1) but also copies all the files in subdirectories also except empty directories.

XCOPY C:\DIR1 D:\DIR2/E

This command copies not only the files in root directory (dir1) but also copies all the files in subdirectories also including empty directories.

12. MOVE COMMAND:

The basic difference between copy and move commands is that copy command simply creates a copy of the original file where as move command moves the entire file .The old file would be removed with move command. Almost (but not all) the commands that are possible with copy command are the same with move command. Practice the above commands with move also and observe the difference.


13. FIND COMMAND: (FILTER)


Filter is nothing but a program, which reads some text and writes some text by converting into our required form. Find command is a filter that can be used to search for a string of text present inside the file. Input should be given with <filename and output as >filename

FIND “STRING TO BE SEARCHED” FILENAME Or

FIND “STRING TO BE SEARCHED” <FILENAME

It displays all the lines that contain the string. Data which is found using the above command can be outputted to another file using >filename as follows

FIND “STRING TO BE SEARCHED” <FILENAME >FILENAME
(Inputfile) (Outputfile)

Search can be done in multiple files at a time using

FIND “STRING TO BE SEARCHED” FILE1 FILE2 ….. FILEN

FIND COMMAND has few options they are:

A. FIND /C “STRING TO BE SEARCHED” FILENAME

It displays the number, which specifies in how many lines the string is found.

B. FIND /N “STRING TO BE SEARCHED” FILENAME

It displays the line numbers along with the text in which string is present.

C. FIND /V “STRING TO BE SEARCHED” FILENAME

It displays the lines where the string is not found

D. FIND /I “STRING TO BE SEARCHED” FILENAME

It searches the string ignoring the case (upper or lower) of it.

14. SORT COMMAND: (FILTER)

This command is used to sort text in a file alphabetically 

C :\> SORT   //press enter and give the text

G
K
L
A
C // press F6 or ^Z and then press enter to save the file
A
C
G
K
L

We can sort a file, which contains some text as follows

C :\> SORT FILENAME

If we want to save the sorted text in another file we can do it as follows

C :\> SORT <FILE1.TXT> FILE2.TXT

Sorted text will be saved in file2.txt

Sort command is a filter for which input should be given with “<” symbol called as input redirection and output with “>” symbol called as output redirection.

15. DIR COMMAND:


Dir command displays all the subdirectories and files in a directory

C:\>DIR

Output can be sent into another file using DIR > FILENAME

DIR command when used with /S option displays the files present in subdirectories also which is not possible with DIR

C :\> DIR/S

There are many options in dir which can be at the c:\> prompt by typing

C :\> DIR/?

A few of them are:
1. Dir/as to view all the system files.
2. Dir/ah to view the hidden files.
3. Dir/ad to view only the directories.
4. dir/a-d to view only the files

16. DATE COMMAND:


DATE command displays the date and asks to enter the new date.

17. TIME:


TIME command displays current time and asks to enter new time.

18. VER:


Displays the version of windows

19. CHKDSK:


Displays the memory details like available or free space, allocated space, used space etc.

20. SCANDISK:

Scans the drives for errors

21. DOSKEY:

C :\> DOSKEY  //(press enter)

Doskey installed //this message appears

This command is very useful while using DOS commands this can be used to see the previous commands we have used using UP ARROW key in cursor movement keys. It also reduces the typing time.

Ex: C:\>COPY CON ND1.TXT (creating a file)

Now i want to create a file named nd2.txt, I can simply move the up arrow it appears like this C :\> copy con nd1.txt i can simply change the “1” number with “2” to create the file nd2.txt without again typing the whole command. So it is advisable to first install doskey whenever we log into the system. We can also install doskey automatically without manually typing doskey every time we log into the system by typing DOSKEY/INSERT in the Autoexec.bat using c :\> edit autoexec.bat Autoexec.bat is a batch file, which is automatically executed every time the system is booted or started. It contains necessary paths that are needed to use various software Note: We can see our most recently used command using F3 key.

22. ECHO:


This command is used to echo or reply the same text to us at the console

C :\> ECHO Neeth  // type enter key

Neeth

23. CLS:


This command clears the screen.

C :\> CLS //Press enter key

24. WILDCARDS:


* , ? Are called wildcards the use of these can be observed by seeing the following examples.


Ex 1: To see the files that are having only .txt as extension

C :\> DIR *.TXT

Ex 2: To see the files that are having only .bat as extension

C :\> DIR *.BAT

EX 3: To see the files whose names start with N(say) the command is

C :\> DIR N*.* //First * indicates there can be any no of char’s after N

// Second * indicates the extension of the file ca be anything

EX 4: To see the files that contains two char’s only the command is

C :\> DIR ??.* // ? Indicates a single char therefore 2 ? are used .

EX 5: To see the text files those have a 0(zero) as the second character of their name

C :\> DIR *0*.TXT.

25. TREE:


This command is used to display the directory structure

C :\> TREE ND /F

This command displays directory structure of nd graphically with all sub directories and all files also.

C :\> TREE ND\P1 /F

This command displays directory structure of P1 subdirectory in ND graphically with all sub directories and all files also.

26. PROMPT:


This command displays our own prompt instead of the default prompt c:\

C :\> PROMPT ND: \

ND: \  //ND:\ appears instead of C:\

There are various options for displaying various prompts instead of C:\

a) C:\> PROMPT $D // Displays current date as the prompt

Ex: mon 10/10/ 05
b) C:\> PROMPT $T //Displays the current time as prompt

Ex: 23:15:39.88
c) C :\> PROMPT $N //Displays the current drive as prompt

Ex: C
d) C:\nd>PROMPT $P //Displays the current path as prompt

Ex: C:\nd
27. MORE COMMAND: (FILTER)

This command can be used to see files that contains text which cannot be seen at a time on the screen by typing as follows

C:\>MORE <FILENAME

It waits for a keystroke after displaying the first page to display the next page.

Also more ca be used as

C :\> TYPE FILENAME | MORE    // | is called pipe symbol

28. LABEL:

This command is used to change the labels of the drives

C:\>LABEL   //press enter key

It displays the current label and asks for the new one.

29. REPLACE:

Replace command is very useful when we are working with large number of files, only a few of which change at a single work session. Replace command is useful when we are working with on the same data files in two different locations. Replace has several command-line options but the most useful one is the /U option, which replaces files on the target only if they are older than the matching files on the source.

C :\> REPLACE *.DOC A: /U

Diskette has now the latest files .you go home and transfer the files to the hard disk as follows

C :\> REPLACE A:*.DOC C: /U

The same result can be obtained with COPY *.* but every file would always be copied, which wastes time. Where as replace command copies only the latest files by comparing the dates.

30. BACKUP and RESTORE:


The backup and restore commands are intended to allow you to make backups of and restore large volumes of files onto multiple diskettes.

C :\> BACKUP C:\*.* A: /S

To restore those files to the hard disk, you would enter this command:

C :\> RESTORE A: C:*.* /S

31.DISKCOPY:

This command is used to copy all the contents in one floppy to another one.

C:\>DISKCOPY A: B:

This command copies from one floppy to another floppy if there are two

Floppy drives.

If there is only a single floppy drive then replace B: with A:

C :\> DISKCOPY A: A:

In this case first it will ask for source floppy and after sometime it will ask for target floppy.

32. WRINTING FILES:


We can print files, txt etc., by redirecting the output to PRN device.
Ex: 1 dir > prn

This command prints the list of directories and files on a paper in printer.
Ex: 2 type nd.txt >prn

This command prints the nd.txt file.
Ex: 3 Copy nd.txt >prn

This command also prints the nd.txt file
In all the above commands we are just redirecting to a printer.

33.COLOR:

This command can be used to change the color of console output.

Syntax: COLOR [ATTR]

Color attribute are specified by two hex digits, the first digit specifies background and the second digit specifies foreground. Each digit can be any one of the following:

0---black 8---grey
1---blue 9---light blue
2---green 10--light green
3---aqua 11--light aqua
4---red 12--light red
5---purple 13--light purple
6---yellow 14--light yellow
7---white 15--bright white

34. TITLE:

This command sets the title for the command prompt window

Syntax: Title string

Ex: C :\> title neeth

neeth appears on the top of the window

35. HELP:

This command is used to get help from the dos about a dos command

Syntax : Help command

Ex: c:\> help dir   //  It displays the details of dir command 

Help can also be obtained using
Syntax : command/?

Ex: dir/?   // Displays the possibilities in dir command

36. EXIT:

To exit or come out of dos.

37. IF:

IF [NOT] STRING1==STRING2 COMMAND
IF [NOT] EXIST FILENAME COMMAND
[NOT] means not is optional
The use of IF construct can be illustrated by examining the below examples

Ex: 1 if nd==nd then echo the string are equal
The above command compares the two strings and if they are same it echoes the message “strings are equal”

Ex: 2 if nd==neeth then echo strings are equal
The above command compares the two strings and since they are not same it will not display the message.

Ex: 3 if not nd==neeth then echo strings are not equal
The above command compares the two strings and since they are not same it will display the message “strings are not equal”

Ex:4 if exist me.txt del filename
This is another case of if in which DOS checks whether the given filename exists and if it exists then it deletes that file.

Ex:5 if not exist me.txt copy con me.txt
In this case the do checks the same as above and if the file is not found then it creates the file taking data from keyboard.

38. IF-ELSE:

IF [NOT] STRING1==STRING2 (COMMAND) ELSE (COMMAND)
IF [NOT] EXIST FILENAME (COMMAND) ELSE (COMMAND)

Ex:1 if nd==nd1 ( echo strings are equal ) else ( echo strings are not equal ) 
The above command compares the two strings and since the two strings are not equal else part executes and displays the information strings are not equal.

Ex:2 if exist sample.txt ( del sample.txt ) else ( copy con sample.txt )
The above command checks if the file named sample1.txt exists or not and performs the commands given in the statement accordingly.

We can also compare using the following operators

1) EQU for equal
2) NEQ for not equal
3) LSS for less than
4) LEQ for less than or equal
5) GTR for greater than
6) GEQ for greater than or equal

Ex:1 if nd equ nd echo strings equal.
Since nd equals nd message will be displayed

Ex:2 if 4 geq 3 copy con nd.txt
This command compares 4 and 3 and since 4 is greater than 3 it creates a file named nd.txt

39. FOR:

For construt can be used in dos as follows
FOR %VARIABLE IN (SET) DO COMMAND
Variable is nothing but the one that would be replaced later in the execution of the instruction It can be understood clearly by observing the following examples

Ex: 1 for %m in (a1, a2, a3) do copy con %d
This command creates three files named a1, a2, a3

Ex: 2 for %r in (a1, a2, a3) do edit %d
The above command opens the three files a1, a2, a3 for editing text

Ex: 3 for %e in (a1, a2, a3) do del %d
The above command deletes the three files a1, a2, a3

Ex: 4 for %c in (a1, a2) do echo % is created
The above command displays the messages a1 is created and a2 is created

Ex: 5 for %s in (1, 4) echo %d
The above command displays 1 and 4

Ex: 6 for %d in (*.*) do type %d
The above command displays all the files at a time

40. MODE:

This command can be used to increase the size of the screen.

Ex: Mode 40
Mode 80 …………
This command can be used if we are not in full screen.

41. SEND:

This command can be used to send messages to any user
Syntax: SEND “MESSAGE” TO USERNAME

Ex: send “good morning” to user4.

42. RUNNING BATCHFILES:

Batch files are system files which can be used to automatically execute a group of commands automatically.
Autoexec.bat is an example of batch file which is automatically executed when the system is started.
We can write our own batch files by creating files with .bat extension.

Ex:1
C:\> edit nd.bat         //opens the file nd.bat now type the following commands
Echo welcome to Dos
Copy con %1
Type %1
Mkdir %2
Cd %2
Del %1                  // now save the file and return to dos
C:\> nd p dir1       // p replaces %1 and dir1 replaces %2

Now the execution of the above command results in

a) creation of a file p
b) typing the contents of p
c) creating directory named dir1
d) movingintodir1
e) deleting the file p

we are supplying arguments(p , dir1) for the batch file at the command prompt called command line arguments. We can also use if, if-else, for etc., in batch files as for our requirement.

Ex 2: edit nd1.bat
cls
Type %1
Shift
Type %1
Pause
Copy con %2
If exist %2 goto end
Copy con %3
:end
C:\> nd p q r
shift command is used here to decrease the no of variables used for example we give %1 for two variables p and q supplied as command line arguments and r will be used for %2.since file r is now created and exists the if condition will be satisfied and %3 file will not be created pause command will be used to wait for a keystroke and then proceed.


Comments