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 » Sat Sep 24, 2022 11:52 am

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: 348
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 » Sun Sep 25, 2022 2:52 am

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: 2860
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 » Sun Sep 25, 2022 4:51 am

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

User avatar
Midas
Posts: 6337
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 » Sun Sep 25, 2022 12:25 pm

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 » Mon Sep 26, 2022 8:10 am

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 » Wed Sep 28, 2022 6:34 pm

None of them seem to support it :(

User avatar
lintalist
Posts: 348
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 » Wed Sep 28, 2022 11:32 pm

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 » Thu Sep 29, 2022 1:28 pm

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: 348
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 » Fri Sep 30, 2022 1:20 am

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 » Thu Oct 06, 2022 8:09 pm

Thank you!! =)

Post Reply