Compare 2 directories in windows [closed]
Asked Answered
C

6

63

I need to compare 2 folders "A" and "B" and get the list of files and folders newly added or modified.

I tried using Winmerge software but it is not comparing the files present inside the internal folders(so i have to point to each internal folder manually and have to compare)

Is there any way to achieve this.

Cozy answered 5/10, 2013 at 19:45 Comment(2)
Need to enable the 'recursive' checkbox in WinMerge software to achieve thisCozy
I had the same problem / need. I found that the WinDiff tool was very helpful. It has a GUI where you choose the 2 directories, and then you can save the list of files that differ (or exist only in one of the two dirs).Febrifacient
Z
61

The following PowerShell code compares the file listings of two folders. It will detect renamed or newly created files and folders, but it will not detect modified data or different timestamps:

$dir1 = Get-ChildItem -Recurse -path C:\dir1
$dir2 = Get-ChildItem -Recurse -path C:\dir2
Compare-Object -ReferenceObject $dir1 -DifferenceObject $dir2

Source: MS Devblog - Dr. Scripto

3rd party edit

How to run a PowerShell script explains how to run the code above in a script.

Zoo answered 7/6, 2020 at 13:37 Comment(6)
It would be useful if you explained what the different components of the commands do.Microcrystalline
@Microcrystalline Compare-Object is a general-purpose object compare function in PowerShell. @kirill is using it in a very smart way by feeding it two arrays of paths, each obtained by using Get-ChildItem. Add -Force to Get-ChildItem to include hidden file system entries.Vernacularize
This works no matter how deep the nesting isCapp
This didn't work for me. No output anywhere for two directories with identically-named text files with disparate contents, while kdiff3 correctly reported the very obvious differences.Albin
@Albin This compares lists of files, not file contents...Misgive
Unfortunately, from my experience this consumed a lot of processing resources and after 5 mins yield nothing. Using robocopy with /L as @dansmith65 mentioned yield results instantly. I compared two 60Gb directories.Contributory
T
45

For Windows you can use this solution.

Here's the right way to do it, without the external downloads. It looks like a lot at first, but once you've done it, it's very easy.
It works in all Windows versions from 7 back to 95. For our example assume that you're comparing two directories named 'A' and 'B'.

  1. run cmd.exe to get a command prompt. (In Windows 7, the powershell won't work for this, FYI.) Then do it again, so that you have two of them open next to each other.
  2. in each window go to the directories that you want to compare. (Using 'cd' commands. If you're not comfortable with this, then you should probably go with the external utilities, unless you want to learn command prompt stuff.)
  3. type 'dir /b > A.txt' into one window and 'dir /b > B.txt' into the other. You'll now have two text files that list the contents of each directory. The /b flag means bare, which strips the directory listing down to file names only.
  4. move B.txt into the same folder as A.txt.
  5. type 'fc A.txt B.txt'. The command 'fc' means file compare. This will spit out a list of the differences between the two files, with an extra line of text above and below each difference, so you know where they are. For more options on how the output is formatted, type 'fc /?' at the prompt. You can also pipe the differences into another file by using something like 'fc A.txt B.txt > differences.txt'.

Have fun.

Tantrum answered 23/10, 2013 at 6:33 Comment(3)
WinMerge was helpful comparing the directory output txt files. I used /s for recursive subdirectories, and omitted the /b so I could compare file times and dates as well. (Of course, every "Directory of" line is a mismatch because the directory names are different.)Ursa
WinMerge supports directory comparison natively in their GUI as of posting this, ie no need to use /s or /b as far as I could tell. It worked great for me in the GUI. (@BobStein thanks for posting WinMerge - it's an excellent solution for Windows users!)Matriculation
No WinMerge required, dir /s /b > out.txt did exactly what I wantedSuperhuman
A
18

This is not necessarily better than other options already mentioned but it might better fit certain use-cases. In my case, I wanted to see what was different before copying those differences from one directory to the other. This method is great for that since the /L option means to only log what would happen.

robocopy C:\dir1 C:\dir2 /MIR /FP /NDL /NP /L

You can further refine the output format with other flags, or change the logic used to to compare, etc. Refer to robocopy docs for all the options.

Amethyst answered 17/6, 2021 at 23:1 Comment(4)
Add /XO /XN to ignore older/newer files. Remove /NDL if you need to list differences in directories (even empty directories).Glazing
In my opinion this is better than the other options - much easier to use output - more versatile options. Thanks Vasiliy for the extra switches. I also would suggest using /xf <extension> to exclude non-essential files such as .ini and thumbs.db. Ex: /xf *.db /xf *.iniConservatism
I've been using robocopy for many years and never thought about using it to compare files on the fly. Unfortunately, it did not show any differences between the two folders, so I am back at square one again.Triumphal
This is the best and cleanest answer! Thanks a lot!Santamaria
C
9

We have been using Beyond Compare for years and it's quite useful. You can see which files are identical, which files are in folder "A" only and which files are in folder "B" only, and files that are different (for those files you can see what specific modifications have been made).

Capable answered 4/1, 2018 at 19:54 Comment(1)
BeyondCompare is an excellent, multi-platform tool that is reasonably priced and integrates with many IDEs. My multi-platform license is portable across my Windows, Linux, and MacOS systems - provided I use just one instance at a time. Outstanding product. GREAT company, Scooter Software. I have no relationship. Just a happy user since ~2000.Glick
G
3

Some years ago, I made a command line utility, CrcCheckCopy, to help me verify the integrity of large data copies. It reads the source folder and produces a list of the CRCs of all the files. And then, using this list, it can verify the other folder.

I also use it to verify the same folder after some years, to make sure nothing was accidentally deleted or modified.

I give it from free from here in case people who arrive to this question want to try it.

Gabriellegabrielli answered 8/1, 2021 at 21:14 Comment(0)
K
2

FreeFileSync did the job for me.

Kraska answered 2/6, 2022 at 12:57 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewPicrate

© 2022 - 2024 — McMap. All rights reserved.