This page explains how to work with files and folders in a Windows Command Prompt. It is part of our getting started guide for the Command Prompt on Windows.
There are two commands you will use most
The "Dir" command is probably the most basic command. It gives you a listing of all the files and folders that are located in the folder that you are currently open in.
For example, in the screenshot above, we are in the root of the C:\ drive. If a DIR command is carried out at this point, then you will get a result similar to this:
Volume in drive C is System
Volume Serial Number is 1234-5678
Directory of C:\
25/10/2002 23:42 0 AUTOEXEC.BAT
26/10/2002 01:03 0 COMLOG.txt
25/10/2002 23:42 0 CONFIG.SYS
06/02/2003 15:08 0 dir.txt
04/11/2002 20:40 262,144 ntuser.dat
25/10/2002 23:49 <DIR> Documents and Settings
27/01/2003 19:58 <DIR> Program Files
27/01/2003 09:26 <DIR> WINDOWS
5 File(s) 262,144 bytes
3 Dir(s) 12,528,111,616 bytes free
This command does not show you any hidden files. To see hidden files you will need to use the ATTRIB command.
Advanced "dir" commands
You can get a little more advanced with the dir command by putting extra commands after the intitial "dir".
will show the contents of the current folder, and any subfolders that are below. This will produce a very long file listing. You might want to filter the results or send them to a text file (see above).
dir /s something.doc
This command will search through all subdirectories below for a file that matches that file name. Similarly, issuing the command
dir /s *.doc
will list all files that end in .doc (Word files).
More information on the DIR command can be found in the DIR Command Reference.
The CD command is very simple to use and all commands are relative to the directory that you are in. Therefore if you want to change to the "Windows" directory, you would type
If you then wanted to change to the "system" directory (which is a sub directory of "windows"), you would enter
However if you knew that you wanted to change to the system directory immediately, you could type this command instead:
However if you were already deep in another directory (for example c:\ documents and settings) and then wanted to move directly to windows\system then you can do that by putting in an initial backwards slash:
This tells the command to go to the root of the drive, then to the directory Windows and system.
Moving Back Up
You can also move back up the tree, instead of down.
If you want to just return to the root of the drive (C:\) you would just type
cd \ or cd\
If you wanted to just go back one level (to use the earlier example, you were in c:\windows\system and you wanted to be in c:\windows) you would just type:
Alternatively, if you want to access a totally different directory, for example "Program Files" you could type
cd program files or cd "program files"
When typing directories to change to, if the directory name is unique, then you can use wild cards. For example
Directory Entry Shortcut: : If you are on Windows 2000 or XP and you have a Windows Explorer screen open at the location that you want to run a Command Prompt command you could open the Command Prompt and typing a long series of directories or CD commands. Instead, just type CD and then a space, then drag and drop the directory you want to work in to the Command Prompt window. The full path to the directory will be entered, ready for you to press enter.
More information on the CD command can be found in the CD Command Reference.
If you want to run a command against a directory on another drive, such as the D drive, then you need to change to that drive, This is done simply by entering the drive letter and a colon:
On Windows Vista and higher you can also combine the drive letter change and directory by using the full path:
On Windows XP and older, you can still enter the above command, but will then need to do d: . When you do, you will find that you are in the directory where you did the CD command to.
You can rename files in a command prompt.
rename something.txt someone.txt
would rename the file something.txt to someone.txt
You can create directories from the command prompt:
would create a directory called "something".
If you want a directory with a name that has a space in it, such as "My Files" you need include the command in " ":
mkdir "my files"
This ensures that the directory is created with the space. Without the space you will get a new directory called "my" and an error message.
You can also use the md command instead of mkdir.
If you want to delete a file, then just type:
If you want to delete all files in a folder then type
similarly if you want to delete all files with a certain extension, then type
(where ext is the extension you want to delete)
To delete a folder, simply enter
There are more advanced options for deleting files through the Command Prompt - view the Command Reference for DEL to see the full list.
Note - if you delete a file within a Command Prompt session it does not go in to the recycle bin. It will be deleted immediately.
You can purchase books which cover the Command Prompt in more depth from our Amazon Store.
© Sembee Ltd. 1998 - 2016.
Reproduction of any content on this web site is prohibited without express written consent. Use of this web site is subject to our terms and conditions.
All trademarks and registered trademarks are property of their respective owners. This site is not endorsed or recommended by any company or organisation mentioned within and is to provide guidance only and as such we cannot be held responsible for any consequences of following the advice given.
Sembee Ltd. is registered in England and Wales at 1 Carnegie Road, Newbury, Berkshire, RG14 5DJ.
Registered company number: 4704428. VAT Number GB 904 5603 43.