Deleting Directories With Phing

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.

<target name="prepare">
<delete dir="myProject_build" />
</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.

<fileset dir="./" id="deleteFiles">
 <include name="myProject_build/*.php" />
 <include name="myProject_build/app/*.php" />
</fileset>
 
<target name="prepare">
  <delete>
    <fileset refid="deleteFiles" />
  </delete>
</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.
CAPTCHA
12 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.