Update set of files using batch file

Microsoft / Dos
October 22, 2009 at 06:39:21
Specs: Windows XP
I need a batch file that can compare the contents of 2 sets of folders (folder#1 and folder#2) by name and then do the following for all files:
If a file exists only in #1 => delete file
if file exists only in #2 => copy this file files to #1
if file (with same name) exists in both #1 and #2 overwrite file in #1 with file from #2, but only if they have different content.

Is this possible?

Cheers


See More: Update set of files using batch file

Report •


#1
October 22, 2009 at 08:35:42
Set the paths for Folder_A and Folder_B into the two variables and run. Beware the script was not tested. Report if anything goes wrong.

By the way this is NT batch not DOS and should be posted in the Programming Forum.

@echo off & setlocal

set Folder_A=
set Folder_B=

pushd "%Folder_A%"
for %%j in (*) do (
  if not exist "%Folder_B%\%%j" (
    del "%%j"
  ) else (
    fc /B "%Folder_B%\%%j" "%%j" > nul || copy "%Folder_B%\%%j"
  )
)
popd
pushd "%Folder_B%"
for %%j in (*) do (
  if not exist "%Folder_A%\%%j" copy "%%j" "%Folder_A%"
)
popd


Report •

#2
October 22, 2009 at 10:01:39
Excellent! That works perfectly. Thanks very much for your time IVO. I'm sorry for posting in the wrong forum.

If you don't mind helping me further please...

If the sets of folders had sub folders, how could this be extended to cope with that?

Thanks again


Report •

#3
October 22, 2009 at 13:37:48
The subfolders introduce a co,plexity that must be defined, i.e. the original question exactly explains the relation about the presence of files in Folder_A and Folder_B. When a tree structure of folders must be scanned how do we process files located in different directories with root in A or B?

In other words the clear relation binding the two sets vanishes due to the multiple folders/levels now present. The question needs to be carefully re-planned.


Report •

Related Solutions

#4
October 22, 2009 at 15:39:11
Hello IVO, thanks again for your response.

Yes I see what you mean.

If a folder exists only in #1 then it should be deleted along with all of it's sub folders and files (if it has any).
If a folder exists only in #2 then it should be copied to #1 (at the same level) along with all it's sub folders and files (if it has any).
If a folder with the same name exists in both #1 and #2 we need to deal with their sub folders and files in the same way as we have delt with files and folders before.


Report •

#5
October 25, 2009 at 14:27:26
During the weekend I performed some tests and did go deeply into the question, but I have to give up as scanning a tree structure leads to too much cumbersome code (almost in batch).

The amount of testing required to produce reliable code adds to the reasons that suggest the challenge is not worth of.

Sorry, I do not say the issue has no solution by batch scripting, just the work required is too expensive.


Report •

#6
October 26, 2009 at 04:23:57
Thank you for your time IVO. I have been thinking the same thing and have started working on a different solution to the same problem.

If I have a text file with a list of the files (full path names) from the 2 structures in the following format:

LIST#1: files that exist in #1 and #2 but are different:
<list of files (full path names)>
LIST#2: folders and files that exist only in #1:
<list of files and folders (full path names)>
LIST#3: folders and files that exist only in #2:
<list of files and folders (full path names)>

Could I then have a batch file that would:
OVERWRITE #1 files in LIST#1 with #2 files.
DELETE files form LIST#2
COPY files in LIST#3 over to #1 ?

I think this solution is easier to implement in batch files but so far I have been unable to achieve it. Could you help with this please?

Thanks


Report •


Ask Question