xcopy /d help - copy if date timestamp are different

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Maxis
Posts: 4
Joined: 28 Nov 2013 12:58

xcopy /d help - copy if date timestamp are different

#1 Post by Maxis » 28 Nov 2013 13:25

we do an xcopy /d of a folder with 10 files in it from source 1 to destination 1. if the file is missing or newer it works GREAT. but recently we found that the users could do a "save" or "save as" on a file in the destination 1 folder and thus create a file with a newer date than the source 1 folder with the same file name. when that happens, the xcopy /d doesn't copy the file to the destination 1 folder, and defeats the whole purpose of always wanting the source 1 folder files to over-write the destination 1 if the date timestamp is DIFFERENT regardless of newer or older in the destination 1 folder.

can someone provide an example of some dos batch that will delete all files in the destination 1 folder that don't match the date timestamp of the matching filename in the source 1 folder PLEASE?

the compare command isn't quite right, and i've google searched for hours with no luck. i'm sure others have come across this same requirements.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: xcopy /d help - copy if date timestamp are different

#2 Post by foxidrive » 28 Nov 2013 16:50

Is this a single folder or a folder tree of files?

Robocopy with the /mir mirror switch will mirror a folder or tree of files.
It is destructive too, in that it removes files which don't exist in the source, so don't get the paths wrong.

Maxis
Posts: 4
Joined: 28 Nov 2013 12:58

Re: xcopy /d help - copy if date timestamp are different

#3 Post by Maxis » 28 Nov 2013 17:40

unfortunately we only have access to windows xp dos commands... not a folder tree - just a folder with 10 files in both source 1 and destination 1

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: xcopy /d help - copy if date timestamp are different

#4 Post by foxidrive » 28 Nov 2013 17:53

This should delete files in the target that are not the same date and time stamp as in the source.

Code: Select all

@echo off
set "t=n:\target folder\"
pushd "c:\sourcefolder"
for %%a in (*.*) do (
   if exist "%t%\%%a" (
      for %%b in ("%t%\%%a") do (
         if not "%%~ta"=="%%~tb" del "%t%\%%a"
      )
   )
)



FWIW Robocopy can be downloaded from Microsoft for XP.

Maxis
Posts: 4
Joined: 28 Nov 2013 12:58

Re: xcopy /d help - copy if date timestamp are different

#5 Post by Maxis » 28 Nov 2013 18:01

thanks so much foxidrive ... i could never have come up with that...i'll test tomorrow

Maxis
Posts: 4
Joined: 28 Nov 2013 12:58

Re: xcopy /d help - copy if date timestamp are different

#6 Post by Maxis » 02 Dec 2013 11:13

Solution we went with: XCOPY both directions. The first XCOPY uses /L (list) and puts the output to a temp file of what files don't match. Then the FOR loop deletes the files found in the temp file. Then the temp file is deleted, and the next XCOPY replaces the files that were deleted (newer on destination side than the source side which is what we didn't want to happen). The source is always the master copy, even if the destination file somehow gets saved with a newer date and timestamp.

set OutofSyncFiles=c:\Temp\OutofSyncFiles.txt

rem ** Delete any files in the C: drive folder that have been change and don't match the source Master Copy on the server
xcopy "destination\*.*" "source" /L /D /Y > "%OutofSyncFiles%"
for /f "delims=" %%i in (%OutofSyncFiles%) do del "%%i"
del "%OutofSyncFiles%"

rem ** Sync up Source Master Copy on the Server to Destination on the C: drive
xcopy "source" "destination" /D /Y /Q /Z /I

Post Reply