Methods for Obtaining Folder and Subfolder Lists from Command Line Interface

Nov 19, 2025 · Programming · 14 views · 7.8

Keywords: Command Line | Directory Operations | Folder Listing | CMD Commands | PowerShell

Abstract: This article provides an in-depth exploration of methods to exclusively obtain folder and subfolder lists in Windows command line interface. By analyzing parameter combinations of the dir command, particularly the mechanism of the /ad parameter, it explains how to filter out files and retain only directory information. The article also compares similar functionalities in PowerShell's Get-ChildItem command, demonstrating implementation differences across various technical solutions for directory traversal tasks. Detailed command examples and parameter explanations help readers deeply understand core concepts of directory operations.

Problem Background and Requirements Analysis

In daily system administration and file operations, there is often a need to obtain folder structure information within specific directories. The core requirement presented by users is: obtaining only the list of folders and subfolders, excluding any files. This requirement is common in scenarios such as system cleanup, directory structure analysis, and backup planning.

Traditional Command Line Solution

In Windows Command Prompt, the dir command is the most fundamental directory listing tool. The initial attempt using dir /s/b/o:n > f.txt command, while capable of recursively listing all contents, produced mixed output containing both files and folders, failing to meet the requirement of obtaining only folders.

Optimized Command Analysis

By adding the /ad parameter, the functionality of displaying only directories can be achieved. The complete optimized command is:

dir /s /b /o:n /ad > f.txt

Parameter Analysis:

PowerShell Comparison Solution

Referencing PowerShell implementation, similar results can be achieved using the Get-ChildItem command:

Get-ChildItem -Path "C:\TargetDirectory" -Directory -Recurse | Select-Object FullName

Parameter Explanation:

Technical Implementation Principles

The core of both solutions is based on file system attribute filtering mechanisms. In Windows file system, each file system object contains specific attribute identifiers, with directory objects having special directory attribute markers. Both the /ad parameter and -Directory switch operate based on this principle for filtering and selection.

Application Scenario Extensions

This technology can be applied to:

Important Considerations

Practical usage requires attention to:

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.