Question

I need a build script to be able to copy a folder with all its nested files and folders to another folder. The destination folder already contains its own files and folders, and its nested folder structure can overlap with the source folder. Having executed some commands, I then need to delete only the files that were copied.

Example:

src_folder
  subfolder1
    file1

dest_folder
  subfolder1
    file2
    file3

Here I need to delete only dest_folder/subfolder1/file1

Is there a way to do this with Ant? If not, can you suggest an alternative build tool that makes this possible?

One way to accomplish similar results would be to back up dest_folder, but it has a lot of files and it takes too long to copy it.

Was it helpful?

Solution

Example

Project files

├── build.xml
├── dest_folder
│   └── subfolder1
│       ├── file2
│       └── file3
└── src_folder
    └── subfolder1
        └── file1

Build output

$ ant
Buildfile: build.xml

copy-files:
     [copy] Copying 1 file to dest_folder
     [copy] Copying src_folder/subfolder1/file1 to dest_folder/subfolder1/file1

run-cmd-on-files-in-dest-folder:
    [apply] dest_folder/subfolder1/file1

clean-files:
   [delete] Deleting dest_folder/subfolder1/file1

build.xml

<project name="demo" default="run">

   <target name="run" depends="copy-files,run-cmd-on-files-in-dest-folder,clean-files"/>

   <target name="copy-files">
      <copy todir="dest_folder" verbose="true">
         <fileset dir="src_folder"/>
      </copy>
   </target>

   <target name="run-cmd-on-files-in-dest-folder">
      <apply executable="echo">
         <srcfile/>
         <fileset dir="dest_folder">
            <present present="both" targetdir="src_folder"/>
         </fileset>
      </apply>
   </target>

   <target name="clean-files">
      <delete verbose="true">
         <fileset dir="dest_folder">
            <present present="both" targetdir="src_folder"/>
         </fileset>
      </delete>
   </target>

</project>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top