How to compare two folders and replace the newer versions of files with the older one.

Any other tech-related topics
Post Reply
Message
Author
Marc
Posts: 165
Joined: Sun May 15, 2011 6:06 pm

How to compare two folders and replace the newer versions of files with the older one.

#1 Post by Marc »

I know this is a weird use case, hence I haven't found any app that does this but maybe there is and I just don't know it...
or perhaps it can be done with a batch script?

User avatar
lintalist
Posts: 434
Joined: Sat Apr 19, 2014 12:52 am
Contact:

Re: How to compare two folders and replace the newer versions of files with the older one.

#2 Post by lintalist »

Untested myself but from what I've read https://ss64.com/nt/robocopy.html (standard on Windows as far as I know, at least since a few years) will skip identical files (size, time, date) but copy files that have different timestamps - so if your source folder has older files, it should overwrite those in the target folder. A quick test should show you if it works and if so you can save it as a batch file.

User avatar
Andrew Lee
Posts: 3052
Joined: Sat Feb 04, 2006 9:19 am
Contact:

Re: How to compare two folders and replace the newer versions of files with the older one.

#3 Post by Andrew Lee »

Was going to suggest robocopy as well, or something like DSynchronize if you prefer a GUI.

User avatar
Midas
Posts: 6710
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Re: How to compare two folders and replace the newer versions of files with the older one.

#4 Post by Midas »

lintalist wrote: I've read https://ss64.com/nt/robocopy.html (standard on Windows as far as I know, at least since a few years)...
If a probably faster(?) CLI utility interests you, then maybe the older XCOPY command (also a CMD default) will do your bidding... its 'MAXAGE' and 'MINAGE' parameters work with file timestamps.


Marc
Posts: 165
Joined: Sun May 15, 2011 6:06 pm

Re: How to compare two folders and replace the newer versions of files with the older one.

#5 Post by Marc »

Thank you all for all the advices. :)
Tried DSynchronize but it doesn't have an option to replace newer files with older files.
Oj don't want to replace older files with newer files, but the opposite, so that if I have two files, the older one replaces the newer file.
I apologize should had better explained this before.
And thanks for robocoy &xcopy will give them a try..

Marc
Posts: 165
Joined: Sun May 15, 2011 6:06 pm

Re: How to compare two folders and replace the newer versions of files with the older one.

#6 Post by Marc »

None of them seem to support it :(

User avatar
lintalist
Posts: 434
Joined: Sat Apr 19, 2014 12:52 am
Contact:

Re: How to compare two folders and replace the newer versions of files with the older one.

#7 Post by lintalist »

I just tested it and RoboCopy simply copies the older file from source to target, I just did:

Code: Select all

Robocopy.exe source\ target\
output - note it copied one "older" file (file1.txt)

Code: Select all

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

   Source : C:\tmp\source\
     Dest : C:\tmp\target\

    Files : *.*

  Options : *.* /DCOPY:DA /COPY:DAT /R:1000000 /W:30

------------------------------------------------------------------------------

                           2    C:\tmp\source\
100%        Older                  31744        file1.txt

------------------------------------------------------------------------------

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         1         0         1         0         0         0
   Files :         2         1         1         0         0         0
   Bytes :    62.0 k    31.0 k    31.0 k         0         0         0
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00

Marc
Posts: 165
Joined: Sun May 15, 2011 6:06 pm

Re: How to compare two folders and replace the newer versions of files with the older one.

#8 Post by Marc »

with older files from the target and soource folders? because otherwise a regular operation would do.
i.e I have:

folder 1
file1 2022
file2 2019

folder 2
file1 2019
file2 2022

I need to end up with
file1 2019
file2 2019

User avatar
lintalist
Posts: 434
Joined: Sat Apr 19, 2014 12:52 am
Contact:

Re: How to compare two folders and replace the newer versions of files with the older one.

#9 Post by lintalist »

Try with /xn

Code: Select all

Robocopy.exe /xn source\ target\
seems to work here with the files setup as you've described them

Marc
Posts: 165
Joined: Sun May 15, 2011 6:06 pm

Re: How to compare two folders and replace the newer versions of files with the older one.

#10 Post by Marc »

Thank you!! =)

Post Reply