How to get a list of sub-folders and their files, ordered by folder-names

Atara picture Atara · Aug 10, 2010 · Viewed 336.7k times · Source

Can I use dir command-line to get a list of sub-folders and their files, ordered by folder-names, and not just file-names ?

using

dir /s/b/o:gn > f.txt

I first get all sub-folders and only then all sub files, e.g.:

 d:\root0\root1\folderA
 d:\root0\root1\folderB
 d:\root0\root1\file00.txt
 d:\root0\root1\file01.txt
 d:\root0\root1\folderA\fileA00.txt
 d:\root0\root1\folderA\fileA01.txt
 d:\root0\root1\folderB\fileB00.txt
 d:\root0\root1\folderB\fileB01.txt

But I want to get -

d:\root0\root1\file00.txt
d:\root0\root1\file01.txt
d:\root0\root1\folderA
d:\root0\root1\folderA\fileA00.txt
d:\root0\root1\folderA\fileA01.txt
d:\root0\root1\folderB
d:\root0\root1\folderB\fileB00.txt
d:\root0\root1\folderB\fileB01.txt

["file00.txt" and "file01.txt" can also be at the end of the list]

Thanks,

Atara

Answer

Cylindric picture Cylindric · Nov 24, 2011

How about using sort?

dir /b /s | sort

Here's an example I tested with:


dir /s /b /o:gn

d:\root0
d:\root0\root1
d:\root0\root1\folderA
d:\root0\root1\folderB
d:\root0\root1\file00.txt
d:\root0\root1\file01.txt
d:\root0\root1\folderA\fileA00.txt
d:\root0\root1\folderA\fileA01.txt
d:\root0\root1\folderB\fileB00.txt
d:\root0\root1\folderB\fileB01.txt

dir /s /b | sort

d:\root0
d:\root0\root1
d:\root0\root1\file00.txt
d:\root0\root1\file01.txt
d:\root0\root1\folderA
d:\root0\root1\folderA\fileA00.txt
d:\root0\root1\folderA\fileA01.txt
d:\root0\root1\folderB
d:\root0\root1\folderB\fileB00.txt
d:\root0\root1\folderB\fileB01.txt

To just get directories, use the /A:D parameter:

dir /a:d /s /b | sort