Folder Comparison

shragi
shragi used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NVITEnd-user support

Commented:
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

Author

Commented:
Hi NVIT - that solution worked thanks a lot :)
NVITEnd-user support

Commented:
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

Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Author

Commented:
Yup initially I lost but i figured it out - thanks again :)
NVITEnd-user support

Commented:
Awesome! Glad you figured it out.
End-user support
Commented:
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

Luis DiazIT consultant

Commented:
@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 Consultant
Top Expert 2016

Commented:
LD16,

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

~bp
Luis DiazIT consultant

Commented:
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 support

Commented:
LD16: Does the adjustment by Bill work for you?
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Thank you shragi.

~bp
Luis DiazIT consultant

Commented:
Perfect. I was able to add to my personal knowledge base.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial