Basically, in an automated batch file, I want to copy and rename a file if the destination file is missing or older. There are several variants, which do not quite work:
copy /Y c:\source\a.file c:\dest\b.file
– always copies, no /D option or something
xcopy /Y /D c:\source\a.file c:\dest\b.file
– if destination does not exist, tries to ask if the destination is a file or directory, creating havoc in the automated build.
robocopy /XO ...
– does not support renaming the file.
It seems Windows has not managed to replicate Unix "cp -u" in 20+ years, or am I overlooking something?
Best Answer
This may be more than you need but with the proper tweaking it should do the job. I did some experimenting and it seems that if you use xcopy with the /D option and specify a destination filename, you will get a copy whether the files are the same or not. (Seems like it's basing the compare off the destination filename instead of the source.) So I only used xcopy to do the comparison but not actually to do the copy. (Thanks selbie for suggesting the /L option.) It seems rather large but if you remove the comments it's pretty small. This example copies and renames files that exist in the destination but are newer using the current date and time. It copies but does not rename files that don't exist in the destination (can be easily changed). And does not copy files that are the same. I can help you modify it to fit your specific needs.