Max
Max

Reputation: 51

How do I copy a directory in Ant without deleting existing files in the destination directory?

I have the following file structure:

I am copying FolderToBeCopied to a location which already contains files:

I copy using the following in my Ant build script:

<copy overwrite="true" todir="DestinationFolder">
            <fileset dir="FolderToBeCopied" includes="**">
            </fileset>
        </copy>

However, when I run the build script, it copies the files somefile1 and somefile2 to folder1 at the destination, but deletes the files already in folder1 (ie. anotherfile1, anotherfile2). Is there a way to prevent it deleting files already in the destination folder?

Upvotes: 2

Views: 1668

Answers (2)

JuanZe
JuanZe

Reputation: 8157

In order to prevent deletion of existing files in the destination folder, you could make a backup appending a timestamp to the filenames:

<project name="demoSO" basedir=".">

   <tstamp>
    <format property="touch.time" pattern="yyMMddHHmmssSSS"/>
  </tstamp>

  <target name="copyMyFiles">
    <copy todir="DestinationFolder" includeemptydirs="false">
      <fileset dir="FolderToBeCopied">
      </fileset>
      <mapper type="glob" from="*" to="*-${touch.time}"/>
    </copy>
  </target>

</project>

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328556

Yes: Locate the delete element in your build script which deletes DestinationFolder and delete it.

copy doesn't delete. overwrite only means "copy even if the target is older than the source".

Upvotes: 1

Related Questions