dir

显示目录的文件和子目录的列表。 如果未使用参数,此命令将显示磁盘的卷标签和序列号,后跟磁盘上的目录和文件列表(包括其名称和上次修改日期和时间)。 对于文件,此命令显示名称扩展名和大小(以字节为单位)。 此命令还显示列出的文件和目录总数、其累积大小以及磁盘上剩余的可用空间(以字节为单位)。

The dir command can also run from the Windows Recovery Console, using different parameters. 有关详细信息,请参阅 Windows 恢复环境(WinRE)

Syntax

dir [<drive>:][<path>][<filename>] [...] [/p] [/q] [/w] [/d] [/a[[:]<attributes>]][/o[[:]<sortorder>]] [/t[[:]<timefield>]] [/s] [/b] [/l] [/n] [/x] [/c] [/4] [/r]

Parameters

Parameter Description
[<drive>:][<path>] 指定要在其中查看列表的驱动器和目录。
[<filename>] 指定要查看其列表的特定文件或文件组。
/p 一次显示一个列表的一个屏幕。 若要查看下一个屏幕,请按任意键。
/q 显示文件所有权信息。
/w 以宽格式显示列表,每行包含多达五个文件名或目录名称。
/d Displays the listing in the same format as /w, but the files are sorted by column.
/a[[:]<attributes>] 仅显示具有指定属性的目录和文件的名称。 如果不使用此参数,该命令将显示除隐藏文件和系统文件之外的所有文件的名称。 If you use this parameter without specifying any attributes, the command displays the names of all files, including hidden and system files. The list of possible attributes values are:
  • d - Directories
  • h - Hidden files
  • s - System files
  • l - Reparse points
  • r - Read-only files
  • a - Files ready for archiving
  • i - Not content indexed files
可以使用这些值的任意组合,但不要使用空格分隔值。 (可选)可以使用冒号(:)分隔符),也可以使用连字符(-)作为前缀来表示“not”。 For example, using the -s attribute won't show the system files.
/o[[:]<sortorder>] Sorts the output according to sortorder, which can be any combination of the following values:
  • n - Alphabetically by name
  • e - Alphabetically by extension
  • g - Group directories first
  • s - By size, smallest first
  • d - By date/time, oldest first
  • 使用 - 前缀来反转排序顺序
将按照列出这些值的顺序处理多个值。 不要用空格分隔多个值,但可以选择使用冒号(:))。

If sortorder isn't specified, dir /o lists the directories alphabetically, followed by the files, which are also sorted alphabetically.

/t[[:]<timefield>] 指定要显示或用于排序的时间字段。 The available timefield values are:
  • c - Creation
  • a - Last accessed
  • w - Last written
/s 列出指定目录和所有子目录中指定文件名的每个匹配项。
/b 显示目录和文件的裸列表,没有其他信息。 The /b parameter overrides /w.
/l 使用小写显示未排序的目录名称和文件名。
/n 在屏幕最右侧显示具有文件名的长列表格式。
/x 显示为非 8dot3 文件名生成的短名称。 The display is the same as the display for /n, but the short name is inserted before the long name.
/c 以文件大小显示千位分隔符。 这是默认行为。 Use /-c to hide separators.
/4 以四位数格式显示年份。
/r 显示文件的备用数据流。
/? 在命令提示符下显示帮助。

Remarks

  • To use multiple filename parameters, separate each file name with a space, comma, or semicolon.

  • You can use wildcard characters (* or ?), to represent one or more characters of a file name and to display a subset of files or subdirectories.

  • 可以使用通配符(*)替换任何字符串字符,例如:

    • dir *.txt 列出了当前目录中以 .txt开头的所有文件,例如 .txt、.txt1、.txt_old。

    • dir read *.txt 列出以读取开头的当前目录中的所有文件,以及以 .txt开头的扩展名,例如 .txt、.txt1 或 .txt_old。

    • dir read *.* 列出以任何扩展名读取开头的当前目录中的所有文件。

    星号通配符始终使用短文件名映射,因此可能会获得意外的结果。 例如,以下目录包含两个文件(t.txt2 和 t97.txt):

    C:\test>dir /x
    Volume in drive C has no label.
    Volume Serial Number is B86A-EF32
    
    Directory of C:\test
    
    11/30/2004  01:40 PM <DIR>  .
    11/30/2004  01:40 PM <DIR> ..
    11/30/2004  11:05 AM 0 T97B4~1.TXT t.txt2
    11/30/2004  01:16 PM 0 t97.txt
    

    你可能希望键入 dir t97\* 将返回文件 t97.txt。 However, typing dir t97\* returns both files, because the asterisk wildcard matches the file t.txt2 to t97.txt by using its short name map T97B4~1.TXT. 同样,键入 del t97\* 将删除这两个文件。

  • 可以使用问号(?)作为名称中单个字符的替代项。 例如,键入 dir read???.txt 会列出当前目录中以读取开头且后跟最多三个字符的 .txt 扩展名的文件。 这包括 Read.txt、Read1.txt、Read12.txt、Read123.txt和 Readme1.txt,但不包括 Readme12.txt。

  • If you use /a with more than one value in attributes, this command displays the names of only those files with all the specified attributes. For example, if you use /a with r and -h as attributes (by using either /a:r-h or /ar-h), this command will only display the names of the read-only files that aren't hidden.

  • If you specify more than one sortorder value, this command sorts the file names by the first criterion, then by the second criterion, and so on. For example, if you use /o with the e and -s parameters for sortorder (by using either /o:e-s or /oe-s), this command sorts the names of directories and files by extension, with the largest first, and then displays the final result. 按扩展名进行字母排序会导致没有扩展名的文件名首先出现,然后是目录名称,然后是扩展名的文件名。

  • If you use the redirection symbol (>) to send this command's output to a file, or if you use a pipe (|) to send this command's output to another command, you must use /a:-d and /b to only list the file names. You can use filename with /b and /s to specify that this command is to search the current directory and its subdirectories for all file names that match filename. 此命令仅列出找到的每个文件名的驱动器号、目录名称、文件名和文件扩展名(每行一个路径)。 Before you use a pipe to send this command's output to another command, you should set the TEMP environment variable in your Autoexec.nt file.

Examples

若要按字母顺序显示所有目录,采用宽格式,并在每个屏幕后暂停,请确保根目录是当前目录,然后键入:

dir /s/w/o/p

输出列出了根目录、子目录和根目录中的文件,包括扩展。 此命令还会列出树中每个子目录中的子目录名称和文件名。

To alter the preceding example so that dir displays the file names and extensions, but omits the directory names, type:

dir /s/w/o/p/a:-d

若要打印目录列表,请键入:

dir > prn

When you specify prn, the directory list is sent to the printer that is attached to the LPT1 port. If your printer is attached to a different port, you must replace prn with the name of the correct port.

You can also redirect output of the dir command to a file by replacing prn with a file name. 还可以键入路径。 For example, to direct dir output to the file dir.doc in the Records directory, type:

dir > \records\dir.doc

If dir.doc does not exist, dir creates it, unless the Records directory does not exist. 在这种情况下,将显示以下消息:

File creation error

若要显示驱动器 C 上所有目录中具有 .txt 扩展名的所有文件名的列表,请键入:

dir c:\*.txt /w/o/s/p

The dir command displays, in wide format, an alphabetized list of the matching file names in each directory, and it pauses each time the screen fills until you press any key to continue.