Computing.Net > Forums > Windows XP > Update set of files using batch file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Update set of files using batch file

Reply to Message Icon

Name: gman8
Date: October 22, 2009 at 06:39:21 Pacific
OS: Windows XP
Product: Microsoft / Dos
Subcategory: General
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: October 22, 2009 at 08:35:42 Pacific
Reply:

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


1

Response Number 2
Name: gman8
Date: October 22, 2009 at 10:01:39 Pacific
Reply:

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


0

Response Number 3
Name: IVO
Date: October 22, 2009 at 13:37:48 Pacific
Reply:

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.


1

Response Number 4
Name: gman8
Date: October 22, 2009 at 15:39:11 Pacific
Reply:

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.


0

Response Number 5
Name: IVO
Date: October 25, 2009 at 14:27:26 Pacific
Reply:

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.


1

Related Posts

See More



Response Number 6
Name: gman8
Date: October 26, 2009 at 04:23:57 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Update set of files using batch file

Defragmenting Using Batch File www.computing.net/answers/windows-xp/defragmenting-using-batch-file/136038.html

what kind of file do I have? www.computing.net/answers/windows-xp/what-kind-of-file-do-i-have/29684.html

Using BATCH file to backup data www.computing.net/answers/windows-xp/using-batch-file-to-backup-data/66927.html