Obtaining Subfolder and File Lists Sorted by Folder Names Using Command Line Tools

Nov 21, 2025 · Programming · 15 views · 7.8

Keywords: command line | directory sorting | file system traversal | dir command | sort command | PowerShell

Abstract: This article provides an in-depth exploration of how to obtain lists of subfolders and their files sorted by folder names in Windows command line environments. By analyzing the limitations of the dir command, it introduces solutions using the sort command and compares the advantages of PowerShell in file system traversal. The article includes complete code examples and performance analysis to help readers deeply understand the implementation principles and applicable scenarios of different methods.

Problem Background and Requirements Analysis

In file system management, there is often a need to obtain complete listings of directory structures. The question raised by user Atara involves a common requirement: how to obtain subfolders and all files they contain, sorted by folder names rather than simple file name sorting.

Using the traditional dir /s/b/o:gn > f.txt command produces the following output:

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

This output method lists all subfolders first, followed by all files, which does not meet the requirement for sorting by folder names. The expected output should be:

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

Analysis of dir Command Limitations

While Windows' dir command is powerful, it has limitations when dealing with complex sorting requirements. The /o:gn parameter indicates grouping by name sorting, but this grouping is based on file types and cannot achieve cross-folder mixed sorting.

In-depth analysis of the dir command's sorting mechanism:

dir /s /b /o:gn

This command will:

However, this sorting method cannot meet the requirement for sorting by full path names, resulting in folders and files being processed separately.

Solution Using the sort Command

By combining the dir and sort commands, the desired sorting effect can be achieved:

dir /s /b | sort

The working principle of this simple yet effective solution is as follows:

  1. dir /s /b recursively obtains complete paths of all files and folders
  2. The pipe symbol | passes the output to the sort command
  3. The sort command sorts complete paths in lexicographical order

Test example comparison:

Original command output:

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

Output after using 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

Advanced Application: Obtaining Directory Lists Only

If only directory lists sorted by name are needed, use:

dir /a:d /s /b | sort

Where the /a:d parameter indicates displaying directories only, thus filtering out all files and retaining only folder paths.

PowerShell Alternative Solution

The reference article demonstrates PowerShell's powerful capabilities in handling file system traversal. Compared to traditional command line tools, PowerShell provides more flexible and powerful file operation functions.

Basic file listing command:

Get-ChildItem -Path E:\music

Display directories only:

Get-ChildItem -Path E:\music –Directory

Display files only:

Get-ChildItem -Path E:\music –File

Recursively traverse all subdirectories:

Get-ChildItem -Path E:\music\Santana –Recurse

Advanced sorting functionality:

Get-ChildItem -Path E:\music\Santana -Recurse -File | Sort-Object Length –Descending

Performance Analysis and Best Practices

In practical applications, the performance of different methods needs to be considered:

Recommended usage scenarios:

Conclusion

By analyzing user Atara's specific requirements, we found an elegant solution using the combination of dir and sort commands. This method not only solves the problem of sorting by folder names but also demonstrates the powerful combination capabilities of command line tools. Meanwhile, PowerShell, as a modern automation tool, provides more possibilities when handling complex file system operations.

In practical applications, it is recommended to choose appropriate tools based on specific needs: command line tools are sufficiently efficient for simple file listing requirements; PowerShell is a better choice for complex file operations and automation tasks.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.