CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Windows – DIR command – How to list the files by sort order?

DIR command, by default displays the files and folders in sorted order by their name. This command provides parameters to allow us to control the sort order. Here we discuss it through this Article.

DIR command provides the switch “/O” to enable the sorting. Through this switch it allows below parameters to control the sorting.

N – Sort the list by File or Folder Name

S – Sort the list by File Size

D – Sort the list by date & time

Here we discuss each one with examples.

List the files by Name

By default DIR command displays the files or folders by sort order in ascending order by name of the files or folders. Below commands produce the same result.

C:\> dir

and

C:\> dir /on

How to change this sort order? You need to prefix the symbol “-” before the parameters to reverse the sort order. For example, to sort the list by name in descending order; the command looks like below:

C:\> dir /o-n

List the files and sort them based on their size

We can use DIR command with the switch “/os” to list the files and folders and sort them by size.

C:\> dir /os

What happened if the files of same size exists? Then it sorts based on the name of the file. Similarly, observe that, the directories and files with zero size will be listed first in sort order by their name and then the files will be sorted by their size. Why it first sorted the directories? Because, the size of the directories in the parent directory will appear as zero. Hence it first sort the directories and files with zero size by Name, as the size of them are equal.

List the files by date & time

We can also list the files and folders by date & time. Below command will help to sort this.

C:\> dir /od

Observe that, the files and folders are sorted by date & time and newly created files and folders will appear last in the sort order.

As I mentioned above, the sort order will be changes by prefixing the symbol “-” before the sort parameter. For example, to sort the files and folders by date & time in descending order, here is the command.

C:\> dir /o-d

Be on learning path always!

// Malin

Windows – DIR command – How to list the files by sort order?

One thought on “Windows – DIR command – How to list the files by sort order?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top