Programming Field

Dir - DOS/Command Prompt Reference

Displays a list of files and directories within the specified directory.

Usage

Usage of Dir

The Dir command is commonly used to examine files in the current directory. This command displays a list of files on the screen (console) based on the specified options. The command is similar in function to the ‘ls’ command in Linux.

Usage of Dir 2

Additionally, using ‘dir’ alone provides detailed information, so it is common to use ‘dir /W’ for a more concise display. Also, to output the names of files (directories) without additional information, ‘dir /B’ is often used.

Dir command provides a rich set of options to control its output. For more details please see the next section ‘Syntax’.

Syntax

dir [<drive>:][<path>][<file-name>] [/A[[:]<attribute>]] [/B] [/L] [/P] [/V] [/W]
  [/C] [/D] [/N] [/O[[:]<sort>]] [/Q] [/R] [/S] [/T[[:]<time-field>]] [/X] [/4]

Options

<drive>:, <path>, <file-name> Specifies the directory or file names to be listed. If omitted, the current directory is used. Specifying the file names can be used such when obtaining date information. However, please note that if the specified file is a hidden or system file, specifying ‘/A:H’ or similar will result in the file not being found.
/A[[:]<attribute>]

Filters the files to be listed based on their attributes. Without specifying the attribute (<attribute>), it includes files with the hidden and system file attributes.

<attribute> specifies the attributes to narrow down. You can specify one of D R A S H for directories, files with read-only, archive, system, or hidden attributes, respectively (refer to Attrib for their explanations). Prefixing with ‘-’ means ‘not’ (e.g. ‘-A’ means ‘not Archive-attribute files’).

If this switch is not specified, hidden and system files are not included in the displayed output, but directories are included.

[Windows NT series/XP or later] You can also specify attributes such as ‘I’ (not content-indexed), ‘O’ (offline), and ‘L’ (reparse points, almost equivalent to junctions and symbolic links).

/B Displays only the names of files and directories. When this switch is specified, /V, /W, and similar switches become no effect, and each line displays one file or directory name. Directory names are not enclosed in [ ], and special directories ‘.’ and ‘..’ are not displayed.
/L Displays all file names in lowercase. (In Windows XP and later, even full-width uppercase characters are converted to lowercase, but, in Windows 95, only short file names are displayed in lowercase, and full-width characters in short file names remain uppercase.)
/P If the list exceeds the screen size, displays the message ‘Press any key to continue’ and separates the list into pages, showing one page at a time (similar to using the More command).
/V

[MS-DOS, Windows 95/98/Me only] Displays a detailed listing. The display includes details such as file allocation size, access date, and more.

If /W is specified, this switch is disabled (or becomes an ‘invalid switch’).

/W

Widens the display of the list. In this format, several file/directory names are shown on one line in a tabular format with consistent spacing between the left and right. Directory names are enclosed in [ ]. This format does not display the update date and file size.

When you specify /B, this switch becomes disabled, and conversely, specifying this switch disables /V.

/C [Windows NT series/XP or later] Displays the file size with digit grouping. This is the default behavior, so if you do not want to use digit grouping, you can specify ‘/-C’.
/D [Windows NT series/XP or later] Similar to /W, but the display order will be vertical instead of horizontal.
/N [Windows NT series/XP or later] Displays file names on the right side of the listing. ‘/N’ is the default option, and specifying ‘/-N’ will show file names on the left side as ‘short file name’ + ‘extension’.
/O[[:]<sort>]

Changes the order in which files are displayed. For <sort> the following options are to be specified:

NSorts files by name in alphabetical order.
SSorts files by size in ascending order (from the smallest to the largest).
ESorts files by extension in alphabetical order.
DSorts files by their modification date in ascending order (from the oldest to the newest).
GDisplays directories first before listing files.
A[Windows 95/98/Me only] Sorts by the most recent last accessed date of files (from the newest to the oldest).

To reverse the order for any of these switches, you can prefix the letter with a ‘-’ (/O:-S, etc.). If omitting <sort>, /O:N is used.

/Q [Windows NT series/XP or later] Adds the owner name of the files to the directory listing. This switch is disabled when /B or /W is specified.
/R

[Windows Vista? or later] Includes alternate data streams (ADS) associated with files in the listing. Alternate data streams are created alongside files and have their own names and data. By default, the Dir command does not display them in the listing. However, specifying the /R option will show the alternate data streams that files are holding.

When /W or /B is specified, /R is ignored.

* The contents of alternate data streams can be created and displayed using the command prompt, but it's not possible to directly delete them using command prompt functionality. To remove an alternate data stream, you need to move the contents of the original file to another file and then delete the file holding ADS. Note that using the Copy command will copy alternate data streams along with the regular file data.

/S [Windows NT series/XP or later] If there are subdirectories in the target, their contents will also be displayed. (Please note that this can lead to a substantial amount of output.)
/T[[:]<time-field>]

[Windows NT series/XP or later] Specifies the date and time to be displayed in the list. Also, the date and time selected by this switch determine the sorting criteria for the /O option. The followings can be specified to <time-field>.

CCreation date and time
ALast access date and time
WLast update date and time

If <time-field> is omitted, /T:W (same for not specifying /T) is used.

/X [Windows NT series/XP or later] Displays the short file name (8.3 format) before the regular file name. If a short file name does not exist, it will be displayed as blank.
/4 [Windows NT series/XP or later] Ensures that the year is always displayed in a 4-digit format.

Details

If the environment variable ‘DIRCMD’ exists, Dir command uses its value as part of the options. By using the DIRCMD environment variable, you can set default options for Dir command, simplifying command line execution. If you want to disable options specified in the DIRCMD variable, you can use Dir command with options prefixed by ‘-’ like ‘/-W’.

When you don't specify a path name with the Dir command, it operates on the current directory, providing output such as file names. To change the current directory, you can use Cd(Chdir), and to change the current drive, you can directly input the drive letter followed by a colon, like ‘x:’ (where ‘x’ is the drive letter). (Also, you can specify a directory name with Dir to list the contents of that specific directory.)

[Windows NT series/XP or later] When you perform a listing without specifying options such as ‘/W’ or ‘/B’, if the list contains reparse point files or directories (junctions, symbolic links, or etc.), the actual files or directories they point to will also be included in the listing.

Samples

Sample 1

dir /W

Displays the files and directories in the current directory. (Using ‘/W’ prevents additional details such as date information from being included in the list, avoiding redundancy.)

Sample 2

dir *.sys /A /W

Displays all files with the ‘sys’ extension, including hidden files, in the current directory

Sample 3

set DIRCMD=/W
dir *.sys /A

This will display the same result as Sample 2. However, unless the DIRCMD environment variable is changed, subsequent Dir commands will be treated as if /W option is included.

Sample 4

set DIRCMD=/W
dir *.sys /A /-W

This will produce a different result compared to Sample 2 or Sample 3.

Sample 5

dir C:\WINDOWS /A /B > list.txt

This command writes the names only of all files and directories within C:\WINDOWS to the list.txt file.

See also