How to Compare the Contents of Two Folders in Windows?

winhelponline.com/blog/compare-two-directories-files-find-differences

August 9, 2016

Let’s assume you made a backup task using , XCopy, SyncToy or any other tool to mirror a certain directory to a different location. Especially if you’re using SyncToy, sometimes files or folders may be missing in the destination, or the destination folder has extra files or folders that are not in the source folder.

Here is how to compare the directory structure (along with files) in two different directory paths to make sure you have an exact replica or mirror copy of the source. Many of the methods below also discuss how to synchronize items in two folders after comparing.

Contents

Compare the contents of two folders using Microsoft’s WinDiff

WinDiff is a graphical file comparison tool from Microsoft which was first released in the year 1992 and subsequently updated. Please hold on! Don’t be taken aback by the year of the initial release.

WinDiff is still awesome and it works perfectly fine in Windows 10, but no support. It has enormous potential. WinDiff can compare ASCII & Binary files, compare two directories, and synchronize the folders (left folder → right folder, or vice-versa).

1/23 You can download WinDiff as part of the Windows XP Support Tools package.

Alternate WinDiff download link (third-party mirror). The last updated WinDiff version is 5.2.3790.0 with the Date modified stamp showing 4-7-2016.

To do a directory comparison using WinDiff:

1. Windiff.exe. 2. On the File menu, click Compare Directories. 3. In the Select Directories dialog box, the two folder names that you want to compare in the Dir1 and Dir2 boxes. If you want to compare files in those folders recursively, enable the Include subdirectories checkbox.

The results or the outline window by default shows identical files as well as the differences. You can hide identical files by disabling Show Identical Files from the Options menu.

As you can see, WinDiff is not just comparing file names but also compares file contents in both locations and highlights changes if the file contents are different.

2/23 Double-clicking on the “different” item opens the comparison view with color-coding. The color-coded results indicate what the file differences are.

WinDiff can do ASCII and binary comparison. This means, WinDiff doesn’t rely upon modified date but compares the actual contents of the files.

For example, I modified a character in a in the destination folder (Right folder). I replaced an exclamation mark with a colon, and the file size is exactly the same (1,127 bytes) in both locations. WinDiff picked it up and showed that the files are different.

To save the directory comparison differences to a file, click Save File List in the File Menu. Type a file name with complete without double-quotes.

Uncheck Identical files and Include Checksums options, and click OK. The output file will look like this: 3/23 -- D:\Websites\Winhelponline\blog\wp-content\uploads\2019 : E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp-content\uploads\2019 -- includes left-only,right-only,differing files .\accesschk.exe only in E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp- content\uploads\2019 .\compare-folders.txt only in E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp- content\uploads\2019 .\fta-1803-w10.txt only in E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp- content\uploads\2019 .\ssh-commands-useful.txt only in E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp- content\uploads\2019 .\05\dell-supportassist.png only in D:\Websites\Winhelponline\blog\wp-content\uploads\2019 .\05\disable-theme-sync-ramesh.png different (E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp-content\uploads\2019 is recent) .\05\file-explorer-not-highlight-files.png only in D:\Websites\Winhelponline\blog\wp-content\uploads\2019 .\05\firefox-forget-about-this-site-4.png different (E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp-content\uploads\2019 is more recent) .\05\msconfig-disable-services-non-microsoft.png only in D:\Websites\Winhelponline\blog\wp- content\uploads\2019 .\05\windows-store-error-0x800706d9.png only in D:\Websites\Winhelponline\blog\wp- content\uploads\2019 .\06\notepad-bing-search.png only in E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp- content\uploads\2019 .\06\rundll32-refresh-commands.txt only in E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp- content\uploads\2019 .\06\archives\404chk.vbs only in E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp- content\uploads\2019 .\06\archives\actxprxy-missing-issues.txt only in E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp-content\uploads\2019 .\06\archives\apps.png only in E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp- content\uploads\2019 .\06\archives\articles.vbs only in E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp- content\uploads\2019 .\06\archives\bash-sh.vbs only in E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp- content\uploads\2019 -- 17 files listed

Since the output file is in tab-delimited , you should be able to import into Microsoft Office Excel or easily.

4/23 Note that you can start WinDiff directly with source and destination paths as additional switches for directory comparison. For example, launching windiff.exe ":\source" "d:\destination" would launch the tool and straight away compare those two folder paths.

WinDiff command-line switches

Here is the complete list of WinDiff command-line switches.

5/23 Usage:

windiff [options] path1 [path2]

Options:

-D Compare one directory only. -F[flags] savefile Save composite file to 'savefile'. The 'flags' may consist of one or more of I (identical), L (left), R (right), F (moved leFt), G (moved riGht), S (Similar left), A (similiAr right), X ( after saving list). (e.g. -FLF saves list of Left or moved-leFt lines). -I file Reads list of files to compare, from the specified input file. Each line can contain one or two filenames, space delimited (with quoting, if filenames contain spaces). Use "-" as the filename to read from stdin. If a line contains only one filename, the file is compared to itself. -N name SEND notification to 'name' end of comparison. -O Outline view (no automatic expansion). -P Perverse comparison: breaks lines on punctuation. -S[flags] savefile Save list of files to 'savefile'. The 'flags' may consist of one or more of S (same), L (left), R (right), D (different), X (exit after saving list). (e.g. -SLD saves list of Left or Different files). -T Compare whole subtree.

To save the comparison report to a file using the -S switch. Examples are below:

Save the list of identical files to .txt:

-SS D:\output\diff.txt c:\folder1 d:\folder2

Save the list of different files to diff.txt:

-SD D:\output\diff.txt c:\folder1 d:\folder2

Save the list of left-only files to diff.txt:

-SL D:\output\diff.txt c:\folder1 d:\folder2

Saves the list of right-only files to diff.txt:

-SR D:\output\diff.txt c:\folder1 d:\folder2

Saves the list of left-only AND right-only files to diff.txt:

-SLR D:\output\diff.txt c:\folder1 d:\folder2

To exit WinDiff automatically after saving the comparison report, add the X switch, as below:

Save the list of right-only files to diff.txt and exit WinDiff

-SRX D:\output\diff.txt c:\folder1 d:\folder2

6/23 Save the list of left-only AND right-only files to diff.txt and exit WinDiff

-SLRX D:\output\diff.txt c:\folder1 d:\folder2

To generate all 5 individual “log” files you would have to run WinDiff 5 times.

Example: windiff -SSX identical.txt c:\folder1 d:\folder2 windiff -SDX different.txt c:\folder1 d:\folder2 windiff -SLX left-only.txt c:\folder1 d:\folder2 windiff -SRX right-only.txt c:\folder1 d:\folder2 windiff -SLRX left-right-only.txt c:\folder1 d:\folder2

See also WinDiff File Contents (windiff.hlp) in HTML format.

Sync folders with WinDiff

What’s more? WinDiff can also copy the missing or different files to the left (Folder 1) or to the right (Folder 2) location. To do so,

1. From the File menu, select Copy Files 2. Type the folder path where you want to copy the missing/different files. Important: Don’t use double-quotes around the folder path even if the folder path contains spaces. WinDiff fails to copy files if you adding double-quotes to the path.

3. Choose the Copy from location (Left to Right tree, or vice-versa) 4. Uncheck Identical Files, and click OK.

WinDiff now syncs the folder by copying the missing and changed files recursively to the specified location (Left → Right).

7/23 Despite the excellent ASCII & binary comparison and basic sync capabilities, WinDiff is often an underrated tool among IT folks. That’s because it sports an outdated user interface and has no Unicode support. But WinDiff is enough for most of us, especially those who use the English language OS.

Microsoft has a knowledgebase article on WinDiff, titled How to Use the Windiff.exe Utility which you may want to check out.

Compare the contents of two folders using WinMerge

Here is how to compare two folders along with sub-folders, using WinMerge.

WinMerge is an Open Source differencing and merging tool for Windows. It can compare both folders and files, presenting differences in a visual text format that is easy to understand and handle. Let’s use it to compare the contents of two folders along with their sub-folders (i.e., recursively).

1. Start WinMerge. From the File menu, click Open

2. Browse to select the 1st folder and 2nd folder for comparing. 3. Make sure that the filter is set to *.* so that all files are compared.The “Include 8/23 Subfolders” (compares recursively) option is enabled by default. If you want to compare files in the top-level directory only, uncheck the option. 4. Click Compare WinMerge generates folder compare results, highlighting the differences in yellow color.

5. From the Tools menu, click Customize Columns…. Choose the Columns you want, such as Left size , Right size , Left date , Right date from the numerous other column options provided.

6. Expand the “Folders are different” entry by double-clicking on it. It shows the list of files that are different, “left only“, “right only” and also the files that are identical.

9/23 Note: You can choose to hide the identical files and folders by unchecking “Show Identical Items” from the View menu in WinMerge.

The advantage of using WinMerge is that it offers five different file compare methods to choose from: 1. Full Contents (Default): Full comparison of files by content, with all the bells and whistles. This method invokes plugins and uses the diffutils engine for fully accurate differencing and moved block detection. This is the most complete and recommended method. 2. Quick Contents: Slimmed-down comparison of files by content. This method uses 10/23 streamlined file comparison code which skips plugins and moved block detection. This method is faster than Full Contents, because it does not load the files. Its drawback is that line filters are not applied when comparing. For example, this method sees file as different even if line filters are set to ignore all differences in the files. 3. Modified Date: Compares only the modification dates on the files, so it is far faster than either of the contents methods. But obviously, it is only as accurate as the modification dates. 4. Modified Date and Size: Similar to Modified Date, but also checks file sizes when dates are identical. 5. Size: Compares only file sizes, so it is fast but not as accurate as the contents methods.

WinMerge supports a lot of command-line switches. Check out the WinMerge Manual for the entire list of command-line switches supported.

Sync folders with WinMerge

If you have unchecked the Show Identical Items from the View menu in WinMerge, it only shows you the list of modified, left-only and right-only files. It’s easy to synchronize the items from left → right, or vice versa.

All you need to do is select the files (or select all), right-click on the selection and click Copy. In the Copy sub-menu, choose one of the options:

11/23 1. Left to right: Copy selected files from the left folder to the right. 2. Left to …: Copy files from the left folder to some other folder. 3. Right to left: Copy selected files from the right folder to the left. 4. Right to … : Copy files from the right folder to some other folder.

That’s it!

Editor’s note: WinMerge is lightning fast. When I tested, it took just under 2 seconds (1762 ms) to compare two folders each consisting of approximately 4000 files in 112 sub-folders. It’s probably the easiest and quickest method to compare two (or three) folders along with their sub-folders.

Compare the contents of two folders using Command Prompt

Method 1: How to Compare Files in Two Folders Using Robocopy

Let’s now see how to use the built-in Robocopy console utility to compare the contents of two folders.

Robocopy (“Robust File Copy for Windows”) is an excellent file copy tool built-in to Windows that has enormous capabilities. Robocopy’s capabilities are above and beyond the built-in Windows copy and XCopy commands. RoboCopy is the most preferable of all the command-line methods because Robocopy compares not only the file names but also the last modified . It reports the list of modified files and new files/folders in the source and destination paths.

Among its excellent features include “mirror” mode copy (similar to Microsoft SyncToy’s “” option), ability to retry copying in case of network interruptions, multi-threaded copying, etc. Other than copying files, we can use Robocopy to just compare the contents of two folders (recursively) and log the differences without copying anything, like a dry run.

12/23 Scenario

My website’s files are synced to an external hard drive ( E:\ ) regularly. For the purpose of illustration, let’s check if these two folders are identical. In this example, we’re going to compare the following folders, recursively (i.e. along with sub-folders, a deep comparison):

D:\Websites\Winhelponline\blog\wp-content\uploads and E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp-content\uploads

1. Open a Command Prompt window. 2. Run the following command and press ENTER:

robocopy "D:\Websites\Winhelponline\blog\wp-content\uploads" "E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp-content\uploads" /L /TS /MIR /LOG:D:\Diff.txt

The results of the above command is logged to D:\Diff.txt file.

3. Exit the Command Prompt window.

Details about the Robocopy command-line switches used

/L :: List only – don’t copy, timestamp or delete any files. This option is used for logging purposes only; for a dry-run. /MIR :: MIRror a directory tree (equivalent to /E plus /PURGE). Since we’re using it with /L switch, no file/folder will be actually copied. /TS :: include source file Time Stamps in the output. /LOG:file :: output status to LOG file (overwrite existing log).

In this example, we’re writing the output to D:\Diff.txt . Here is what the log look like:

13/23 ------ROBOCOPY :: Robust File Copy for Windows ------

Started : Monday, April 8, 2019 5:06:10 PM Source : D:\Websites\Winhelponline\blog\wp-content\uploads\ Dest : E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp-content\uploads\

Files : *.*

Options : *.* /TS /L /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /R:1000000 /W:30

------

583 D:\Websites\Winhelponline\blog\wp-content\uploads\ *EXTRA -1 E:\RameshPC-Synced Backup\Websites\Winhelponline\blog\wp- content\uploads\2019\videos\ *EXTRA File 821886 2019/04/07 07:19:24 tiktok-superman-dance.mp4 3 D:\Websites\Winhelponline\blog\wp-content\uploads\2019\Scripts\ Older 210 2019/04/08 10:54:59 0x80071A91.vbs Newer 211 2019/04/08 11:35:32 ErrCodeFinder.vbs New File 211 2019/04/08 11:35:32 Sample.vbs 211 D:\Websites\Winhelponline\blog\wp-content\uploads\h1-10\ 83 D:\Websites\Winhelponline\blog\wp-content\uploads\h1-11\ 1 D:\Websites\Winhelponline\blog\wp-content\uploads\reg\ 9 D:\Websites\Winhelponline\blog\wp-content\uploads\temp\

------

Total Copied Skipped Mismatch FAILED Extras Dirs : 113 0 113 0 0 1 Files : 4125 3 4122 0 0 1 Bytes : 59.96 m 632 59.95 m 0 0 802.6 k Times : 0:00:00 0:00:00 0:00:00 0:00:00 Ended : Monday, April 8, 2019 5:06:10 PM

You can clearly see the extra directories and files in the source as well as the destination folders. Also, it compares the timestamp and tells you if files are older or newer (compared to the same file in your destination).

14/23 In this example, here are the differences spotted:

1. *EXTRA Dir: Extra directory exists in the specified path (destination). 2. *EXTRA File: Extra file exists in the above directory (destination). 3. Older: This file is older compared to the one in destination. 4. Newer: This file is newer compared to the one in destination. 5. New file: This file does not exist in the destination.

Robocopy is an amazing tool to compare & sync files in two folders (along with sub-folders several levels deep) and list out the differences in a log file.

Synchronize folders using Robocopy

To synchronize folders (only from the left → right folder) with RoboCopy, use the same command-line syntax we used earlier to compare folders, but this time without the /L (list-only) switch.

RoboCopy does a one-way sync (from Left → right folder)

15/23 RoboCopy’s Mirror copy mode ( /MIR ) is similar to Microsoft SyncToy‘s “Echo” feature. Remember, RoboCopy does only left → right synchronization — NOT a two-way synchronization.

It’s important to note that the /MIR switch also removes any extra files and directories in the destination folder tree. The “Extra” files/folders are items present in the destination but not present in the source path. The /MIR (mirror) command makes sure that the destination folder structure (and files) is an exact copy of the source.

Use the following command-line syntax to sync items from left → right folder: robocopy source_folder destination_folder /MIR /MT

The /MT switch is to do a multi-threaded copy for a super-fast copy operation. It’s an optional switch, but extremely useful.

The sync status is outputted in the console for each item processed, and the statistics are shown at the end of the output.

To log the output to a file, use this syntax: robocopy source_folder destination_folder /MIR /MT /TS /LOG:D:\Diff.txt

16/23 Automate the commands using a Batch file

You can add all the Robocopy commands to Notepad, and save it as a Windows Batch file (with .bat extension). Simply double-click on the Batch file whenever you want to sync the mentioned folders. You may even run the batch file at the required intervals using Task Scheduler.

Note that the methods command-line methods 2, 3 & 4 below only help you compare the structure of two directories along with files. They don’t compare file sizes or modified date to know which is the newer. They only do a plain comparison based on file names. In a production environment, you may need to use other tools or methods if you need to verify the integrity of your files in the source and destination folders, to make sure they are the exact same copies.

Method 2: Compare Two Folders Using PowerShell

Using PowerShell you can recursively compare two folders easily.

Start PowerShell. Copy the following snippet and paste them all in the PowerShell window, and press ENTER

Enter the source folder and destination folders, replacing the source_folder_path and dest_folder_path placeholders respectively.

17/23 $Folder1 = "source_folder_path" $Folder2 = "dest_folder_path" function Get-Directories ($path) { $PathLength = $path.length Get-ChildItem $path -Recurse | % { Add-Member -InputObject $_ -MemberType NoteProperty -Name RelativePath -Value $_.FullName.substring($PathLength+1) $_ } }

Compare-Object (Get-Directories $Folder1) (Get-Directories $Folder2) -Property RelativePath | Sort RelativePath, Name -desc

You’ll see an output like this:

The side indicator <= means the file or folder exists only in the source. i.e., missing in the destination. The side indicator => means the file or folder exists only in the destination. i.e., missing in

18/23 the source.

Editor’s note: The above code compares only the file names without matching any other details like size, date modified, etc. PowerShell has the capability to do a hash-comparison of items in two folders and output the results. But, for non-coders, RoboCopy is the easiest of all the command-line methods discussed in this article.

Method 3: List the folder structure using TREE command

To compare two directories along with sub-directories, the good old TREE command comes extremely handy. Suppose you want to compare the two folders (including file names) D:\SOURCE and D:\DEST, follow these steps:

1. Open a Command Prompt window. 2. Type these commands:

TREE D:\SOURCE /A /F >D:\SOURCE.TXT

TREE D:\DEST /A /F >D:\DEST.TXT

This outputs each of the command results to separate text files in your D:\ drive’s root directory. The text files will have your directory structure in a tree format, which makes it easy for comparison work.

Quick Tip: To compare only the directories, remove the /F flag. 3. Next, fire up your text comparison utility like WinDiff, WinMerge or DiffMerge. Don’t have any of those? No problem! You can compare the two text files online via diffchecker.com. 4. Go to DiffChecker.com and paste the contents of d:\source.txt in one column, and D:\DEST.txt in another column. Click differences.

19/23 Directory comparison

20/23 Directory and files comparison

Method 4: List folder structure using the DIR Command

To compare the contents of two directories, another command-line you can use is the DIR command.

Use the following command-line syntax to generate the list of files in a directory recursively. You can then compare it using a compare tool or DiffMerge.

DIR D:\SOURCE /S /AD /B >D:\SOURCE.TXT

DIR D:\DEST /S /AD /B >D:\DEST.TXT

Quick Tip: To include files along with the directory listing, remove the “/AD” flag. There is one problem though, when using the DIR command. It lists full directory and file paths in the output, as below:

1. D:\Source\Apr\Week1 2. D:\Source\Apr\Week2 3. D:\DEST\Apr\Week1 4. D:\DEST\Apr\Week2 21/23 In that case, you’ll need to remove the comparison base paths D:\SOURCE\ and D:\DEST\ in the respective text files by editing them with Notepad. To bulk text, use the Replace option in Notepad and remove every occurrence of the base path.

Once done, save the files and compare the contents online via DiffChecker or using an offline text-comparison tool like WinDiff or WinMerge.

Directory comparison – List generated using DIR instead of TREE

I bet you’ll find directory contents output generated using TREE to be much easier and far neater than DIR.

Hope the above methods helped you quickly compare the contents of two folders recursively, identify missing and modified files and reconcile differences.

Let’s know your comments!

About the author

22/23 Ramesh Srinivasan founded Winhelponline.com back in 2005. He is passionate about Microsoft technologies and has a vast experience in Windows — delivering support for Microsoft's consumer products. He has been a Microsoft MVP (2003-2012) who contributes to various Windows support forums.

23/23