Avatar of shragi
shragiFlag for India

asked on 

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.
VB ScriptSystem ProgrammingWindows OS

Avatar of undefined
Last Comment
Luis Diaz
Avatar of NVIT
NVIT
Flag of United States of America image

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

Avatar of shragi
shragi
Flag of India image

ASKER

Hi NVIT - that solution worked thanks a lot :)
Avatar of NVIT
NVIT
Flag of United States of America image

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

Avatar of shragi
shragi
Flag of India image

ASKER

Yup initially I lost but i figured it out - thanks again :)
Avatar of NVIT
NVIT
Flag of United States of America image

Awesome! Glad you figured it out.
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Luis Diaz
Luis Diaz
Flag of Colombia image

@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.
Avatar of Bill Prew
Bill Prew

LD16,

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

~bp
Avatar of Luis Diaz
Luis Diaz
Flag of Colombia image

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.
Avatar of NVIT
NVIT
Flag of United States of America image

LD16: Does the adjustment by Bill work for you?
Avatar of Bill Prew
Bill Prew

Thank you shragi.

~bp
Avatar of Luis Diaz
Luis Diaz
Flag of Colombia image

Perfect. I was able to add to my personal knowledge base.
Windows OS
Windows OS

This topic area includes legacy versions of Windows prior to Windows 2000: Windows 3/3.1, Windows 95 and Windows 98, plus any other Windows-related versions including Windows Mobile.

129K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo