I need to replace all the files contained in a dir with all the files that exists in other.
This would be a pseudo code for this task:
foreach (string file in /foo/var)
{
srcFile = "/other/dir/" + GetFileName(file);
Copy(srcFile, file);
}
I need to replace the file only if exists in both /foo/var
and /other/dir
. Also there are files that exist in /foo/var
and do not exist in /other/dir
and vice versa.
What is the best way to do it with NAnt?
This should do the job. It will only look at files in the root target directory and ignore subfolders. It will need a bit more work if you want it to include files in subfolders
<project name="Sync" default="sync" xmlns="http://nant.sf.net/release/0.85/nant.xsd">
<property name="source.path" value="D:\test\source\" />
<property name="target.path" value="D:\test\target\" />
<target name="sync" description="Copies only the matching files to a folder">
<foreach item="File" property="targetFile">
<in>
<items basedir="${target.path}"> <!-- include all files from the target path -->
<include name="*" /> <!-- this will not include subfolders -->
</items>
</in>
<do>
<if test="${file::exists(source.path + path::get-file-name(targetFile))}">
<copy
file="${source.path + path::get-file-name(targetFile)}"
tofile="${targetFile}"
overwrite="true"
/>
</if>
</do>
</foreach>
</target>
</project>
I tested it with Nant 0.86