4

Just wondering how Windows determines file ordering when running a dir command (without any arguments) in a command prompt.

I know you can explicitly specify an order, but just curious how ordering works when no parameters are used.

The reason is that I have a home stereo which can read songs from a USB drive, and it seems to play songs in the same order as that seen when I run a dir command on a windows XP command prompt. If I can figure out how the ordering works in a vanilla dir command, I might be able to control the play order of the songs on the stereo by changing certain attributes of the files

3
  • 2
    Which version of Windows? What filesystem on your USB drive. My dir lists in alphabetical order. Sep 30, 2012 at 14:49
  • 1
    @David: Same for me in my Win7Home Sep 30, 2012 at 17:03
  • 1
    @DavidHeffernan: Windows XP. USB is FAT formatted.
    – Moe Sisko
    Oct 1, 2012 at 4:06

2 Answers 2

4

dir (without arguments) command always shows files/dirs unsorted, in other words - in same order, as files/dirs are located on disk. NTFS file system "sorts" files/dirs internally (simplifying).

Thought, you may copy files one-by-one to achieve preferred sorting on your player.

5

The order is "undefined", see below. But this is an XY problem because there are a lot of tools made for your real sorting purpose


According to Microsoft's dir documentation (emphasis mine)

/o [[:]SortOrder]: Controls the order in which dir sorts and displays directory names and file names. If you omit /o, dir displays the names in the order in which they occur in the directory. If you use /o without specifying SortOrder, dir displays the names of the directories, sorted in alphabetic order, and then displays the names of files, sorted in alphabetic order. The colon (:) is optional.

The order in which files occur in a directory depends on the file system and how they store file lists in a directory entry:

  • In FAT12/16/32 the file allocation table is just a simple linear list and when a new file is created it's simply put in an empty space in the list. Hence depending on the creation and deletion state the list order will vary. That explains why mp3 players often play in the order you copy files to the folder
  • In NTFS directory entries are stored in a B-tree structure, so the result will be an almost-sorted list

More on Raymond Chen's blog: What order does the DIR command arrange files if no sort order is specified?

If you don't specify a sort order, then the DIR command lists the files in the order that the files are returned by the Find­First­File function.

Um, okay, but that just pushes the question to the next level: What order does Find­First­File return files?

The order in which Find­First­File returns files in unspecified. It is left to the file system driver to return the files in whatever order it finds most convenient.

You can manually sort the files by moving them to another directory in the order you want

If the storage medium is a FAT-formatted USB thumb drive, then the files will be enumerated in a complex order based on the order in which files are created and deleted and the lengths of their names. But the easy way out is simply to remove all the files from a directory then move file files into the directory in the order you want them enumerated. That way, the first available slot is the one at the end of the directory, so the file entry gets appended.


See also In which order does command COPY copy files from source to destination?:

The commands COPY, DEL, DIR, FOR, etc. do not sort file names matching a wildcard pattern before processing. DIR has option /O to request an ordered output depending on next character.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .