When I run tree /a /f > output.txt in Windows' cmd, I get a nicely formatted directory structure. But the directory listing is not in any particular order. How can I get the tree to sort folders and files in alphabetical order?

Note: I need the output formatted as a hierarchy, so using dir will probably not be sufficient. I probably need to use tree.

An excerpt from the output of tree

+---HCM Documents
|   +---Interfaces
|   +---Process Flows
|   +---Workshops
|   |   \---Overviews
|   +---Approvals
|   +---Requirements
|   \---Misc
+---Testing Documents
|   +---Statuses 
|   +---Meeting Minutes 
+---Finance Documents
|   +---Taxes
|   +---General Ledger
|   +---Business
|   +---Process Flows
|   +---Purchases
|   \---Workshops
+---Infrastructure Documents
|   +---Issue and Risk Log
|   +---Timelines
|   +---Templates
|   \---Decisions Log

Note that the folder names are not in alphabetical order. They are also not in order of modified date or size, or any other order I can think of.

Also, this is the default order I get when I run dir without specifying sort order.

These documents are being accessed over a network drive but I don't see why that should make a difference. (Though when I run tree on local folders, they do seem to be in alphabetical order. Perhaps this is actually a factor?)

  • 1
    Tree sorts alphabetically for me by default (Window 7). What version of Windows are you using? Do you have a custom DIRCMD environment variable set? – Ƭᴇcʜιᴇ007 Oct 1 '13 at 17:54
  • using windows 7. I don't see any environment variable called DIRCMD. – Barry Rosenberg Oct 1 '13 at 18:19
  • Can you edit your question and add an example of the output you're getting from Tree? – Ƭᴇcʜιᴇ007 Oct 1 '13 at 20:10
  • 1
    There is no variable DIRCMD unless you create it. SETX DIRCMD=/o:n etc. That will then be the option(s) that apply by default to the DIR command. – Debra Oct 2 '13 at 3:26
  • 1
    tree does not take the sorting directly from dir. It uses it's own findfirst and findnext so the DIRCMD has no effect. It does work on the dir-command. Just tested this with a Linux server where the order is also messed up. Unfortunately tree stays un-ordered. – Rik Oct 2 '13 at 8:34

Tree is a very simple program which loops through all the directories (and if specified the files). It does not do buffering. You can see this because it builds the tree of directories directly on screen (if the output is not redirected). With lots of directories this is slow and output begins right away.

Because tree does not read all the directories at once it can do no sorting. So it is dependent on the filesystem to present the directories in order. If the filesystem does not do this, tree will not be ordered.

If you want an ordered list you will have to go for a utility that can read all the directory and files at once and then present an ordered tree-like view.

| improve this answer | |

There are two options known by me:

  1. Total Commander and its tree invoked by AltF10. Then you can find the tree nicely sorted in c:\Users\your_username\AppData\Local\GHISLER\ in file treeinfoC.wc. You may need to remove brackets [] then by some editor and its regular expression replace function.
  2. Use console (cmd.exe) command:

    dir /b /s /ad c:\

    which can be redirected to a file by:

    dir /b /s /ad c:\ > c:\all_c_drive_folders.txt
| improve this answer | |

Here's what I did: Create the tree structure to your TEMP directory:

cd /d %TEMP%

Then copy the directory structure to the source

xcopy /t /e SOURCEDIR .

now tree will give you a nice alpha-sorted tree output.

| improve this answer | |

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.