Folder Comparison

There are two folders
FolderA - Contains multiple sub folders and multiple files
FolderB - Also contains multiple sub folders and files (more sub folders and files than FolderA)

Consider it like this FolderA is a updated version of FolderB but only contains updated files.

Now I want to compare FolderB with FolderA and copy all the common files from FolderB into FolderC with out loosing structure.

Example: if there is a file testing.xml in  FolderA/ext/tmp/settings/testing.xml  and also in  FolderB/ext/tmp/settings/testing.xml
then I want to copy  FolderB/ext/tmp/settings/testing.xml  to  FolderC/ext/tmp/settings/testing.xml

How can I do that using simple script.
shragiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITEnd-user supportCommented:
This does what you want.
Make a .bat file of this code.
Run it in a CMD window.
Notes:
- Change set root= to the folder above FolderA and FolderB. I used C:\Local\test
- This version ECHOs the MD and COPY commands. To run it for real, remove the ECHO prefixes from each in the :DoIt section.

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set root=C:\Local\test
set FolderA=%root%\FolderA
set FolderB=%root%\FolderB
set FolderC=%root%\FolderC

pushd %FolderB%
for /r %%a in (*) do (
  set AFile=%%a&set BFile=%%a&set CFile=%%a
  set AFile=!AFile:FolderB=FolderA!
  set CFile=!CFile:FolderB=FolderC!
  if exist "!AFile!" (call :DoIt "!CFile!")
)
popd
goto :eof

:DoIt
set CDir=%~dp1
if not exist "%CDir%" ECHO md "%CDir%"
ECHO copy "!BFile!" "%CDir%">nul
goto :eof

Open in new window

shragiAuthor Commented:
Hi NVIT - that solution worked thanks a lot :)
NVITEnd-user supportCommented:
I forgot to mention...

Lines 5-7 are related to lines 12-13:

New code:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set root=C:\Local\test
set After=%root%\After
set Before=%root%\Before
set BeforeTest=%root%\BeforeTest

pushd %Before%
for /r %%a in (*) do (
  set AFile=%%a&set BFile=%%a&set CFile=%%a
  set AFile=!AFile:Before=After!
  set CFile=!CFile:Before=BeforeTest!
  if exist "!AFile!" (call :DoIt "!CFile!")
)
popd
goto :eof

:DoIt
set CDir=%~dp1
if not exist "%CDir%"  md "%CDir%"
 copy "!BFile!" "%CDir%">nul
goto :eof

Open in new window

Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

shragiAuthor Commented:
Yup initially I lost but i figured it out - thanks again :)
NVITEnd-user supportCommented:
Awesome! Glad you figured it out.
NVITEnd-user supportCommented:
Here's, hopefully, a more "generic" version:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

REM BEGIN Variables
set root=C:\Local\test
set FolderAName=After
set FolderBName=Before
set FolderCName=BeforeTest
REM END Variables

set FolderA=%root%\%FolderAName%
set FolderB=%root%\%FolderBName%
set FolderC=%root%\%FolderCName%

pushd %FolderB%
for /r %%a in (*) do (
  set AFile=%%a&set BFile=%%a&set CFile=%%a
  REM set AFile=!AFile:Before=After!
  REM set CFile=!CFile:Before=BeforeTest!
  set AFile=!AFile:%FolderBName%=%FolderAName%!
  set CFile=!CFile:%FolderBName%=%FolderCName%!
  if exist "!AFile!" (call :DoIt "!CFile!")
)
popd
goto :eof

:DoIt
set CDir=%~dp1
if not exist "%CDir%"  md "%CDir%"
 copy "!BFile!" "%CDir%">nul
goto :eof

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LD16Commented:
@NVIT: Thank you very much for this solution. Is it possible to assign your last comment as an accepted solution.
I want to add this proposal to my knowledge management. If you are not able to do this, could you please let me know how should I proceed?

Thank you very much for your help.
Bill PrewIT / Software Engineering ConsultantCommented:
LD16,

I unaccepted your earlier solution selection, go ahead and close again as you want.

~bp
LD16Commented:
Thank you very much Bill. However I am not able to accept the solution as I was not the person who ask the question as a result I am not able to assign the solution.

Thank you very much for your help.
NVITEnd-user supportCommented:
LD16: Does the adjustment by Bill work for you?
Bill PrewIT / Software Engineering ConsultantCommented:
Thank you shragi.

~bp
LD16Commented:
Perfect. I was able to add to my personal knowledge base.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.