I maintain a Windows 98SE machine for lots of reasons (Star Commander, SIO-2-PC, ADTPro, 3.5" and 5.25" PC Floppies, RS-232 port, Zip, Jazz, CD-ROM, Ethernet, etc). It is a very useful box.

I also tend to boot to a command prompt a lot on this box, but Windows 98SE uses MS-DOS version 7.1 which has some differences from MS-DOS 6.22.

Mostly this is no big deal, but I do miss the nice DEFRAG command (from MS-DOS 6.22) that would not only defragment your drive but also allow you to sort the directory structure alphabetically. This helped to soothe my file system OCD immensely.

I'm looking for a similar tool I can use from MS-DOS 7.1 (or Windows 98SE if need be) to sort my folders. I've checked the Windows 98 version of Defrag, but alas, it doesn't seem to allow for the nifty alpha sorting that was available in earlier versions.

I've considered trying to run the MS-DOS 6.22 version but suspect it will either complain about being the wrong version, or worse, screw up my filesystem. So..

Does anyone know of a command line tool or Windows 98 utility that will safely and reliably allow me to alpha-sort my directories?

  • 4
    Do you have evidence that your partitions access files most frequently in alphabetical order? The time that DIR spends sorting filenames in memory is far less than the mechanical access time of pulling in files alphabetically. Also, it's often slower to sort an already sorted list than a random one, so you may be making your computer less efficient than it could be by insisting on a sorted defrag. Defragging is a necessary thing on MS-DOS and is rewarded by better performance. Microsoft's removal of the sort option might have been to give better performance overall. – scruss Jun 21 '17 at 15:11
  • 6
    Just put set DIRCMD=/o in your AUTOEXEC.BAT file. – user722 Jun 21 '17 at 16:06
  • 1
    No, I do not have any reason to believe this will improve performance. I simply like being able to see things alphabetical in DOS based file picker dialogs. And (unless someone can tell me how) I cant seem to use DIR /OG and /ON at the same time. An already alphabetical folder means I can use DIR /OG and still get my files/folders grouped and alphabetical. I was only half-kidding when I said its an OCD thing... – Geo... Jun 21 '17 at 16:07
  • 4
    If you can find them, the old Norton Utilities (pre-Symantec borgation) included a ds tool that would, on any given directory, sort the directory entries. If you're using the "DOS" of Win98SE, though, it may not work; Win98SE was the first of the (consumer) Windows editions that wasn't built on DOS. (Win98 not SE was built on DOS - the last version that was essentially a supershell for DOS) – Jeff Zeitlin Jun 22 '17 at 11:44
  • 2
    It's not a good idea to physically sort the files in alphabetical order since you really want the frequently accessed ones at the head of the list. I wouldn't worry about the order on the disk as long as you can configure dir to list them in the right order. – JeremyP Jul 3 '17 at 15:13

With MS-DOS 7.1, 6.22, and possibly older versions as well, you can have the dir command sort your files alphabetically on the fly. This can be done with dir /o:n. The /o switch tells dir that you want to sort the output in a certain, specific order, and :n tells it specifically that you want it in name (alphabetical) order.

If you don't want to type this switch manually every time, consider adding it to the dircmd variable. Assuming you don't currently have anything in said variable, you can type set dircmd=/o:n, and presto. You could add that exact command in your autoexec.bat file, or your config.sys file (either one should work).

  • dircmd is good if you always want the same output. For a more flexible scheme, see my answer. – RichF Jun 23 '19 at 18:02
  • The advantage of just typing the actual dir command is that you are using standard commands which will work on any other computer. Setting the dircmd command just changes the output without having to change your way of working. – Thorbjørn Ravn Andersen Jan 16 '20 at 14:00

The DEFRAG.EXE utility in DOS 6.22 probably won't work as you expect, as (1) it doesn't know anything about FAT32, and (2) it doesn't know anything about long file names, which means that it quite likely will trash your LFNs unless you back them up first.

Searching around, one of the pages referred to directory sort utilities for Windows that deal with FAT partitions. These may help as long as they are still Win95/98/Me-compatible: I make no guarantees about their reliability, and the usual cautions about mucking around with mounted filesystems applies.

As others have noted, sorting the physical directory structure may not be as useful as you think, because DOS makes no effort to keep the directories ordered. You'll have to run the sorting utility periodically to maintain sorted directories.

  • 2
    "it doesn't know anything about long file names, which means that it quite likely will trash your LFNs" Not just "quite likely"; it will trash your long file names, if any, and you'll be left with only the mangled 8+3 ones. I distinctly remember this being a major caveat when Windows 95 hit the market; it was pretty much "whatever you do, don't run disk utilities not updated to be compatible with Windows 95!" – user Jan 4 '18 at 20:59
  • Welcome to Retrocomputing Stack Exchange. Thanks for sharing an answer! Rather than just providing links to data that could go away at any time, it is good if you can summarize the salient point of the link. Making the answer more self contained with references to your sources will also help you do well on Stack in general. You should probably add a disclaimer to the top that @MichaelKjörling is suggesting from one of the links. – Rowan Hawkins Jan 5 '18 at 1:47
  • 1
    I agree with @aCVn - Norton Utilities were not compatible with long file names. – Thorbjørn Ravn Andersen Jun 25 '19 at 10:07
  • it was possible to lock the lfn on windows so you could run the dos utility, but why? there is no reason to reorder the fat entries. – Rowan Hawkins Mar 7 '20 at 7:34

I have never cared what order a directory is actually in. So I don't know how to physically arrange the storage order of a directory structure. (Well, you could copy the whole directory someplace else, delete the original files, then copy them back in the order you want them. Lotta work, but doable.) So instead ...

The dir command is quite flexible, having many options and modifiers. It will do what you want, and more. I couldn't be bothered with remembering all the possible parameters (see dir /?), so I maintain three batch files in my dos path that allow me to do what I want by name.

alphabetical by extension, list.bat

@echo off
dir %1 /ogen /p %2 %3 %4

by date, ddir.bat

@echo off
dir %1 /og-d /p %2 %3 %4

by size, sdir.bat

@echo off
dir %1 /og-s /p %2 %3 %4

I used to have a fourth one, adir.bat which was true alphabetical (not grouping extensions). After years of not using it, I don't even bother with it any more.

@echo off
dir %1 /ogn /p %2 %3 %4

I am in the habit on dos & windows systems of maintaining a c:\bin directory as part of my path. It is where I keep useful short utilities like above. The directory is part of my path, so I just have to type the name, such as list or sdir.


  1. The two echo commands are to prevent the script itself from showing as it executes.
  2. The /o option specifies order, with many sub-option(s) which follow immediately without a space.
    2a. g indicates to group directories first
    2b. e indicates to group by extension
    2c. n indicates alphabetic
    2d. -d by date, newest first (leave off the dash if want oldest first)
    2e. -s by size, largest first (leave off the dash if want smallest first)
  3. the /p option specifies to paginate. Press spacebar to continue, ctrl-c to end immediately
  4. the %2 (etc) indicates to transfer a parameter from the command line. For example, list /s will list alphabetically, and also include subdirectories (each subdirectory being grouped together).
  • You may save the two echos, just by placing the @ at the beginning of the dir command, so making your BAT file a one liner. – mcleod_ideafix Aug 27 '20 at 22:38

Sorting a pre-LFN (long file name) directory was relatively easy. The file system guaranteed that no file name is longer than 8+3, simple sorting algorithms could be used to exchange directory entries you wanted in a different order.

With LFN support, things have gotten a bit tricky: Every single file requires one directory entry for the 8+3 name, plus a variable number of additional entries (dfepending on the actual LFN length) for the lonmg file name. Thus it is way more effort (if not close to impossible) to sort the physical directory entries in a safe way (replacing the complete directory with a sorted one would always be possible, but that's not considered "safe".

This is probably the reason why MS removed support for re-sorting the directory on file systems with LFN support. You should look for a work-around as mentioned in other ansers (setting DIRCMD seems to be a simple way to achieve what you want)

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.