如何使用DOS命令列出文件?

22
我需要列出文件名称并将其存储为文本文件! 我所做的是使用以下DOS命令。
 x:\xyz> dir *.* > files.txt

这段代码的作用是将目录“xyz”中存在的文件名以及每个文件的大小、时间和日期存储起来!

如何只获取文件名?

2个回答

38

尝试使用 dir /b 命令,以获取裸格式

dir /? 命令可以显示您可以使用的 dir 命令的文档。这是我在 Windows 7 上的输出:

C:\>dir /?
Displays a list of files and subdirectories in a directory.

DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
  [/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

  [drive:][path][filename]
              Specifies drive, directory, and/or files to list.

  /A          Displays files with specified attributes.
  attributes   D  Directories                R  Read-only files
               H  Hidden files               A  Files ready for archiving
               S  System files               I  Not content indexed files
               L  Reparse Points             -  Prefix meaning not
  /B          Uses bare format (no heading information or summary).
  /C          Display the thousand separator in file sizes.  This is the
              default.  Use /-C to disable display of separator.
  /D          Same as wide but files are list sorted by column.
  /L          Uses lowercase.
  /N          New long list format where filenames are on the far right.
  /O          List by files in sorted order.
  sortorder    N  By name (alphabetic)       S  By size (smallest first)
               E  By extension (alphabetic)  D  By date/time (oldest first)
               G  Group directories first    -  Prefix to reverse order
  /P          Pauses after each screenful of information.
  /Q          Display the owner of the file.
  /R          Display alternate data streams of the file.
  /S          Displays files in specified directory and all subdirectories.
  /T          Controls which time field displayed or used for sorting
  timefield   C  Creation
              A  Last Access
              W  Last Written
  /W          Uses wide list format.
  /X          This displays the short names generated for non-8dot3 file
              names.  The format is that of /N with the short name inserted
              before the long name. If no short name is present, blanks are
              displayed in its place.
  /4          Displays four-digit years

Switches may be preset in the DIRCMD environment variable.  Override
preset switches by prefixing any switch with - (hyphen)--for example, /-W.

不幸的是,/B裸命令和按列排序的/W和/D裸开关不能同时使用。我不知道有什么方法可以在DOS中轻松地按列顺序获取数据并抑制摘要。 - ggb667

25

如果你只想获取文件名而非目录名,那么使用以下代码:

dir /b /a-d > file.txt

如果你想过滤一个特定类型的文件,例如xml:dir *.xml /b /a-d /s | sort > files1.txt - open-ecommerce.org

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接