You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

递归目录列表输出受限求助:dir命令生成文件中途截断

Fixing the Truncated 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.

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 200 prevents line wrapping for long file paths.
  • -Encoding UTF8 ensures 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(%%f handles empty filenames/paths better than plain echo %%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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:13:31