Deleting Directories With Phing

13th January 2009 - 2 minutes read time

Although using Phing is mainly about copying files, you might also need to delete directories and files using the delete element. Remember that the default behavior of copy command copies files only if the source files are different from the destination files. A prudent approach might be to delete the build directory and then recreate it, ready for Phing to copy files into.

To delete a directory you need to use the dir attribute of the delete element, the delete element also accepts the file attribute to delete specific files. The following target will delete the directory myProject_build.

  1. <target name="prepare">
  2. <delete dir="myProject_build" />
  3. </target>

The delete element can also accept a fileset element, which will delete multiple files. the following code will delete any php file from the myProject_build folder and from a sub folder called app.

  1. <fileset dir="./" id="deleteFiles">
  2. <include name="myProject_build/*.php" />
  3. <include name="myProject_build/app/*.php" />
  4. </fileset>
  6. <target name="prepare">
  7. <delete>
  8. <fileset refid="deleteFiles" />
  9. </delete>
  10. </target>

Remember when doing this to remove any dir or file attributes from the delete element as they can interfere with your fileset. Also, if you use a fileset be careful not to use the same fileset that copies the files into the build directory as the only thing you will accomplish is the deletion of your application. This is quite easy to do, I even did it whilst testing the previous example!

Automated Build With Phing

Add new comment

The content of this field is kept private and will not be shown publicly.