递归目录列表输出受限求助:dir命令生成文件中途截断
dir /s Output Issue on Large Servers Hey there, Mitch! I’ve run into this exact line-limit problem with dir /s on growing Windows servers before—let’s break down solutions that work without needing to split up folder traversal (perfect for your frequently changing directory structure).
Why Your dir /s Command Is Truncating
The 1,002,991 line cutoff is a known limitation of the legacy cmd.exe dir command when outputting massive amounts of data. Older Windows Server versions (and even some newer ones in certain contexts) cap the output buffer for dir, leading to incomplete logs once that threshold is hit.
Solution 1: Use PowerShell (Recommended)
PowerShell has no inherent line limits for recursive directory listings and handles large datasets far more reliably. Here’s a command that replicates your original dir /s output (or can be adjusted for more detail):
Basic File Path List (Matches Your Original Goal)
Get-ChildItem X:\ -Recurse | Select-Object FullName | Out-File "X:\Back-Ups\File Lists\X-Drive-Complete-File-List.txt" -Encoding UTF8
Detailed Output (Like dir /s with Dates/Size)
If you need the same metadata as dir, use this to include file size, last modified time, etc.:
Get-ChildItem X:\ -Recurse | Format-Table -AutoSize FullName, Length, LastWriteTime | Out-File "X:\Back-Ups\File Lists\X-Drive-Complete-File-List.txt" -Encoding UTF8 -Width 200
- The
-Width 200prevents line wrapping for long file paths. -Encoding UTF8ensures special characters (if any in your filenames) are saved correctly.
Solution 2: Stick with cmd.exe but Use for /r Instead
If you can’t switch to PowerShell for some reason, the for /r loop avoids dir’s buffer limits by iterating through each file individually and appending to your log:
@echo off >"X:\Back-Ups\File Lists\X-Drive-Complete-File-List.txt" ( for /r X:\ %%f in (*) do echo(%%f )
- The
>clears the target file first, then the loop writes each file path to it. echo(%%fhandles empty filenames/paths better than plainecho %%f.- This will include all files in subdirectories, just like
dir /s.
Bonus Tips
- If you need to run this daily automatically, wrap the PowerShell or cmd command in a scheduled task—both work seamlessly with Windows Task Scheduler.
- For extremely large drives, PowerShell might take a bit longer, but it’s far less likely to fail mid-run compared to
dir.
Give these a shot and let me know if you need to adjust anything for your specific server setup!
内容的提问来源于stack exchange,提问作者Mitch Buz Stringer

