14

In the version of command.com included with MS-DOS, DIR seems to print files in a random order, but if one runs multiple DIR commands, they all print the files in the same order. This order does not appear to be based on date, size, or alphabetization. So what is the order? Does it simply print whatever files it finds first?

31

When a new file is created in a FAT-based file system, its entry will be placed in the first vacant directory slot, if there is one, or else the directory will be extended to add another cluster worth of vacant slots if possible (and the new entry will be placed in the first of those). If no files are ever deleted, files will be assigned directory entries in the order of creation.

Before the advent of long file names, each file that was deleted would result in an empty directory slot, which would get filled by the next file to be created. Long file names complicate this process because they are stored using multiple consecutive directory slots (though I don't know the exact process).

The "dir" command in MS-DOS defaults to reporting files in the same order as their directory entries, but command-line arguments in later versions allow sorting by various criteria.

  • Some details about long file names, by Raymond Chen: devblogs.microsoft.com/oldnewthing/20110826-00/?p=9793 – fernando.reyes Apr 19 at 18:55
  • 1
    And with the knowledge that new entries will be placed in the first vacant slot, you could actually manually order the directory with carefully ordered DEL and COPY operations (I have done that). – dirkt Apr 21 at 4:06
  • "or else the directory will be extended to add another cluster worth of vacant slots" Apart from the root directory, which – if I remember correctly – couldn't be extended. Again, IIRC, with standard tools, the root-dir-size was fixed on floppies (7 clusters, 112 entries in most cases) but I seem to remember you could alter the size when formatting a hard-drive. – TripeHound Apr 24 at 9:27
  • 1
    @TripeHound: Added "if possible" to the sentence. If it's possible to create a file, it will be ordered as described, but as you note that might not always be possible. – supercat Apr 24 at 13:15
22

DIR lists files in the order they’re returned by the find first and find next calls.

On FAT file systems, RAM drives, CD-ROMs etc. this is the order of the directory entries on disk, which on FAT file systems is file creation order as long as no files are deleted. On network file systems, it’s whatever order the server and redirector choose. Other file system drivers can exhibit different behaviour; thus on HPFS and NTFS, which sort directories’ contents on disk, files are returned in the file systems sort order (as can be seen in OS/2 or Windows virtual DOS boxes on HPFS or NTFS volumes).

There are tools which will re-order entries on disk, to provide a permanent sort order for DIR. Defragmenting could also re-order files (commonly, directories first, then files).

DOS 5 added various sorting options to DIR itself; the order then depends only on those, when present.

  • A VDM in OS/2 or Windows NT was a third case, halfway between. Local disc volumes weren't presented as network redirected, but neither were the underlying filesystems unsorted. They were often HPFS or NTFS where directories are sorted by their nature. – JdeBP Apr 19 at 17:41
4

Whatever it finds first. DIR in MS-DOS command.com starts at the beginning of the directory table and reads it through to the end. The files will be in the order they were added to the directory table.

  • 2
    "The files will be in the order they were added to the directory table." If the other two answers are correct, that's not strictly true. Rather, the files would be in the order in which they exist in the directory entries list. Is that what you meant, or are you proposing an answer different from that of the other two currently existing answers? – a CVn Apr 20 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.