Top 50 DOS Problems Solved: Sorting Directory Listings

Q: Could you tell me if it’s possible to make the DIR command list files in alphabetical order?

A: Earlier versions of DOS didn’t allow this but there’s a way round it. MS-DOS 5 gives you an /ON switch to use with DIR, for instance:

DIR *.TXT /ON /P

would list all the files with names ending in .TXT, pause the listing every screenful (/P) and sort the names into alphabetical order (/ON).

Users of earlier DOS programs can shove the output from DIR through a utility program that sorts the listing before printing it on the screen. That utility is SORT.EXE, supplied with DOS. … [So:]

DIR | SORT

diverts the output from DIR into SORT, which sorts the directory listing and sends it to the screen. Put this in a batch file called SDIR.BAT and you will have a sorted directory command called SDIR.

I guess earlier versions of DIR followed the Unix philosophy of “do one thing”…

2 thoughts on “Top 50 DOS Problems Solved: Sorting Directory Listings

  1. > I guess earlier versions of DIR followed the Unix philosophy of “do one thing”…
    From http://man7.org/linux/man-pages/man1/ls.1.html :
    List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor –sort is specified.
    (I don’t get the metaphor that make it relevant for syndication)

  2. Note that if (unlike the example) your DIR command can return several extensions then you probably want to use DIR /ONE to sort by both name and extension.

Leave a Reply

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