CMD Using DIR to order directories by name
dir
is tree-walking the directory but only sorting entries in the leaf directories. This is a problem because it's a FAT filesystem. On a FAT filesystem, the entries in any given directory are unsorted, causing the tree-walk to be unsorted. NTFS directories, by contrast, are always sorted.
You have a couple solutions. The easiest would be to pipe the output through sort
:
dir /b/s | sort
(If you're using sort
to do the ordering, there's no need for the /on
option to dir
.)
Your other alternative would be to use a Unix-style ls
, which certainly would get this right. Examples would be the ls
in either Cygwin or my own Hamilton C shell.
Related videos on Youtube
Paul Fleming
Updated on September 18, 2022Comments
-
Paul Fleming almost 2 years
I'm using the following command to list files including those in sub-directories ordered by name.
dir /b/s/on
The files are correctly ordered within their given directories but the directories are not ordered. The directories appear to be ordered by last modified date.
Is
dir
able to also sort directories?Here's a screenshot of a segment of the output (obfuscated):
-
Karan over 11 yearsThat commands displays sub-dirs and files all intermixed and sorted alphabetically for me.
-
Paul Fleming over 11 years@Ofiris I'm using FAT32 and NTFS on W7.
-
Ofiris over 11 yearsOk , noticed the tag now, ':' is indeed optional.
-
Karan over 11 yearsIs the obfuscated string exactly the same for all lines listed above in the screenshot?
-
Paul Fleming over 11 years@Karan. Yes, the obfuscated directory name after the drive letter is the same for all entries. It's actually inside that directory that I'm running the dir command.
-
Synetech over 11 yearsAre you sure that
Q:\…\D
is actually a plain, rawD
and not a Unicode character being displayed asD
because it looks similar. The command-prompt maps some Unicode characters that it cannot display to the next closest character that it can, while others get mapped to?
instead. For example,á℠…‽≠ßš¯Ÿ
might get displayed asá?.??ßs_Y
. Wheneverdir
(or even Explorer) tries to sort names containing Unicode characters it cannot display, it puts them before ASCII characters. In other words,≠5.txt
will come beforeAll.txt
. (Though it should still put “\D
” after\A
.) -
Paul Fleming over 11 years@Synetech It's definitely a D. I created the folders A-Z in that order. Then to demonstrate my issue, I renamed "D" to "D2" and then back to "D". This makes it the latest edited directory.
-
Synetech over 11 years
I renamed "D" to "D2" and then back to "D". This makes it the latest edited directory.
Um, that’s not how it works. In NTFS, making a change to the contents of a directory updates the folder’s timestamp, but not in FAT32. Moreover, renaming a directory never changes that directories timestamp even in NTFS, only its parent’s timestamp. If you created the folders in alphabetical order, then on FAT32, they should remain so even when listed with justdir
unless created in a folder where items have been deleted before. Try moving them all to a completely fresh, new folder and re-test. -
Paul Fleming over 11 yearsI'm just saying, that's what I did and that's when it became first in the list in DIR. That was on FAT32. Does that suggest that DIR actually orders directories based on last modified date regardless of filesystem?
-
Nicole Hamilton over 11 yearsThere's an exception to the ordering rule for FAT directories that things are in the order added. If you delete something, that creates an empty slot. Adding a new file or directory will add it into that open slot, taking the old entry's position in the ordering. But that may (don't remember) only happen if there's no space at the end. The point is, FAT directory entries have to be considered unordered unless you explicitly order them with a directory sorter.
-
Paul Fleming over 11 years@NicoleHamilton. Thanks for that information but I have to asked, is it relevant to how DIR displays directories? Is it possible that DIR is actually ordering the results after they're retrieved from the file system, just like it does with the files when I use the
/on
switch? -
Nicole Hamilton over 11 yearsWithout seeing the code, anything is possible.
dir
does have a known problem that the result of treewalking a FAT directory is ordered correctly. They could simply have a bug in their sort code that only affects sorting of directories as described in that bug report, but I think it's more likely they just don't sort at all and you're seeing whatever is there in the filesystem. If we guess right about the bug, we should be able to predict new failure cases like yours. If this was my problem to fix and I didn't know the code, this is where I'd look. -
Nicole Hamilton over 11 yearsIf you list a single directory with
dir
and without any/o
option, you should see the entries listed in the order they appear in that directory. I don't believedir
will do any ordering and, in some quick experiments I just did, it doesn't. -
phuclv about 7 years@NicoleHamilton indeed
dir
won't do any sorting if you don't specify/o
blogs.msdn.microsoft.com/oldnewthing/20140304-00/?p=1603
-
-
Paul Fleming over 11 yearsCan you demonstrate DIR with the sort command?
-
Synetech over 11 years
dir is tree-walking the directory but only sorting entries in the leaf directories.
I don’t follow, are you saying that the directories in the current directory are not sorted? I just tried it in a folder with lots of files and folders on a FAT32 drive and it worked correctly from the CWD, down. -
Nicole Hamilton over 11 yearsFAT and FAT32 directory entries are unordered, unlike NTFS. They're in the order in which they were added. (If you copy a directory from NTFS, they will be ordered.) In the old DOS days, people used to use directory sorters to sort the entries on disk so they'd be in order when listed.
-
Synetech over 11 yearsYes, I know all that, but it does not answer the question. Your answer implies that
dir /o*
does not sort subdirectories in the CWD which does not seem to be the case (and would be very odd anyway). -
Nicole Hamilton over 11 yearsIt's a known problem that
dir
does not sort the entire tree, only leaf directories if you treewalk a FAT directory. There is no organization to the FAT directory structure. By contrast, NTFS directories use a special data management structure called a B-tree ... the directories are essentially "self-sorting".