0% found this document useful (0 votes)
19 views

Week 11

The document provides information about using the Windows command line interface (CLI), also known as the command prompt. It discusses the history and origins of DOS and the CLI in Windows. It explains how to start the command prompt and navigate directories and drives using commands like CD, DIR, COPY, DEL etc. Detailed examples are given showing how to use these commands to create, view, move and delete files and folders from the CLI.

Uploaded by

ngyanchi11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Week 11

The document provides information about using the Windows command line interface (CLI), also known as the command prompt. It discusses the history and origins of DOS and the CLI in Windows. It explains how to start the command prompt and navigate directories and drives using commands like CD, DIR, COPY, DEL etc. Detailed examples are given showing how to use these commands to create, view, move and delete files and folders from the CLI.

Uploaded by

ngyanchi11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Windows Command Line

DOS (Disk Operating System)


• Brand name of the first OS used on IBM personal computers
• DOS was a product bought by the young Microsoft corporation
when Bill Gates was 25 years old and rebranded as a Microsoft
product
• DOS used a CLI system
• Many people call the CLI in Windows the DOS box
• Also called the Command Prompt
• Console is the proper name of the CLI in most other OSs such as
Linux
Commands
• Anything that can be done in a GUI can be done in a CLI
• Any program can be run from the CLI
• In a number of cases it is faster to issue a command in the CLI then
to use the mouse in a GUI
• In the event of serious problems it is possible to make repairs to a
system using the CLI
• The Windows Repair Environment can be run in Command Prompt
Starting the Command Prompt
• Select the Start menu (windows Icon bottom left corner in Windows 10)
then select the Command Prompt Tile
Or
• In the Search box enter “cmd” or command and then select Command
Prompt App from the pop up list
Or
• Windows Key + r and type cmd in the dialog box
Or
• Windows Key +x, then select run from the menu and type cmd in the dialog
box
Command Prompt Window
• The Command Prompt
Window is very simple and
has a prompt to let you
know that it is waiting for
your command
• The actual prompt normally
shows which level of the file
system you are currently
looking at. (I.E. it presents
you with a path and a
greater than symbol)
The Path
• The path is the location on a disk drive
• Many commands use the path to determine where to look for information
• For example: C:\Users\mburg
• This path starts at the C: drive, the first \ represents the root directory of the C: drive,
Users represents a folder in the root directory, the second\ represents opening
another sub folder, which in this example is the mburgess folder
• This is known as an absolute path or full path as it shows all the levels from the root
directory of a specific drive down to a lower level folder
• Unlike the Windows Desktop or file manager the command prompt can only see one
folder at a time
• Commands you type without specifying a path affect the current folder (relative path)
unless you specify a new path (absolute path) inside the command
Example of absolute path Current location Command with full path

• Typing the command


dir c:\users\mburg
from any location will
result in you getting a
list of all the files and
folders inside that
specific folder.
Example of relative path Current location Command with no path

• Typing the command


dir without a path
from a location will
result in you getting a
list of all the files and
folders in the current
location
Command parameters
• Some commands need the names of files or directories
• copy myfile.txt C:\temp
• Copy the file myfile.txt from the current location to the folder at path C:\temp
• type myfile.txt
• Display the contents of the myfile.txt file at the current location
• Programs can also have parameters
• notepad commands.txt
• Open notepad with the file commands.txt which is in the current location
• Most of these commands are using relative paths. If we do not specify a full (absolute) path the
system completes the command assuming that all paths start at the current location. (The only
exception in the above examples is the destination of the copy command where we specified
the full path C:\temp
Command Syntax
• Verb {Source path/file} {Destination path/file} {switches}
• Verb is the command we want to do
• Source path/file is the file we wish to work with
• Destination path/file is where we want to send our processed data
• Switches are special characters that will allow us to modify the way a
command functions
a b c
• Example: Copy grandma.txt c:\letters /v
a) Copy the file named grandma.txt found at the current location (relative path)
b) to the C: drive inside the folder letters in the root directory of C: (absolute path)
c) Verify that the files are the same after the copy
Some Command Prompt Commands
(DOS)
• md / mkdir Make directory
• rd / rmdir Remove directory Modify/navigate directory (folder) structure
• cd / chdir Change directory
• copy Copy a file from one location to another
• move Move a file from one location to another
• del Delete a file
• fc Compare the contents of 2 files
• cls Clear Screen
• help Displays the details of the specified command. EXAMPLE: help copy
• You can find a complete list of command at the following web page
http://technet.microsoft.com/en-us/library/cc754340.aspx
Some command examples
• dir command shows contents of
c:\users\mburg\examples
• When command is finished the
prompt appears to let you know
the system is waiting for a new
command
Some command examples continued
• cd folder1 command changes
the current location to c:\
users\mburg\examples\
folder1
• The new prompt reflects the
change in current location
Some command examples continued
• dir command shows contents
of current location (folder1)
• md folder2 creates a new
folder inside the current
location
• dir command shows the
contents of current location
(folder1). Note that folder2 is
now listed in the contents
Some command examples continued
• copy a file using absolute path
for source and relative path
for destination
• Command line responds with
“1 files copied”. Most
command do not tell you if
they completed or not they
just happen and then a new
prompt is displayed
Some command examples continued
• If you want to copy a file into the same
location with a new file name we change
the destination parameter
• In this example we use a relative path for
both the source and the destination but
supply a new name for the destination
• We can also do this if we copy a file to
another location and want to assign a
new name to it
• The second copy command uses a
relative path for the source and an
absolute path for the destination with a
new filename at the end of the path
Some command examples continued
• dir command to display
contents of current location
(folder1)
• Notice that there are now 2
entries; folder2 and
simpletext.txt
Some command examples continued
• Use the type command to
display the contents of the
simpletext.txt file stored in
the current location
Some command examples continued
• Use the del command to delete folder2
from the current location
• Command prompt responds with are you
sure. Notice that this line show a * at the
end of the displayed path
• We respond with y
• Using the dir command to display the
contents of the current location shows us
that folder 2 is still there
• del command can only delete files; when
the system asked if we were sure, the path
it displayed ended with the * and was
actually asking if we were sure that we
wanted to delete all of the files in folder2
Some command examples continued
• Use the rd command to
remove folders
• This command will only work
if the folder is empty as you
cannot remove a folder that
has files or folders inside it
• The dir command results now
shows that folder2 is gone
Some command examples continued
• cd.. Command tells the system
to change the current location
to the parent directory (ie 1
level up)
• The new prompt reflects the
change in location
Some command examples continued
• dir command shows that
folder1 is in the examples
folder (current location)
• rd folder1 causes an error
message stating that the
directory is not empty
Some command examples continued
• Use del command to delete
simpletext.txt
• System responds with a file not
found error because our current
location is c:\users\mburg\examples
and the file does not exist here
• Use cd folder1 to change location
• The use the del command to delete
the file
• The dir command shows that the file
is gone
Some command examples continued
• To change your location from one drive
to another you cannot use the cd
command. It can only be used to
navigate the structure of the current
drive
• The command does not generate an
error message but you can see by the
new prompt that nothing changed
• To change your location to a new drive
simply type the letter of the drive
followed by a colon.
• In the second image you can see the
command and the resulting new prompt
Some command examples continued
• When you change from one
drive to another the location
you end at will normally be the
last path that you used on that
drive
• You can see that moving to the
e: drive brought us to the root
directory of e: . Going back to
the c: drive our prompt shows
us that we are at the folder1
location
Some command examples continued
• So far we have always used
the relative path when using
the cd command. We can also
use an absolute path but we
must use the current drive as
our starting point.
Some command examples continued
• When we understand the file
structure we can also use our
relative path with the parent
folder notation to navigate
• In the example to the left we
are changing to the parent of
the current location, then to
the parent of the new location
and then to the parent of the
new location.
Some command examples continued
• If we use a relative path that
starts with a \ the system always
assumes that we want to start
from the root directory level
• Typing cd \ will take us to the
root of the current drive
• When we type cd \users the
system starts at the root and
searches for a folder called users
Some command examples continued
• The copy command can also be used to
create text files.
• The procedure is to type copy con
filename.txt, press enter, then type the text
that you want inside the file, when done
press the ctrl key and the z key at the same
time, then press enter
• The only thing you have to understand is
that this will only work in a folder that you
have permissions in. Example your user
folder.
• Con (a special environment variable)
becomes the source for the command and
represents the keyboard, mytextfile.txt is
the destination, and the ctrl-z indicated that
you are finished entering data at the console
Some command examples - redirection
• Another way to create a file is with redirection. We
can send the results of a command to a file.
• In the top example we are using the echo command
to send some text to the default output device (the
screen). You will note that the text is immediately
repeated after the command and before the system
returns with its ready prompt
• In the bottom image we are using the echo
command with a redirection symbol (>) to change
the destination of the output to a file. If the file does
not exist it will be created. If the file already exists it
will be rewritten. The text is not duplicated on the
screen but sent directly to the specified file
• If you do not want to overwrite the file you can use
the append redirection symbol >> to have the new
data added to the end of the existing file. If the file
does not exist it will be created.
Some command examples - redirection
• We can redirect the output of
any command to a file
• In the example to the right we
are sending the output of the dir
command to the burgessFolder
file
• A quick dir of the current
location shows that the file
exists
• Using the type command we can
look at the contents of the file
Some command examples - piping
• Some commands require input from another command
• Having the output of one command become the input for a second
command is called piping
• We use the straight line symbol | when we want to pipe output
from one command into another
• dir | more This command will take the output from the dir
command and send it to the more command which will then send
the output to the screen, one screen at a time, pausing between
screens to wait for the user to press a key to continue
Using Wildcards when Selecting Files
• The Windows Command Line has 2 wildcards that can be used when
selecting files as part of a command; the * and the ?
• The wild cards are used to replace characters in the required path to
allow for more specific selection or more wide ranging selection
• The wildcards are used to build patterns that the system will use to
search for the required file(s)
• The * wild card is used to represent any number of characters and any
actual characters
• The ? Wild card represents any character in the specific location that
the ? appears in. It always represents 1 character
The * wildcard
• If the * wildcard is used before the “.” for the file extension, any characters
between the * and the . are ignored (because the * represents all of the
characters until the “.”)
• Copy a*er.* would be interpreted by the system as copy a*.*
• If the * wildcard is used in the extension, any characters between the *
and the end of the filename are ignored. (because the * represents all of
the characters until the end of the file)
• Copy *.*er would be interpreted as copy *.*
• Copy *.* - copy all files no matter how long the filename or extension are
• Copy a*.b* - copy all files that start with an “a” and have any number of
other characters before the “.”and also have an extension that starts with
a “b” and any number of other characters until the end of the file name.
The ? wildcard
• If the ? wildcard is used in a filename the specific location must exist in
the filename and can have any character in that location
• Copy File?.txt would copy all files that had “File” as the start of the
filename, followed by any single character, followed by the extension txt
• This would include File1.txt, file2.txt, filea.txt
• This would not include File10.txt, file34.txt
• Copy a????.txt would copy all files that start with “a”, followed by any 4
characters, followed by the extension txt
• This would include apple.txt, attic.txt, a1000.txt
• This would not include advertisement.txt, avenue.txt
Batch Files
Batch Files
• Batch files are text files that contain console commands
• The file extension on a batch file is .bat EXAMPLE: list.bat
• An example for you to try on your computers now.
1. Open the command prompt
2. Type Copy con mybatch.bat and press enter
3. Type the following commands pressing enter after each
rem create a new directory
md newdir
dir
4. Press the ctrl key and the z key at the same time then press enter
5. When you get the prompt type mybatch.bat and press enter. This will run the batch file as if it were
a program and perform all of the commands that are inside the batch file. The results will be
displayed on the screen and you should be able to find newdir in the directory listing (you may
have to scroll if your location is in a folder with many files)
Batch Files continued
• You can write programs with variables using batch files
• You can have the user enter parameters when they run the batch
file and then use those parameters in the code of the batch file
• You can have the batch file pause and accept data from the
keyboard
• Batch files enable you to automate tasks by scheduling when the
batch file should run using the Windows task scheduler
How the System Runs Commands and
Programs
• Whenever a command is give in the Windows CLI the system
checks:
• If the command is a built in command such as dir
• If not, it checks to see if a program exists in the current location
with the name entered at the prompt
• If not, it checks to see if the name entered at the prompt exists
inside the PATH environment variable (This is how the system can
run programs that are not in the current location such as notepad)
The PATH Environment Variable
• The PATH environment variable contains a semicolon separated list of absolute paths that the system will look in to find
programs that are typed at the prompt
• This way we do not have to constantly change our location inside the drive structure to run programs
• Normally when you open the command prompt and you have not been moving around using the cd command, the
prompt you get is your user directory EXAMPLE: c:\users\mburg.
• If the PATH environment variable did not exist and I tried to run notepad from the default prompt location, the system
would generate a file not found error
• Try it now, open a command prompt and verify that you are in your user folder, if not try to get there using the cd
command. Then type notepad and press enter
• You can add new paths to the PATH environment variable but you must be careful not to overwrite the current contents

PATH=%PATH%c:\users\mburg;
• Would add my user directory to the PATH environment variable. If you omit the %PATH% you would overwrite the
PATH environment variable with your user path and remove all of the system set paths. Windows should reset the
path when it reboots but you never know……
• You can also modify the path environment variable through the GUI by going to Control Panel, then System, then
select Advanced system settings, then Environment variables

You might also like