I have two folders in Windows, one called DATA and another called COPIES , they would be the source and destination directories, respectively. By running a batch I can create copies of the source content to the destination directory, using ROBOCOPY , for example.
The first copy I make is complete, that is:
ROBOCOPY C:\DATOS D:\COPIAS
The next day, I run the batch again, but this time I am only interested in copying the new or modified files since the last copy, I would do something like this:
ROBOCOPY C:\DATOS D:\COPIAS \XO
The \XO
(e X include Olders ) statement excludes older files. This works correctly, and is fine.
Now comes the problem:
I want the execution of the batch not to overwrite the files that are already in the destination, but to copy them as a new document ( mi libro (1).docx
, for example). This is so that, in case of making an error in the source file, I can choose between the previous versions of my destination directory in recovery mode. Something like Dropbox, where you can see a history of the versions of the same file.
Searching the official information of the ROBOCOPY command, I ca n't find any instructions that tell me how to proceed.
I also tried reviewing these queries on the English site, but they don't work in real environment:
robocopy c:\Sourcepath c:\Destpath /E /XC /XN /XO
Also with the XCOPY command:
echo n | copy /-y <SOURCE> <DESTINATION>
The latter just doesn't overwrite, but it doesn't copy either, that is, it doesn't do anything.
Is there another alternative? Has a similar problem happened to someone in the community?
Thanks for your time and attention.
Robocopy doesn't support what you need, so you'll have to do it programmatically. A programming language like python, ruby, php or perl will give you more flexibility.
However if you want a purely batch solution you could do something similar to this. Here is a basic example where changed files are copied by adding " - copy x" (with additional 'x's)
The idea is to call
xcopy
with the parameter/D
(which makes it only try to "copy" the files that have changed) and with/L
(which does not copy the files, it only prints what it is going to copy)This way you get a list of what has changed.
This list can be 'captured' and traversed with the buble
FOR
. I recomend this reading to see all the options that the FOR statement allows and how it works.For each file that we know has changed, we'll look to see if there is any version " - copy x" in the destination, and if so, we'll stick with the one with the most 'x's.
We do this by capturing the output of the command
DIR
, sorting by filename.For the subject of adding additional "x"s I recommend this other reading where the extensions of the parameters are explained. Extensions are used to, among other things, 'separate' a file's path from its extension, the file without the path, etc.
Finally we make the copy of the file to the destination with the "copy name"