Batch file to compare 2 directories and get names of files that are different [dos]

17,797

Solution 1

First do a dir /s on both folders. Then use fc /a to compare the results.

For anything better than that (depending in your needs) you'll need a specialized tool. For instance have a look at Windiff or WinMerge.

Solution 2

Batchfile

@echo off
if "%2" == "" GOTO Usage

cd /D %1
if errorlevel 1 goto usage

for %%x in (*.*) do if NOT exist %2\%%x echo missing %2\%%x
cd /D %2
for %%x in (*.*) do if NOT exist %1\%%x echo missing %1\%%x

goto end

:usage
echo Usage %0 dir1 dir2
echo where dir1 and dir2 are full paths
:end

Usage

Environment:

F:\so>dir /s dir1 dir2
 Volume in drive F is WIN2K
 Volume Serial Number is 921E-EC47

 Directory of F:\so\dir1

2010-11-22  10:33       <DIR>          .
2010-11-22  10:33       <DIR>          ..
2010-11-22  10:33                   13 a
2010-11-22  10:33                   13 b
2010-11-22  10:33                   13 c
               3 File(s)             39 bytes

 Directory of F:\so\dir2

2010-11-22  10:33       <DIR>          .
2010-11-22  10:33       <DIR>          ..
2010-11-22  10:33                   13 a
2010-11-22  10:33                   13 b
2010-11-22  10:33                   13 c
2010-11-22  10:33                   13 D
2010-11-22  10:33                   13 E
               5 File(s)             65 bytes

     Total Files Listed:
               5 File(s)             65 bytes
               2 Dir(s)     219,848,704 bytes free

F:\so>

Running:

F:\so\dir1>dirc f:\so\dir1 f:\so\dir2

F:\so\dir1>dirc f:\so\dir1 f:\so\dir2
missing f:\so\dir1\D
missing f:\so\dir1\E

F:\so\dir2>
Share:
17,797
Nick Sinas
Author by

Nick Sinas

I have experience developing in Assembly Embedded C C++ C# Objective-C Swift Python And a tiny bit of HTML/CSS I am currently a C# and iOS developer at Metova

Updated on June 13, 2022

Comments

  • Nick Sinas
    Nick Sinas almost 2 years

    I will have 2 directories, folder1 with a set files list, and folder2 with the same set file list but with more files. I need to get folder2's "other files"
    Does something exist like file compare (fc) for directories to return the differences?

    EDIT I am currently creating 2 lists using dir and then doing a file compare. Now I just need to parse the output of the fc to only contain the file names.

    fc /a "C:\whatever\text1.txt" "C:\whatever\text2.txt" >> "C:\whatever\differences.txt"