Windows CMD中仅比较两个文件夹文件名:查询FC参数或替代命令
Great question! The fc command is built for comparing file contents, so it won’t focus solely on filenames by default. But don’t worry—there are a few straightforward ways to do exactly what you need using built-in Windows tools. Here are my go-to methods:
Method 1: Use dir + fc (Simple Text Comparison)
Since fc works well with text files, we can generate sorted lists of filenames for each folder and then compare those lists. Here’s how:
- Generate clean, sorted filename lists for both folders:
dir "C:\Your\Folder\Path\A" /b /on > FolderAFiles.txt dir "C:\Your\Folder\Path\B" /b /on > FolderBFiles.txt
/b: Switches to "bare format" (only filenames, no extra metadata like size or date)/on: Sorts the list alphabetically by filename, ensuring consistent order for accurate comparison
- Compare the two lists with
fc:
fc FolderAFiles.txt FolderBFiles.txt
This will highlight any filenames that don’t match between the two folders.
- Clean up the temporary files when you’re done:
del FolderAFiles.txt FolderBFiles.txt
Method 2: Use robocopy (No Temporary Files Needed)
robocopy is usually used for file copying, but it’s also a powerful tool for comparing directory structures. You can use it to list filename differences directly without creating intermediate files:
robocopy "C:\Your\Folder\Path\A" "C:\Your\Folder\Path\B" /l /e /njh /njs /ndl /np /ns
Let’s break down the key parameters:
/l: "List only" mode—no actual copying happens/e: Include subfolders (remove this if you don’t want to compare subdirectories)/njh /njs: Hide job headers and summaries to keep output clean/ndl: Don’t list directory names (focus only on filenames)/np /ns: Hide progress and file size details
The output will show you filenames that exist in one folder but not the other. If you want to filter further:
- Add
/xlto see only files unique to Folder A (exclude files present in Folder B) - Add
/xnto see only files unique to Folder B (exclude files present in Folder A)
Method 3: PowerShell (More Flexible & Readable Output)
If you’re comfortable with PowerShell, this method gives you a clear, structured view of differences:
# Get lists of filenames from both folders $filesInA = Get-ChildItem -Path "C:\Your\Folder\Path\A" -Name $filesInB = Get-ChildItem -Path "C:\Your\Folder\Path\B" -Name # Compare the lists Compare-Object -ReferenceObject $filesInA -DifferenceObject $filesInB
The output will clearly label which filenames are unique to each folder (indicated by <= for Folder A and => for Folder B).
Each method has its pros: dir+fc is great for quick text-based checks, robocopy is perfect for no-fuss directory comparisons, and PowerShell offers the most flexibility for scripting or detailed analysis.
内容的提问来源于stack exchange,提问作者G.C

