How to compare contents of two folders

  • Article
  • 09/15/2021
  • 2 minutes to read

This example demonstrates three ways to compare two file listings:

  • By querying for a Boolean value that specifies whether the two file lists are identical.

  • By querying for the intersection to retrieve the files that are in both folders.

  • By querying for the set difference to retrieve the files that are in one folder but not the other.

    Note

    The techniques shown here can be adapted to compare sequences of objects of any type.

The FileComparer class shown here demonstrates how to use a custom comparer class together with the Standard Query Operators. The class is not intended for use in real-world scenarios. It just uses the name and length in bytes of each file to determine whether the contents of each folder are identical or not. In a real-world scenario, you should modify this comparer to perform a more rigorous equality check.

Example

namespace QueryCompareTwoDirs { class CompareDirs { static void Main(string[] args) { // Create two identical or different temporary folders // on a local drive and change these file paths. string pathA = @"C:\TestDir"; string pathB = @"C:\TestDir2"; System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA); System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB); // Take a snapshot of the file system. IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories); IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories); //A custom file comparer defined below FileCompare myFileCompare = new FileCompare(); // This query determines whether the two folders contain // identical file lists, based on the custom file comparer // that is defined in the FileCompare class. // The query executes immediately because it returns a bool. bool areIdentical = list1.SequenceEqual(list2, myFileCompare); if (areIdentical == true) { Console.WriteLine("the two folders are the same"); } else { Console.WriteLine("The two folders are not the same"); } // Find the common files. It produces a sequence and doesn't // execute until the foreach statement. var queryCommonFiles = list1.Intersect(list2, myFileCompare); if (queryCommonFiles.Any()) { Console.WriteLine("The following files are in both folders:"); foreach (var v in queryCommonFiles) { Console.WriteLine(v.FullName); //shows which items end up in result list } } else { Console.WriteLine("There are no common files in the two folders."); } // Find the set difference between the two folders. // For this example we only check one way. var queryList1Only = (from file in list1 select file).Except(list2, myFileCompare); Console.WriteLine("The following files are in list1 but not list2:"); foreach (var v in queryList1Only) { Console.WriteLine(v.FullName); } // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } // This implementation defines a very simple comparison // between two FileInfo objects. It only compares the name // of the files being compared and their length in bytes. class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileInfo> { public FileCompare() { } public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2) { return (f1.Name == f2.Name && f1.Length == f2.Length); } // Return a hash that reflects the comparison criteria. According to the // rules for IEqualityComparer<T>, if Equals is true, then the hash codes must // also be equal. Because equality as defined here is a simple value equality, not // reference identity, it is possible that two or more objects will produce the same // hash code. public int GetHashCode(System.IO.FileInfo fi) { string s = $"{fi.Name}{fi.Length}"; return s.GetHashCode(); } } }

Compiling the Code

Create a C# console application project, with using directives for the System.Linq and System.IO namespaces.

See also

  • LINQ to Objects (C#)
  • LINQ and File Directories (C#)

We all have backup folders on our computers. As a result, there can be two backup folders with similar names, and you won’t know what’s what. When this happens, it is difficult to decide which folders to keep and which to discard. You can verify both folders manually, but the problem may occur if there are too many files with similar names. To avoid unnecessary hard work, you need a tool to compare folders in Windows 10.

The comparison tools can give you similarities and differences between the folder based on different factors. Once you know the differences, you can share files directly from the File Explorer to the hard drive, flash drive, emails or simply discard them.

5 Ways To Compare Folders in Windows 10

There are Windows 10 tools, inbuilt options, and third-party software to compare the contents of two folders, be it file sizes, file formats or backed-up files. We have listed some easy ways to compare folders in Windows 10.  

1. Command Prompt

Let’s go with the inbuilt way first. You can use Command Prompt to compare folders in Windows 10. Windows 10 provides commands for everything, and if you are comfortable with the command line, you can go with it this way.

You can use Robocopy to compare folders in Windows 10. It comes inbuilt in Windows 10.

Press Windows key + S and type cmd in the search bar of the Start menu. Open Command Prompt from the search results.

Open Command Prompt

In the Command Prompt terminal, type the below command and press Enter.

robocopy “Folder 1 Path” “Folder 2 Path” /L /NJH /NJS /NP /NS

You need to add both the location of the folders in the command.

The command will look like this:

robocopy "C:UsersUrviDesktopFolder 1" "C:UsersUrviDesktopFolder 2" /L /NJH /NJS /NP /NS
robocopy command execution

Make sure you type in the right path of the folder/directory; otherwise, you will get errors while executing the command.

The output of this command will be as below.

                        C:UsersUrviDesktopFolder 1

          *EXTRA File                   abc.bat

          *EXTRA File                   sshot-6.png

          *EXTRA File                   sshot-8.jpeg

            New File                    sshot-2.png

            New File                    taskkill.lnk

Here, abc.bat, sshot-6.png, and sshot-8.png are present in Folder 2 and not in Folder 1. Similarly, sshot-2.png and taskkill.lnk are present in Folder 1 and not Folder 2.

The files which are present in both folders are not displayed in the output. Therefore, some users may find the output very simple and not that informative as comparison tools. Well, if you just want to know missing files in the source and destination folder, the robocopy command is perfect.

You can compare two files as well with the Command Prompt. This can be useful for the developers and writers with files with different versions to know the difference between two files.

In the terminal window, go to the location where the files are saved using the CD command.

For example,

cd C:UsersUrviDesktopFolder 1

You can use the fc command to compare both the files, as shown below. We have compared text files.

fc file1.txt file2.txt
Compare two files

You will see the lines which are different in the output below. If the files are at different locations you can add the file path before the file name, just as shown below.

fc C:UsersUrviDesktopFile1.txt C:UsersUrviDesktopFile2.txt

Make sure you add the file extension to the command and file names are correct; otherwise, the command won’t be able to find the file in the mentioned folder.

2. WinDiff

If you want a graphical interface to compare folders in Windows 10, you can use WinDiff. It is developed by Windows and is available for download on the website. The app is straightforward to use with no extra icons and features.

Firstly, download the WinDiff app and then extract it. As the installation process completes, the app will be downloaded in the zip file. Make sure you extract it before using it.

Open the extracted folder and double-click on the RunWinDiff.exe file to run it. Now, add the path of the windiff.exe file and ensure it is in the right location; otherwise, you won’t be able to compare the folders. Once the path is added, click on the three dots to add the folder. The other icon next to the three dots is to add a file for comparison.

Lastly, click on the search icon to compare folders in Windows 10.

Compare folder using WinDiff

The comparison of files will be displayed on the new window. You will see detailed file comparison – identical files as well as differences between files.

Comparison result

Click on Expand button to see the file contents.

You can change the result’s view and make changes to what should be visible under the Options menu.

Change Options

You can play around with the option to see different results of the comparison and differences in files. WinDiff can be used as a file comparison tool as well.

3. WinMerge

You can use a third-party app called WinMerge to compare folders in Windows 10. The app is safe and easy to use.

Step 1: Download WinMerge from its official website and double-click on the installation file to start with the install. Click on Run.

Step 2: Click on Next after reading the license agreement.

Click on Next

Step 3: Select the location where you want to save program files of WinMerge. If you are not sure about the location, let it be as it is. Click on Next.

Click on Next

Step 4: Keep the plugins and features getting installed with WinMerge as it is. You can install the languages package if you want.

Once everything is done, click on Next.

Click on Next

Step 5: Select Additional tasks as per your wish. We recommend enabling Explorer context menu integration. And then click on Next.

Select Additional tasks

Step 6: Once you reach the Ready to Install window, check all the settings and click on Install.

Click on Install

Step 7: Lastly, click on Finish to complete the installation and launch the WinMerge application on your computer.

Click on Finish

Step 8: Once the WinMerge is installed, press Ctrl + O keys simultaneously to launch a new comparison window.

Step 9: You can click on the Browse button to add the folder/directory location. Also, check the checkbox next to Read-only for both the folders.  

The Folder Filter should be set to *.* and tick the checkbox next to Include Subfolders.

Click on Compare to start the comparison. If the folder contains 100s of files and subfolders, the comparison output may take time.

Compare folder using WinMerge

Now, you have the comparison result in the next tab. You can see the right and left file dates along with filenames and extensions. The extension column can help you know file types.

Comparison results

If you want to compare files, you can double-click on the file and see the difference. If it is a text file, you will see file content in two side-by-side windows. The view of the comparison result can be changed easily from the View option in the menu bar.

4. PowerShell

If you love writing PowerShell scripts, you will like this way of comparing folders in Windows 10. Scripts are fun only if we know how to work with them; otherwise, they can get annoying to understand. You can try out the below script in PowerShell and check if you find it comfortable or not.

Press Windows key + X to open the quick link menu and click on Windows PowerShell to open it.

In the PowerShell window, type the following script and press Enter.

$fso = Get-ChildItem -Recurse -path C:UsersUrviDesktopFolder1$fsoBU = Get-ChildItem -Recurse -path C:UsersUrviDesktopFolder2Compare-Object -ReferenceObject $fso -DifferenceObject $fsoBU

Script Credit

Here, change the folder1 and folder2 paths as per your requirement.

Folder compared using PowerShell

The output of the comparison means abc.bat, sshot-6.png, and sshot-8.png are present in Folder 2 but missing in Folder 1. On the other hand, file1.txt, file2.txt, sshot-2.png, and taskkill.ink are present in Folder 1 and not in Folder 2.

You can map your output according to the above analysis.

5. Third-party Applications

WinMerge is a famous third-party application for comparing folders in Windows 10. However, if you want more options for third-party apps, you can go through the list below.

Total Commander

Total Commander is all in one when it comes to files and folders. You can use it as a file manager or folder comparison tool. If there are differences in the files/folders, you can also synchronize it with Total Commander. The tool is easy to use and has too many features to help you with files and folders.

Download Total Commander

Meld

Meld is a tool for developers and coders who want to compare folders, directories, and version control projects. It is a perfect tool for developers who work with multiple files and need constant updates on code changes. You can compare files two and three ways with Meld. The tool supports Git, Subversion, Bazaar, and much more. The code comparison can be made easy with this tool.  

Download Meld

DiffMerge

DiffMerge can be used on Windows, macOS, and Linux operating systems to compare and merge files visually. You can use it for folder and file comparison. The advanced tool makes it easy to check duplicate files and folders. There is an option to integrate this tool in File Explorer. The tool is easy to use and customizable as per the user’s needs.

Download DiffMerge

FreeFileSync

FreeFileSync is a free and open-source tool that can help you compare folders in Windows 10. It is an intelligent tool that can help you take backup of your files intelligently. You can create backup copies of all the essential files on your computer using this tool. In addition, FreeFileSync helps you in the synchronization of files on Windows, Mac, and Linux.

Download FreeFileSync

Conclusion

Comparing document files and folders visually can be a lot of unwanted hard work. If you have 100s and 1000s of subfolders and files in a particular folder, it becomes impossible to check them manually. There are tools, commands, and scripts that can help you compare folders in Windows 10.

The third-party tools can help you compare, merge, and synchronize folders. You can also use Command Prompt terminal and PowerShell to compare folder contents. If you want a user interface for comparing tools like WinMerge and WinDiff is perfect. There are many ways to compare folders in Windows 10; you can choose the method you are comfortable with.

Toplist

Latest post

TAGs