DIR command does not have a built-in switch to display only folder names with link information. To achieve this, you must combine the /AD flag for directories with output redirection and parsing tools like FINDSTR or PowerShell. This approach filters the standard directory listing to isolate specific items, providing the precise control needed for batch scripting and automation tasks without relying on non-existent flags.
Understanding DIR Output Limitations
The DIR command is a powerful utility for listing files and directories, but it lacks a dedicated parameter for showing "only" folders in a custom format that includes link details by default. A common misconception involves looking for switches like /L or others that might imply lowercase or link-specific output. These do not exist. The DIR command outputs a verbose list including attributes, size, date, and name. To get just folder names, you must filter this output.
Furthermore, "link information" in Windows typically refers to symbolic links or junction points. Standard DIR output marks these with an L attribute in the first column of the output. However, extracting this specific data requires parsing the text output, as there is no single command-line argument that returns only folder names alongside their symlink status in a clean, machine-readable format.
Filtering Directories Using Command Prompt
If your goal is simply to list folder names without file entries, the most effective method uses the /AD (Attributes: Directory) switch combined with /B (Bare format). The /B switch removes extra information like headers and summary counts, leaving just the names.
dir /ad /b
This command lists all subdirectories in the current path. If you need to include hidden folders, add the /AH switch. While this does not explicitly show "link information," it provides the clean list of folders often required for subsequent processing. For more complex filtering, such as excluding system folders, you can pipe the output to FINDSTR.
dir /ad /b | findstr /v /i "system"
This technique allows you to refine the list further. It is important to note that this method does not distinguish between regular folders and symbolic links. To identify links, you must look at the attributes in the full DIR output or use PowerShell.
Using PowerShell for Advanced Link Detection
PowerShell offers superior capabilities for identifying symbolic links and junctions compared to the traditional Command Prompt. The Get-ChildItem cmdlet (aliased as gci or dir) can inspect the Attributes property of each item. Symbolic links have the ReparsePoint attribute set.
Get-ChildItem -Directory | Where-Object { $_.Attributes -match 'ReparsePoint' } | Select-Object Name
This script retrieves all directories and filters them to show only those that are reparse points (symbolic links or junctions). You can modify the Where-Object clause to exclude certain types of links or to include regular directories as well. This method provides the "link information" aspect that the native DIR command cannot easily provide in a filtered view.
For a broader overview that includes both regular folders and links, you can list all directories and check their type:
Get-ChildItem -Directory | Select-Object Name, @{Name="Type";Expression={if($_.Attributes -match 'ReparsePoint'){'Link'}else{'Folder'}}}
This creates a custom object with the folder name and its type, making it easy to identify which directories are actual links. This is far more reliable than trying to parse the text-based output of DIR, which can be locale-dependent and inconsistent.
Parsing DIR Output for Attributes
If you must stick to Command Prompt, you can parse the raw DIR output to find lines containing both directory indicators and link markers. The full DIR output starts with a line indicating if it is a volume or directory. Each file/directory line starts with attributes like D for directory, A for archive, etc. A symbolic link directory might appear as L or include D depending on the version and context, but usually, symlinks are marked with L in the attribute column.
dir /a:d | findstr /r "^....L"
This regex attempts to match lines where the 5th character (attributes) indicates a link. However, this is fragile because the spacing and formatting of DIR can vary based on screen width and language settings. It is generally recommended to avoid this method for production scripts due to its instability.
Integrating with Batch Processes
When automating tasks, you often need to process these folder names. You can combine the FOR loop with the filtering techniques mentioned above. For example, to iterate over all symbolic link directories using PowerShell within a batch file:
powershell -Command "Get-ChildItem -Directory | Where-Object { $_.Attributes -match 'ReparsePoint' } | ForEach-Object { Write-Output $_.FullName }"
This command executes PowerShell from within a .bat file, captures the output, and prints the full paths of all symbolic link directories. This approach leverages the strength of PowerShell for identification while maintaining compatibility with legacy batch environments.
For more advanced text manipulation, consider combining these results with other tools. If you need to extract specific metadata or descriptions from these folders, you might look into related techniques for handling file metadata. See How to Get File Description Field from File Metadata Using Command Line for details on retrieving additional properties.
Best Practices for Directory Listing
- Use Bare Format: Always use
/Bwhen scripting to avoid parsing headers and footers. - Handle Spaces: When iterating through results, ensure your loops handle paths with spaces correctly by using quotes.
- Prefer PowerShell: For any task involving attribute inspection or complex filtering, PowerShell is significantly more robust than CMD.
- Test Locally: Always test your commands in a safe environment before applying them to critical systems, as modifying or deleting linked folders can break shortcuts or dependencies.
Frequently Asked Questions
Does DIR have a switch to show only symbolic links?
No, the DIR command does not have a specific switch to list only symbolic links. You must use the /A attribute filter combined with text parsing or switch to PowerShell for accurate identification of reparse points.
Why is my DIR output different on different computers?
The formatting of DIR output depends on the code page, language settings, and window width. This variability makes parsing DIR output unreliable for scripts. Using /B minimizes differences, but PowerShell is still preferred for consistency across different locales.
Can I use DIR to delete all empty folders?
While DIR can list empty folders, it cannot delete them directly. You would need to pipe the output to a RMDIR command using a FOR loop, but caution is advised to avoid deleting non-empty directories accidentally.
What is the difference between a junction and a symbolic link?
Both are reparse points, but junctions are limited to local volumes and are essentially hard links to directories, while symbolic links can span across volumes and point to files or directories. PowerShell's Get-ChildItem treats both similarly under the ReparsePoint attribute.
How do I hide system folders from DIR output?
Use the /A-D switch to exclude directories, or /A:-D to include only non-directories. To exclude system folders specifically, you may need to filter out names starting with $ or use FINDSTR to remove known system directory names from the list.