Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Two Folders and its Subfolder batch file

I have two folders that contain all the same files and subfolders, but the conents inside each file may have changed. I want to write a batch file that will search through each file and look for any differences. What's the best tool for what I want to do?

like image 743
user2296207 Avatar asked Jul 11 '13 17:07

user2296207


2 Answers

No need for a batch file. A single FC command can do what you want:

fc folder1\* folder2\*

You can be more specific for the file mask in the first folder if you want. For example folder1\*.txt.

The command will report on files that exist in folder1 but are missing in folder2. Extra files in folder2 are simply ignored.

There are a number of options to the FC command. Enter HELP FC or FC /? from the command prompt to get more information.

EDIT

Extending the solution to support subfolders is a bit tricky. It is easy to iterate the folder hierarchy for a given root using FOR /R. The problem is getting the relative paths so that the hierarchy can be applied to another root.

The simplest solution is to use FORFILES instead, since it directly supports relative paths. but FORFILES is... S L O W :/

At this point, a batch file makes sense:

@echo off
setlocal
set "folder1=c:\path\To\Folder1\Root"
set "folder2=d:\path\To\Folder2\Root"
set "fileMask=*"

for /f "delims=" %%F in (
  'echo "."^&forfiles /s /p "%folder1%" /m "%fileMask%" /c "cmd /c if @isdir==TRUE echo @relpath"'
) do fc "%folder1%\%%~F\%fileMask%" "%folder2%\%%~F\*"
like image 60
dbenham Avatar answered Sep 20 '22 16:09

dbenham


I'm having better luck with the following batch file. The path names have to completely match though; so only the Drive Letter differs.

Mapping a shared network folder may make this easier. I can't say for sure for your case.

'--- setlocal set "folder1=D:\User\Public" set "Drv2=E:" set "fileMask=*"

setlocal
 set "folder1=<DrvLttr>:\<Folder>\<SubFolder>"
 set "Drv2=<DrvLttr2>:"
 set "LogFile=<SystemDrv>\User\<UserName>\<LogFileName>"

ECHO "the '>' may OVER WRITE or make a ~NEW~ File for the results" > "%LogFile%"
ECHO " '>>' adds to the end of the file >> "%LogFile%"

FOR /R %folder1% %%A in ( *.* ) DO FC "%%A" "%Drv2%%%~pnxA" 1>> "%LogFile%" 2>&1

If you wish to note what is going to be the command for testing, you can try inserting ECHO after DO. You can drop the 1>>... stuff to see the result on screen, instead of having to open the output file.

like image 44
Gregory D. Mellott Avatar answered Sep 17 '22 16:09

Gregory D. Mellott