Is there any command in ant to copy files from one folder structure to another without checking the last modified date/time overwriting files. Basically I want all extra files from folder A to folder B. That is: no files of folder B are replaced but extra files of folder A comes to folder B.
I saw "ant copy" and "ant move" commands, but didn't help. Any suggestions.
A FileSet is a group of files. These files can be found in a directory tree starting in a base directory and are matched by patterns taken from a number of PatternSets and Selectors. PatternSets can be specified as nested <patternset> elements.
Path: This object represents a path as used by CLASSPATH or PATH environment variable. A path might also be described as a collection of unique filesystem resources. and PathElement: Helper class, holds the nested <pathelement> values.
A target is a container of tasks and datatypes that cooperate to reach a desired state during the build process. Targets can depend on other targets and Apache Ant ensures that these other targets have been executed before the current target.
Ant's copy task's overwrite attribute states: "Overwrite existing files even if the destination files are newer."
So setting it to false only prevents them from being overwritten if they are newer, but if the original files are newer, they overwrite no matter what.
The only way i've found to get around this is to touch the destination files before copying, i.e:
<touch>
<fileset dir="${dest.dir}"/>
</touch>
<copy todir="${dest.dir}" overwrite="false">
<fileset dir="${src.dir}"/>
</copy>
you can do it as below:
Copy a single file
<copy file="myfile.txt" tofile="mycopy.txt"/>
Copy a single file to a directory
<copy file="myfile.txt" todir="../some/other/dir"/>
Copy a directory to another directory
<copy todir="../new/dir"><fileset dir="src_dir"/></copy>
Copy a set of files to a directory and more...
please see this link for more information.Copy Task
While <touch>
works, it updates the file dates in the destination and as such it is not exactly true to the request.
According to the documentation If you have at least Ant 1.6.2, you can use the granularity attribute.
The number of milliseconds leeway to give before deciding a file is out of date. This is needed because not every file system supports tracking the last modified time to the millisecond level. Default is 1 second, or 2 seconds on DOS systems. This can also be useful if source and target files live on separate machines with clocks being out of sync. since Ant 1.6.2.
just set it to a large number. I use 9223372036854, which should be about 292 years (probably this is enough). (I just chopped the last 6 digits off Long.max) which is enough to not generate warnings about dates being in the future
<copy todir="${dest.dir}" overwrite="false" granularity="9223372036854">
<fileset dir="${src.dir}"/>
</copy>
this will guarantee that if the files are there at all they will be see as not old enough by the ant task and thus, not overwritten (and also not touched by another task)
If there isn't an elegant solution you can always do it in a sequence:
<move>
folder <B>
to <C>
<copy>
files of folder <A>
to <B>
<copy>
files of folder <C>
to <B>
using overwrite=true
(Do things the other way around, replacing the new files, not the old ones)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With