Compare directories on Windows

All we need is an easy explanation of the problem, so here it is.

I am manipulating a whole bunch of files and placing them in a different directory. What I need to do now is find out which files are in the original directory that aren’t in the output directory (the problem is being processed by a dozen computers). Is there any script I can run on Windows that will display this?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Use WinDiff. It comes with Visual Studio and Platform SDK and can be downloaded separately. It’s mainly to compare files, but it also allows you to recursively compare folders.

Method 2

In PowerShell:

$d1 = get-childitem -path $dir1 -recurse 
$d2 = get-childitem -path $dir2 -recurse 
compare-object $d1 $d2 

Method 3

If you are looking for a manual process and have visual studio installed, then you can use windiff.exe to show the differences.

Method 4

I tend to use PathSync

Method 5

you can try this vbscript, no need to download any stuff.

Set objFS = CreateObject("Scripting.FileSystemObject")
Set d = CreateObject("Scripting.Dictionary")
Set objArgs = WScript.Arguments
strFolderA= objArgs(0)
strFolderB = objArgs(1)
Set objFolder = objFS.GetFolder(strFolderA)
Set objFolder1 = objFS.GetFolder(strFolderB)
For Each strFile In objFolder.Files
    strFileName = strFile.Name
    strFilePath = strFile.Path
    'collect all files and their full paths.
    d.Add strFileName, strFilePath
Next

For Each strFile In objFolder1.Files
    strFileName = strFile.Name
    strFilePath = strFile.Path
    If Not d.Exists(strFileName) Then
        WScript.Echo "Not found in : " & strFolderA & "->" & strFilePath
    End If 
Next

output

C:\test>dir /B c:\tmp
file
test.bat

C:\test>dir /B c:\tmp1
test.bat

C:\test>cscript //nologo test.vbs c:\tmp1 c:\tmp
Not found in : c:\tmp1->C:\tmp\file

Method 6

FreeCommander does this quickly. Pick folder on right, pick folder on left, compare. It’s a handy utility.

Method 7

In cygwin

comm <options> <(cd $dir1;find -type <type>|sort)  <(cd $dir2;find -type <type>|sort) |less -S

The comm command gives three columns of output unless you suppress them.

I used -S so less doesn’t wrap and you can see the columns clearly.

options

  • -23 shows what is unique to $dir1 in first column, suppresses other two columns
  • -13 shows what is unique to $dir2 in second column, suppresses other two columns
  • -12 shows every thing the same between $dir1 and $dir2, suppresses first two columns

type

f for just files d for just directory or use type twice with both or remove type altogether for everything.

In CMD

Note haven’t tested the below I assume the pipe works in CMD.

If you got findutils and coreutils from here http://gnuwin32.sourceforge.net/packages.html (because you don’t want cygwin) and the less package (but you can use > \somepath\file instead of |less -S then notepad file etc).

cd %dir1%
find -type <type> | sort > \somepath\file1
cd %dir2%
find -type <type> | sort > \somepath\file2
comm <options> file1 file2 |less -S

Note replace $… and #…# with the paths if you don’t know how to make variables 🙂

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply