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:
/s: Recursively displays contents of specified directory and all subdirectories/b: Uses bare format (no heading information or summary), displaying only file or directory names/o:n: Sorts output results by name/ad: Key parameter, displays only directories, filtering out all files
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:
-Directory: Retrieves only directory objects, equivalent to/adin cmd-Recurse: Recursively traverses all subdirectoriesSelect-Object FullName: Selects full path property for output
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:
- Identifying empty directory structures during system cleanup
- Project documentation structure analysis
- Directory verification in automated backup scripts
- Directory traversal in permission audits
Important Considerations
Practical usage requires attention to:
- Ensure read permissions for target directories
- Large directory structures may require extended processing time
- Pay attention to file encoding formats during output redirection
- Hidden directories may require additional parameter handling