当前位置:   article > 正文

如何在Windows中使用DIR命令

windows dir /s >doc.txt

The DIR command is a powerful Windows Command Prompt function that lists all files and subdirectories contained in a specific directory. The DIR command also offers some switches that unlock some powerful functionality. Let’s take a look.

DIR命令是功能强大的Windows命令提示符功能,它列出了特定目录中包含的所有文件和子目录。 DIR命令还提供了一些可解锁某些强大功能的开关。 让我们来看看。

DIR命令开关 (DIR Command Switches)

You can use the DIR command by itself (just type “dir” at the Command Prompt) to list the files and folders in the current directory. To extend that functionality, you need to use the various switches, or options, associated with the command.

您可以DIR使用DIR命令(只需在命令提示符下键入“ dir”)即可列出当前目录中的文件和文件夹。 要扩展该功能,您需要使用与该命令关联的各种开关或选项。

基于文件属性的显示 (Display Based on File Attributes)

You can add “/A” followed by a letter code after the DIR command to display files with a specific attribute. These letter codes include:

您可以在DIR命令之后添加“ / A”和字母代码,以显示具有特定属性的文件。 这些字母代码包括:

  • D: Displays all directories in the current path

    D:显示当前路径中的所有目录

  • R: Displays read-only files

    R:显示只读文件

  • H: Displays hidden files

    H:显示隐藏文件

  • A: Files that are ready for archiving

    答:准备存档的文件

  • S: System files

    S:系统文件

  • I: Not content indexed files

    I:不包含索引文件

  • L: Reparse points

    L:重新积分

So, for example, to display just the directories in the current path, you’d type the following command and then hit Enter:

因此,例如,要仅显示当前路径中的目录,请键入以下命令,然后按Enter:

dir /ad

You can combine those codes, too. For example, if you wanted to show only system files that are also hidden, you could use the following command:

您也可以合并这些代码。 例如,如果只想显示也被隐藏的系统文件,则可以使用以下命令:

dir /ash

You also can add a “-” (minus) in front of any of those letter codes to specify that the DIR command does not show that kind of file. So, for example, if you don’t want to see any directories in the results, you could use this command:

您还可以在任何字母代码之前添加“-”(减号)以指定DIR命令不显示该类型的文件。 因此,例如,如果您不想在结果中看到任何目录,则可以使用以下命令:

dir /a-d

One more tip: Instead of cramming the main switch and the letter code together the way we did in our examples, you can use a colon to separate the switch from its optional codes. Like this:

另一个提示:您可以使用冒号将开关与其可选代码分开,而不是像我们在示例中那样将主开关和字母代码挤在一起。 像这样:

dir /a:d

It can make things a little easier to parse, but it’s entirely optional.

它可以使事情更容易解析,但是它是完全可选的。

显示剥离结果 (Display Stripped Results)

Using the /b switch with the DIR command strips away all excess information, displaying only the name of the folders and files in the current directory and not attributes like file size and time stamps. Type the following command to make it work:

在DIR命令中使用/b开关将删除所有多余的信息,仅显示当前目录中的文件夹和文件的名称,而不显示文件大小和时间戳之类的属性。 键入以下命令以使其工作:

dir /b

使用千位分隔符显示 (Display Using Thousands Separator)

In modern versions of Windows, the Command Prompt shows large numbers separated by commas (so: 25,000 instead of 25000). This wasn’t always the case. In older versions, you had to use the /c switch to show those commas.

在现代版本的Windows中,命令提示符显示大量数字,并用逗号分隔(因此:25,000而不是25000)。 并非总是如此。 在旧版本中,您必须使用/c开关显示这些逗号。

Why bother including it here if it’s already the default? Because if for whatever reason you don’t want to show those commas, you can use this switch along with the “-” minus sign:

如果已经是默认值,为什么还要在此处添加呢? “ - ”因为如果出于某种原因你不想显示这些逗号,你可以与一起使用此开关减号:

dir /-c

在列中显示结果 (Display Results in Columns)

You can use the /D switch to display results in two columns instead of one. When you display results this way, the Command Prompt does not show extra file information (file size and so on)—just the names of the files and directories.

您可以使用/D开关在两列而不是一列中显示结果。 当您以这种方式显示结果时,命令提示符不会显示额外的文件信息(文件大小等),而只会显示文件和目录的名称。

dir /D

小写显示结果 (Display Results in Lowercase)

The /L switch displays all names of files and folders as lowercase.

/L开关将文件和文件夹的所有名称显示为小写。

dir /L

在最右边显示文件名结果 (Display Filename Results on the Far Right)

By default, the Command Prompt displays the names of files to the far right. The /N switch used to be used to achieve this effect. Now, you can use it along with a “-” (minus) to have filenames displayed on the far left instead.

默认情况下,命令提示符将在最右边显示文件名。 /N开关曾经用于实现此效果。 现在,您可以将其与“-”(减号)一起使用,而将文件名显示在最左侧。

dir /-N

按排序显示结果 (Display Results in Sorted Order)

You can use the /O switch followed by a letter code to display directory results sorted in various ways. Those letter codes include:

您可以使用/O开关后跟字母代码来显示以各种方式排序的目录结果。 这些字母代码包括:

  • D: Sorts by date/time. Older entries appear first.

    D:按日期/时间排序。 较早的条目将首先出现。

  • E: Sorts by file extension in alphabetical order.

    E:按文件扩展名按字母顺序排序。

  • G: Sorts by listing folders first, then files.

    G:首先列出文件夹,然后列出文件。

  • N: Sorts by the name of file/folder in alphabetical order.

    N:按文件/文件夹名称的字母顺序排序。

  • S: Sorts by file size, smallest to largest.

    S:按文件大小排序,从最小到最大。

So, for example, you could use the following command to sort results by time and date, with older entries appearing first:

因此,例如,您可以使用以下命令按时间和日期对结果进行排序,而较早的条目将首先出现:

dir /OD

You can also add “-” (minus) before any of the above options to reverse the order. So, for example, if you want to sort files by time and date with newer entries appearing first, you could use this command:

您还可以在上述任何选项之前添加“-”(减号)以颠倒顺序。 因此,例如,如果要按时间和日期对文件进行排序,并且首先出现较新的条目,则可以使用以下命令:

dir /O-D

一次显示一页结果 (Display Results One Page at a Time)

Some directories have hundreds or thousands of files. You can use the /P switch to have the Command Prompt pause the results after it displays each screen. You have to press a key to continue viewing the next page of results.

一些目录具有成百上千个文件。 在显示每个屏幕之后,可以使用/P开关使命令提示符暂停结果。 您必须按一个键才能继续查看结果的下一页。

dir /P

显示元数据 (Display Metadata)

Using the /Q switch on the DIR command displays metadata tied to files and directories, along with ownership details.

在DIR命令上使用/Q开关可显示与文件和目录绑定的元数据,以及所有权详细信息。

dir /Q

目录/ Q

显示备用数据流(ADS) (Display Alternate Data Streams (ADS))

The /R switch displays any alternate data streams (ADS) that files might contain. ADS are a feature of the NTFS file system that let files contain additional metadata for locating files by author and title.

/R开关显示文件可能包含的所有备用数据流(ADS)。 ADS是NTFS文件系统的一项功能,该功能允许文件包含其他元数据,以便按作者和标题查找文件。

dir /R

显示所有文件和文件夹以及内部的所有内容 (Display All Files and Folders and Everything Inside)

You can use the /S switch to recursively show all files and folders inside the current directory. This means all files and folders in every subdirectory, all files and folders in those subdirectories, and so on. Be prepared for a lot of results.

您可以使用/S开关递归显示当前目录内的所有文件和文件夹。 这意味着每个子目录中的所有文件和文件夹,这些子目录中的所有文件和文件夹,依此类推。 准备好很多结果。

dir /S

显示结果按时间排序 (Display Results Sorted by Time)

Using the /T switch along with a letter code lets you sort results by the different time stamps associated with files and folders. These letter codes include:

使用/T开关和字母代码,可以按与文件和文件夹关联的不同时间戳对结果进行排序。 这些字母代码包括:

  • A: The time the item was last accessed.

    答:上次访问该项目的时间。

  • C: The time the item was created.

    C:项目创建的时间。

  • W: The time the item was last written to. This is the default option used.

    W:上次写入项目的时间。 这是使用的默认选项。

So, for example, to sort results by the time items were created, you could use the following command:

因此,例如,要按创建项目的时间对结果进行排序,可以使用以下命令:

dir /TC

以宽格式显示结果 (Display Results in Wide Format)

The /W switch is similar to /D (which shows columns), but instead, it sorts the results in wide format horizontally.

/W开关与/D (显示列)相似,但是,它以水平宽格式对结果进行排序。

dir /W

显示短名称文件名 (Display Short Name Filenames)

The /X switch shows a file’s short name when the long name doesn’t comply with 8.3 naming rules.

当长名称不符合8.3命名规则时, /X开关将显示文件的短名称。

dir /X

显示DIR的帮助页面 (Display Help Pages For DIR)

Using the /? switch displays helpful information regarding the DIR command, including a brief description of all the switches we’ve talked about.

使用/? 开关显示有关DIR命令的有用信息,其中包括我们已经讨论过的所有开关的简要说明。

DIR命令示例 (DIR Command Examples)

All right, now you know about the switches and options associated with the DIR command. Let’s take a look at a few real-world examples to gain a better understanding as to how you can start putting them to use.

好了,现在您知道与DIR命令关联的开关和选项。 让我们看一些实际示例,以更好地了解如何开始使用它们。

A simple dir command returns a list of all files and folders in the current directory you’re in.

一个简单的dir命令返回您当前所在目录中所有文件和文件夹的列表。

Running the following command shows all system files inside your current path by utilizing the “s” attribute:

运行以下命令,利用“ s”属性显示当前路径内的所有系统文件:

dir /a:s

But what if you want to view all files of a certain type within all subsequent folders of your current path. That’s easy, just run this extremely fast and useful command:

但是,如果您想查看当前路径的所有后续文件夹中特定类型的所有文件,该怎么办。 这很简单,只需运行以下极其快速且有用的命令:

dir \*.mp3 /s

You can replace the “.mp3” part with whatever file format you’re looking for.

您可以使用所需的任何文件格式替换“ .mp3”部分。

The asterisk acts as a wildcard, saying “find anything with .mp3 file format at the end” while the “/s” recursively looks through all folders within your current path.

星号充当通配符 ,表示“以.mp3文件格式结尾查找所有内容”,而“ / s”递归浏览当前路径中的所有文件夹。

Now, you may have noticed that returned a LOT of results. Almost too many to be able to read before they scrolled off the screen. This is where we can use the pause switch to give you a chance to read them. To do that, modify the command like this:

现在,您可能已经注意到返回了很多结果。 在滚动离开屏幕之前,几乎无法阅读。 在这里,我们可以使用暂停开关为您提供阅读它们的机会。 为此,请像这样修改命令:

dir \*.mp3 /s /p

Another trick the Command Prompt offers is called piping. You can use the “>” character to send the results of one command to another place or service. A good example of this is sending all your results to a text file. You can then scroll through them later or import them into other types of documents. To do that, you could use the command:

命令提示符提供的另一个技巧是管道。 您可以使用“>”字符将一个命令的结果发送到另一地点或服务。 一个很好的例子就是将所有结果发送到文本文件 。 然后,您可以稍后滚动它们或将它们导入其他类型的文档。 为此,您可以使用以下命令:

dir \*.mp3 /s /b > filename.txt

We added the /b switch in there to only output the filenames themselves, without any of the other details. The greater than symbol reroutes everything normally displayed in your results directly to the file.

我们在其中添加了/b开关,仅输出文件名本身,而没有其他任何细节。 大于符号会将通常显示在结果中的所有内容直接重新路由到文件。



There are many more combinations and uses for the DIR command, but this should be a good starting point to help you understand the basics.

DIR命令还有更多组合和用法,但这应该是帮助您了解基础知识的一个很好的起点。

翻译自: https://www.howtogeek.com/363639/how-to-use-the-dir-command-in-windows/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/411594
推荐阅读
相关标签
  

闽ICP备14008679号