-1

I need to compare two folders using Batch commands. Both the folders have subdirectories and files in them. The comparison must check every file. If there is any difference, for instance if we are comparing folder1 and folder2, and file1 is present in folder1 but it is not present in folder2, then it must return true (so that I can do some other operations), else false.

I am actually doing a copy from one folder to another. Once it's done I need to validate if all the files are copied.

Note: I cannot use any third party tools.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Bala
  • 1,077
  • 5
  • 15
  • 35
  • 1
    It is very important that you expand your question to specifiy exactly what you are thinking defines any difference. It is also as important to provide some code with your question. – Compo Dec 20 '16 at 13:27

1 Answers1

2
@echo off
REM Writing the folder tree to a file for each path
pushd "Folder 1"
for /f "delims=" %%c in ('tree /f') do >>"%~dp0folder1.txt" echo "%%c"
popd
pushd "Folder 2"
for /f "delims=" %%d in ('tree /f') do >>"%~dp0folder2.txt" echo "%%d"
popd

REM removing the first three lines from each of the two files
more +3 "folder1.txt" >"folder1.txt.new"
move /y "folder1.txt.new" "folder1.txt" >nul

more +3 "folder2.txt" >"folder2.txt.new"
move /y "folder2.txt.new" "folder2.txt" >nul

REM comparing files
fc /b folder1.txt folder2.txt>nul && echo same || echo different

REM cleaning up
del folder1.txt
del folder2.txt

Should work as long as the folders are on the same drive. Changes the directory to the first folder and prints the output of the command tree to one file and same for the other folder.
Then compares a /binary file comparison and outputs same if the outputs of the command and with that the folders are the same. If not it outputs different.
Deletes two help-files after.

NOTE: If you already have files with that names, change it!

Feel free to ask if something is unclear :)

Edit: Remove first 3 lines to prevent comparison faliures based on Volume-Number/Drive-Letter. Credit for the way to remove goes to dbenhams answer to another question.

Community
  • 1
  • 1
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
  • Hello @geisterfurz007, Thanks for the response . I am afraid the folders are in different drives , one folder is in the physical drive and another one is in a mapped drive . – Bala Dec 20 '16 at 13:46
  • @bkr Is there anything missing? If not please consider marking this as your answer, else I have to know what to improve! – geisterfurz007 Dec 21 '16 at 14:15