Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do Binary comparison of files recursively within 2 folder paths using cmd?

I need to write a script for binary comparison of all files within 2 folders recursively. The 2 folders are installation folders and contain same files, but they are installation folders of different versions. I need to find which files (.dll, .lib, .exe) have changed with regard to previous versions.

I have tried using fc command

fc /b %1\* %2\* > result.txt 

But, it compares files only inside the stated folder. I need to recursively compare all files within all folders.

I thought this can be achieved by for loop.

For /r C:\test\%%f in (*) do @fc /b %%f C:\test\%2\%%~nxf > result.txt  

The problem here is %%~nxf which only gives the file name not the relative path.

I tried using forfiles command:

forfiles /s /p C:\test\%1 /m * /c "cmd /c @fc /b %1\@relpath %2\@relpath"

@relpath introduces .\ in the middle of the path, which is messing up my complete path. Any pointers on this one will be highly appreciated.

Please help!

like image 754
Kumar Vikramjeet Avatar asked Feb 04 '13 08:02

Kumar Vikramjeet


1 Answers

This is solution requires to use 3rd-party tool, but it is very efficient.

You will need to get md5deep - it is just 20KB executable.

Then, execute it against first directory like this:

md5deep -l -r dir1 > old.txt

Result is saved in old.txt and looks like this:

e7c3fcf5ad7583012379ec49e9a47b28  .\a\file1.bin
2ef76c2ecaefba21b395c6b0c6af7314  .\b\file2.bin
45e19bb4b38d529d6310946966f4df12  .\c\file3.bin
...

Then, run it in negative matching mode against second directory:

md5deep -l -r -X old.txt dir2

It will print checksum and names of all files in second directory which differ from original.

You can also run it in standard checksumming mode against second dir and compare the result yourself.

At any rate, I doubt that you can get away with just cmd: you will have to write something in more higher-level language, like C/C++/C#/whatever.

like image 137
mvp Avatar answered Jan 03 '23 10:01

mvp