57

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.

user2369634
  • 897
  • 3
  • 9
  • 17
  • 4
    Need to enable the 'recursive' checkbox in WinMerge software to achieve this – user2369634 Mar 24 '16 at 14:26
  • 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). – Max Jul 27 '22 at 11:03

6 Answers6

51

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.

surfmuggle
  • 5,527
  • 7
  • 48
  • 77
Kirill Yunussov
  • 1,955
  • 1
  • 21
  • 24
  • 1
    It would be useful if you explained what the different components of the commands do. – kojow7 Jun 29 '20 at 03:21
  • 4
    @kojow7 `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. – Tomáš Hübelbauer Aug 11 '20 at 16:20
  • This works no matter how deep the nesting is – puerile Nov 28 '20 at 10:14
  • 2
    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. – MiloDC Jun 17 '21 at 22:15
  • 3
    @MiloDC This compares lists of files, not file contents... – kwitee Sep 01 '21 at 07:36
  • 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. – marckassay Dec 03 '21 at 00:10
43

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.

gringo_dave
  • 1,372
  • 17
  • 24
  • 4
    [WinMerge](https://winmerge.org/?lang=en) 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.) – Bob Stein Apr 14 '21 at 18:33
  • 2
    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!) – Tom Dec 21 '21 at 18:44
  • No WinMerge required, `dir /s /b > out.txt` did exactly what I wanted – roman m Jan 31 '23 at 19:32
16

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.

dansmith65
  • 165
  • 1
  • 6
  • 2
    Add `/XO /XN` to ignore older/newer files. Remove `/NDL` if you need to list differences in directories (even empty directories). – Vasiliy Zverev Jun 24 '21 at 21:53
  • 1
    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 to exclude non-essential files such as .ini and thumbs.db. Ex: /xf *.db /xf *.ini – Mike Bain Nov 30 '21 at 02:46
  • 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. – Wayne Barron Jun 02 '22 at 01:42
  • This is the best and cleanest answer! Thanks a lot! – Prabath Jun 08 '22 at 06:43
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).

AlfredD
  • 191
  • 2
  • 6
  • 2
    [BeyondCompare](https://www.scootersoftware.com/shop.php) 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. – ScottWelker Oct 11 '21 at 23:54
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.

Mike
  • 436
  • 5
  • 9
0

FreeFileSync did the job for me.

Sagruob
  • 1
  • 2
  • 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 Review](/review/late-answers/31925800) – Ankur Lahiry Jun 06 '22 at 14:58